codeforces 570 D. Tree Requests 树状数组+dfs搜索序

链接:http://codeforces.com/problemset/problem/570/D


D. Tree Requests
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the n - 1 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex i is vertex pi, the parent index is always less than the index of the vertex (i.e., pi < i).

The depth of the vertex is the number of nodes on the path from the root to v along the edges. In particular, the depth of the root is equal to 1.

We say that vertex u is in the subtree of vertex v, if we can get from u to v, moving from the vertex to the parent. In particular, vertex v is in its subtree.

Roma gives you m queries, the i-th of which consists of two numbers vihi. Let's consider the vertices in the subtree vi located at depthhi. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used.

Input

The first line contains two integers nm (1 ≤ n, m ≤ 500 000) — the number of nodes in the tree and queries, respectively.

The following line contains n - 1 integers p2, p3, ..., pn — the parents of vertices from the second to the n-th (1 ≤ pi < i).

The next line contains n lowercase English letters, the i-th of these letters is written on vertex i.

Next m lines describe the queries, the i-th line contains two numbers vihi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in thei-th query.

Output

Print m lines. In the i-th line print "Yes" (without the quotes), if in the i-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes).

Sample test(s)
input
6 5
1 1 1 3 3
zacccd
1 1
3 3
4 1
6 1
1 2
output
Yes
No
Yes
Yes
Yes
Note

String s is a palindrome if reads the same from left to right and from right to left. In particular, an empty string is a palindrome.

Clarification for the sample test.

In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z".

In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d" respectively. It is impossible to form a palindrome of them.

In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome.

In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome.

In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c" and "c". We may form a palindrome "cac".



题意:

告诉你一颗树的父子关系,1节点为根,再告诉你每个点上的字母。

问 v节点 子树(包括v节点)在第h行的所有节点的字母能否组成回文串。


做法:

先用dfs 搜索 把所有节点标个左标号和右标号。 这样标号以后,每个节点 用左标号 当自己 新的标号。 然后  子树所有节点 的新标号 肯定在 子树根节点的 左右标号之间。

标号之后分层来做。

每层  对每个字母分别做统计。

把该层所有节点 的 左标号 在树状数组中+1. 然后对于该层的所有询问 做 树状数组统计,(sum(rit[v])-sum(lft[v]-1))。   

如果是奇数 说明这个 字母在查询的区间内 有奇数个。

每个查询  最多有一个奇数个的字母,否则不能构成回文串


#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<math.h>

using namespace std;
const int N = 500100;

int f[N];
vector<int> son[N];
int id;
int lft[N],rit[N];
int deps[N]; 
int ans[N];
char str[N];
vector<int> G[N];//深度
vector<pair<int,int> > Q[N];
void dfs(int nw,int dep)
{
	lft[nw]=id++;
	deps[nw]=dep;
	G[dep].push_back(nw);
	for(int i=0;i<son[nw].size();i++)
	{
		int to=son[nw][i];
		dfs(to,dep+1);
	}
	rit[nw]=id++;
}

int bit[2*N];

int lowbit(int x)
{
	return x&(-x);
}

void add(int wei,int x)
{

	while(wei<=id)
	{
		bit[wei]+=x;
		wei+=lowbit(wei);
	}
}

int sum(int wei)
{
	if(wei==0)
		return 0;
	int sum=0;
	while(wei>0)
	{
		sum+=bit[wei];
		wei-=lowbit(wei);
	}
	return sum;
}

int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=2;i<=n;i++)
		{
			scanf("%d",f+i);
			son[f[i]].push_back(i);
		}
		scanf("%s",str+1);
		id=1;
		dfs(1,1);
		int dep=1;
		for(int i=1;i<=m;i++)
		{
			int vv,hh;
			scanf("%d%d",&vv,&hh);
			dep=max(hh,dep);
			Q[hh].push_back(make_pair<int,int>(vv,i));
		}

		for(int i=1;i<=dep;i++)
		{
			for(int j=0;j<26;j++)
			{
				if(j==25)
					int kkk=1;
				for(int k=0;k<G[i].size();k++)//每个节点
				{
					if(str[G[i][k]]-'a'==j)
						add(lft[G[i][k]],1);
				}
				
				for(int k=0;k<Q[i].size();k++)
				{
					int v=Q[i][k].first;
					int ii=Q[i][k].second;
					if((sum(rit[v])-sum(lft[v]-1))&1) ans[ii]++;
				}

				 
				for(int k=0;k<G[i].size();k++)//每个节点
				{
					if(str[G[i][k]]-'a'==j)
						add(lft[G[i][k]],-1);
				}
				// printf("jj %d \n",j);
			}

			//printf("ii %d \n",i);
		}

		for(int i=1;i<=m;i++)
		{
			if(ans[i]<=1)
				printf("Yes\n");
			else
				printf("No\n");
		}

	}
	return 0;
}



 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick Tree)是一种用于高效处理区间和查询的数据结构,常用于解一维数组的前缀和、区间更新和查询等问题。 在 Codeforces 上,树状数组常被用来解决一些与区间和查询有关的问题。它可以在 O(logn) 的时间内完成单点更新和查询,以及区间求和等操作。 下面是一个简单的示例代码,展示了如何实现一个基本的树状数组: ```cpp #include <iostream> #include <vector> using namespace std; // 获取最低位的 1 int getLowbit(int x) { return x & -x; } // 树状数组的单点更新操作 void update(vector<int>& fenwick, int index, int delta) { while (index < fenwick.size()) { fenwick[index] += delta; index += getLowbit(index); } } // 树状数组的前缀和查询操作 int query(vector<int>& fenwick, int index) { int sum = 0; while (index > 0) { sum += fenwick[index]; index -= getLowbit(index); } return sum; } int main() { int n; cin >> n; vector<int> fenwick(n + 1, 0); // 初始化树状数组 for (int i = 1; i <= n; i++) { int val; cin >> val; update(fenwick, i, val); } // 进行查询操作 int q; cin >> q; while (q--) { int type; cin >> type; if (type == 1) { int index, delta; cin >> index >> delta; update(fenwick, index, delta); } else if (type == 2) { int l, r; cin >> l >> r; int sum = query(fenwick, r) - query(fenwick, l - 1); cout << sum << endl; } } return 0; } ``` 在这个示例中,我们使用了一个长度为 n 的数组 `fenwick` 来表示树状数组。`update` 函数用于更新树状数组中的某个元素,`query` 函数用于查询树状数组中某个区间的和。 你可以根据具体问题的要求进行相应的修改和扩展。希望对你有所帮助!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值