Submission #802046

#TimeUsernameProblemLanguageResultExecution timeMemory
802046mrwangRegions (IOI09_regions)C++14
65 / 100
3655 ms71340 KiB
#include<bits/stdc++.h> #define TASKNAME "codeforce" #define pb push_back #define pli pair<int,int> #define fi first #define se second #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); using namespace std; using ll=long long; const ll maxN=3e5+10; const ll inf=1e18; const ll mod=1e9+7; ll in[maxN],out[maxN],tg=0,h[maxN],cnt[maxN],ct[maxN]; ll dem1[450][25001],dem2[450][25001]; vector<ll>g[maxN]; void DFS(ll u) { in[u]=++tg; for(int v:g[u]) { DFS(v); } out[u]=tg; } ll k[maxN]; void dfs(ll u,ll c,ll i) { cnt[u]=k[u]==c; for(int v:g[u]) { h[v]=h[u]+(k[v]==c); dfs(v,c,i); cnt[u]+=cnt[v]; } dem1[i][k[u]]+=cnt[u]; dem2[i][k[u]]+=h[u]; } ll n,r,q; vector<ll>vv[25001],vec[25001]; ll pp[maxN]; void solve() { cin >> n >> r >> q; for(int i=1;i<=n;i++) { if(i!=1) { ll p; cin >> p; g[p].pb(i); } cin >> k[i]; ct[k[i]]++; vv[k[i]].pb(i); } ll sz=0; DFS(1); for(int i=1;i<=r;i++) { if(ct[i]*ct[i]>n) { ++sz; pp[i]=sz; dfs(1,i,sz); } } for(int i=1;i<=n;i++) { vec[k[i]].pb(in[i]); } for(int i=1;i<=r;i++) sort(vec[i].begin(),vec[i].end()); for(int i=1;i<=q;i++) { ll x,y; cin >> x >> y; if(ct[x]*ct[x]>n) { cout << dem2[pp[x]][y]<<endl; } else if(ct[y]*ct[y]>n) { cout << dem1[pp[y]][x]<<endl; } else { ll ans=0; for(auto zz:vv[x]) { ans+=upper_bound(vec[y].begin(),vec[y].end(),out[zz])-lower_bound(vec[y].begin(),vec[y].end(),in[zz]); } cout << ans<<endl; } } } int main() { fastio //freopen(TASKNAME".INP","r",stdin); //freopen(TASKNAME".OUT","w",stdout); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...