This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int bucket=450;
int parent[200001];
vector<int>children[200001];
int reg[200001];///regions
vector<int>indexx[25001];
vector<int>regions[25001];
int ticker=0;
int beg[200001],en[200001];
void dfs(int n) {
indexx[reg[n]].push_back(ticker);
beg[n]=ticker++;
for(int i:children[n]) {
dfs(i);
}
en[n]=ticker-1;
}
vector<int>large;
int num[25001];
map<pair<int,int>,int>ans;
void dfs1(int n) {
for(int i:large) {
ans[{i,reg[n]}]+=num[i];
}
num[reg[n]]++;
for(int i:children[n]) {
dfs1(i);
}
num[reg[n]]--;
}
int main() {
int n,r,q;cin>>n>>r>>q;
cin>>reg[1];
regions[reg[1]].push_back(1);
for(int i=2;i<=n;i++) {
cin>>parent[i]>>reg[i];
children[parent[i]].push_back(i);
regions[reg[i]].push_back(i);
}
dfs(1);
for(int i=1;i<=r;i++) {
if(indexx[i].size()>=bucket)large.push_back(i);
}
dfs1(1);
for(int i=0;i<q;i++) {
int x,y;cin>>x>>y;
if(indexx[x].size()>=bucket)cout<<ans[{x,y}]<<endl;
else {
int anss=0;
for(int ind:regions[x]) {
int pos1=upper_bound(indexx[y].begin(),indexx[y].end(),beg[ind])-indexx[y].begin();
int pos2=upper_bound(indexx[y].begin(),indexx[y].end(),en[ind])-indexx[y].begin();
anss+=pos2-pos1;
}
cout<<anss<<endl;
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |