D. Tree Requests

https://codeforces.com/gym/241159/problem/D

#include
using namespace std;
typedef long long ll;
string ch;
vector vec[500100];
vector ans[500100];
vector > num[500100];
struct node{
	int l;
	int r;
	int depth;
}tree[500100];
int sign;
void dfs(int id,int depth){
	tree[id].l=sign;
	tree[id].depth=depth;
	ans[depth].push_back(id);
	for(int i=0;i>n>>m;
	for(int i=2;i<=n;i++){
		scanf("%d",&temp);
		vec[temp].push_back(i);
	}
	cin>>ch;
	sign=0;
	dfs(1,1);
	for(int i=1;i<=n;i++){
		for(int j=0;j'm')
				fir+=(1<<(ch[ans[i][j]-1]-'n'));
			else
				sec+=(1<<(ch[ans[i][j]-1]-'a'));
			if(j!=0){
				fir^=num[i][j-1].first;
				sec^=num[i][j-1].second;
			}
			num[i].push_back(make_pair(fir,sec));
		}
	}
	int v,h;
	while(m--){
		scanf("%d%d",&v,&h);
		int l=0;
		int r=ans[h].size()-1;
		int templ=r+1,tempr=-1;
		while(l<=r){
			int mid=(l+r)/2;
			if(tree[ans[h][mid]].l<=tree[v].l){
				l=mid+1;
			}
			else{
				r=mid-1;
				templ=mid;
			}
		}
		l=0;
		r=ans[h].size()-1;
		while(l<=r){
			int mid=(l+r)/2;
			if(tree[ans[h][mid]].l>=tree[v].r){
				r=mid-1;
			}
			else{
				l=mid+1;
				tempr=mid;
			}
		}
		ll fir=0,sec=0;
		if(templ>0&&tempr

 

你可能感兴趣的:(D. Tree Requests)