http://codeforces.com/contest/570/problem/D
仔细想来,还真没做过一道dfs序的题....
先将查询按深度排序,对于要操作的深度,将树上的这一层都放到树状数组上。查询的时候,就查询这颗子树内各个字母出现的个数。
#include
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i = 0;i < n;i ++)
#define repe(i,n) for(int i=1;i<=n;i++)
#define mst(A,k) memset(A,k,sizeof(A))
struct Qry{
int u,h,o;
Qry(){}
Qry(int u,int h):u(u),h(h){}
void read(int oo)
{
o = oo;
scanf("%d%d",&u,&h);
}
bool operator<(const Qry& a)const
{
return h < a.h;
}
}qry[500005];
int n,q;
int fst[500005],nxt[500005],v[500005],tol;
int dep[500005],in[500005],out[500005];
char str[500005];
int C[26][500005];
bool ans[500005];
vector
D[500005];
void dfs(int u,int d,int& scc)
{
in[u] = ++scc;
dep[u] = d;
D[d].push_back(u);
for(int e=fst[u]; ~e; e=nxt[e])
{
dfs(v[e],d+1,scc);
}
out[u] = scc;
}
void edge(int a,int b)
{
v[tol] = b;
nxt[tol] = fst[a];
fst[a] = tol++;
}
int lowbit(int x)
{
return x & (-x);
}
void Add(char ch,int x,int w)
{
int o = ch - 'a';
while(x <= n)
{
C[o][x] += w;
x += lowbit(x);
}
}
int Sum(int o,int x)
{
int res = 0;
while(x)
{
res += C[o][x];
x -= lowbit(x);
}
return res;
}
int main() {
scanf("%d%d",&n,&q);
int fa;
tol = 0;
mst(fst,-1);
for(int i=2; i<=n; i++)
{
scanf("%d",&fa);
edge(fa,i);
}
scanf("%s",str+1);
for(int i=0; i