#include<bits/stdc++.h>
using namespace std;
#define N 200100
#define SZ 410
int CC,in[N],out[N],id[N],prc[SZ][SZ],hv[SZ],CC2,reg[N],ANS;
vector<int>hav[N],adj[N];
map<int,int> sup[SZ],sub[SZ];
void upd(map<int,int>&mp,int x,int v){
while(x<N) mp[x]+=v,x+=x&-x;
}
void query(map<int,int>&mp,int x,int v){
while(x) ANS+=mp[x]*v,x-=x&-x;
}
void dfs(int n){
if(id[reg[n]])for(int i=1;i<SZ;i++)
prc[i][id[reg[n]]]+=hv[i];
hv[id[reg[n]]]++; in[n]=++CC;
for(auto i:adj[n])dfs(i);
hv[id[reg[n]]]--; out[n]=CC;
if(id[reg[n]])
upd(sup[id[reg[n]]],in[n],1),
upd(sup[id[reg[n]]],out[n]+1,-1),
upd(sub[id[reg[n]]],in[n],1);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int n,q,r;
cin>>n>>r>>q;
cin>>reg[1];
for(int i=2;i<=n;i++){
int p;cin>>p>>reg[i];
adj[p].push_back(i);
}
for(int i=1;i<=n;i++)
hav[reg[i]].push_back(i);
for(int i=1;i<=r;i++)
if(hav[i].size()>=500)
id[i]=++CC2;
dfs(1);
while(q--){
int a,b;
cin>>a>>b;
ANS=0;
if(id[a]&&id[b])
ANS=prc[id[a]][id[b]];
else if(id[a]||id[b])
if(hav[a].size()>hav[b].size())
for(auto i:hav[b])
query(sup[id[a]],in[i],1);
else for(auto i:hav[a])
query(sub[id[b]],in[i]-1,-1),
query(sub[id[b]],out[i],1);
else {
map<int,int>mp;
for(auto i:hav[a])
upd(mp,in[i],1),
upd(mp,out[i]+1,-1);
for(auto i:hav[b])
query(mp,in[i],1);
}
cout<<ANS<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10840 KB |
Output is correct |
4 |
Correct |
6 ms |
10840 KB |
Output is correct |
5 |
Correct |
12 ms |
10840 KB |
Output is correct |
6 |
Correct |
17 ms |
10840 KB |
Output is correct |
7 |
Correct |
55 ms |
10840 KB |
Output is correct |
8 |
Correct |
59 ms |
11020 KB |
Output is correct |
9 |
Correct |
114 ms |
11352 KB |
Output is correct |
10 |
Correct |
341 ms |
11352 KB |
Output is correct |
11 |
Correct |
926 ms |
11608 KB |
Output is correct |
12 |
Correct |
898 ms |
12112 KB |
Output is correct |
13 |
Correct |
1617 ms |
11608 KB |
Output is correct |
14 |
Correct |
3606 ms |
12120 KB |
Output is correct |
15 |
Correct |
3683 ms |
15192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
8048 ms |
59908 KB |
Time limit exceeded |
2 |
Execution timed out |
8068 ms |
65540 KB |
Time limit exceeded |
3 |
Execution timed out |
8057 ms |
43160 KB |
Time limit exceeded |
4 |
Correct |
2738 ms |
12120 KB |
Output is correct |
5 |
Correct |
2186 ms |
14212 KB |
Output is correct |
6 |
Execution timed out |
8098 ms |
23304 KB |
Time limit exceeded |
7 |
Execution timed out |
8031 ms |
14168 KB |
Time limit exceeded |
8 |
Execution timed out |
8019 ms |
25540 KB |
Time limit exceeded |
9 |
Execution timed out |
8098 ms |
18892 KB |
Time limit exceeded |
10 |
Execution timed out |
8087 ms |
24956 KB |
Time limit exceeded |
11 |
Execution timed out |
8077 ms |
17944 KB |
Time limit exceeded |
12 |
Execution timed out |
8058 ms |
43640 KB |
Time limit exceeded |
13 |
Execution timed out |
8048 ms |
41480 KB |
Time limit exceeded |
14 |
Execution timed out |
8023 ms |
127024 KB |
Time limit exceeded |
15 |
Execution timed out |
8034 ms |
43644 KB |
Time limit exceeded |
16 |
Execution timed out |
8061 ms |
51460 KB |
Time limit exceeded |
17 |
Execution timed out |
8029 ms |
112116 KB |
Time limit exceeded |