Submission #647348

#TimeUsernameProblemLanguageResultExecution timeMemory
647348KarukRegions (IOI09_regions)C++14
100 / 100
4999 ms106688 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...