#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,i,j,ii,jj,zx,xc,tes,t,f[200009],k[630],T=320,K[25009],F[25009],TT,lf[200009],rg[200009],tim,C,D,lef,rig,mid;
long long ans[630][25002],ZX;
vector <int> v[200009];
vector <pair <int, int> > vv[200009];
void dfs(int q, int w){
tim++;
lf[q]=rg[q]=tim;
int qw=vv[f[q]].size();
vv[f[q]].push_back(make_pair(lf[q],0));
for(int h=1; h<=TT; h++){
ans[h][f[q]]+=k[h];
}
if(F[f[q]]!=0) k[F[f[q]]]++;
for(vector <int>::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it)==w) continue;
dfs((*it),q);
if(rg[q]<rg[(*it)]) rg[q]=rg[(*it)];
}
vv[f[q]][qw].second=rg[q];
if(F[f[q]]!=0) k[F[f[q]]]--;
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>zx>>tes;
cin>>f[1];
for(i=2; i<=a; i++){
cin>>c>>f[i];
v[c].push_back(i);
}
for(i=1; i<=a; i++){
K[f[i]]++;
}
TT=0;
for(i=1; i<=a; i++){
if(K[i]>T){
TT++;F[i]=TT;
}
}
dfs(1,0);
for(t=1; t<=tes; t++){
cin>>c>>d;
if(vv[c].size()>T){
cout<<ans[F[c]][d]<<endl;
}else{
C=vv[c].size();D=vv[d].size();ZX=0;
for(vector <pair <int, int> >::iterator it=vv[c].begin(); it!=vv[c].end(); it++){
lef=-1;rig=D;
while(lef+1<rig){
mid=((lef+rig)>>1);
if(vv[d][mid].first>=(*it).first){
rig=mid;
}else{
lef=mid;
}
}
ii=rig;
lef=ii-1;rig=D;
while(lef+1<rig){
mid=((lef+rig)>>1);
if(vv[d][mid].first<=(*it).second){
lef=mid;
}else{
rig=mid;
}
}
jj=lef;
ZX+=(jj-ii+1);
}
cout<<ZX<<endl;
}
}
return 0;
}
Compilation message
regions.cpp: In function 'int main()':
regions.cpp:44:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | if(vv[c].size()>T){
| ~~~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9704 KB |
Output is correct |
2 |
Correct |
6 ms |
9620 KB |
Output is correct |
3 |
Correct |
8 ms |
9672 KB |
Output is correct |
4 |
Correct |
10 ms |
9672 KB |
Output is correct |
5 |
Correct |
12 ms |
9832 KB |
Output is correct |
6 |
Correct |
14 ms |
9836 KB |
Output is correct |
7 |
Correct |
34 ms |
9800 KB |
Output is correct |
8 |
Correct |
35 ms |
9800 KB |
Output is correct |
9 |
Correct |
49 ms |
10440 KB |
Output is correct |
10 |
Correct |
91 ms |
10184 KB |
Output is correct |
11 |
Correct |
101 ms |
10596 KB |
Output is correct |
12 |
Correct |
139 ms |
11456 KB |
Output is correct |
13 |
Correct |
200 ms |
10716 KB |
Output is correct |
14 |
Correct |
229 ms |
11396 KB |
Output is correct |
15 |
Correct |
328 ms |
15768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1365 ms |
15224 KB |
Output is correct |
2 |
Correct |
1527 ms |
13392 KB |
Output is correct |
3 |
Correct |
2739 ms |
17884 KB |
Output is correct |
4 |
Runtime error |
34 ms |
27032 KB |
Execution killed with signal 11 |
5 |
Runtime error |
34 ms |
27996 KB |
Execution killed with signal 11 |
6 |
Runtime error |
35 ms |
28476 KB |
Execution killed with signal 11 |
7 |
Runtime error |
44 ms |
29636 KB |
Execution killed with signal 11 |
8 |
Runtime error |
53 ms |
32588 KB |
Execution killed with signal 11 |
9 |
Runtime error |
73 ms |
35320 KB |
Execution killed with signal 11 |
10 |
Runtime error |
85 ms |
37596 KB |
Execution killed with signal 11 |
11 |
Runtime error |
91 ms |
34180 KB |
Execution killed with signal 11 |
12 |
Runtime error |
78 ms |
38756 KB |
Execution killed with signal 11 |
13 |
Runtime error |
74 ms |
37696 KB |
Execution killed with signal 11 |
14 |
Runtime error |
79 ms |
37424 KB |
Execution killed with signal 11 |
15 |
Runtime error |
80 ms |
39616 KB |
Execution killed with signal 11 |
16 |
Runtime error |
78 ms |
39596 KB |
Execution killed with signal 11 |
17 |
Runtime error |
80 ms |
39772 KB |
Execution killed with signal 11 |