Submission #116393

#TimeUsernameProblemLanguageResultExecution timeMemory
116393kig9981Regions (IOI09_regions)C++17
30 / 100
8066 ms119772 KiB
#include <bits/stdc++.h> #ifdef NON_SUBMIT #define TEST(n) (n) #define tout cerr #else #define TEST(n) ((void)0) #define tout cin #endif using namespace std; const int rt=448; vector<int> adj[200000], R[25000], O; int node_cnt=-1, RV[200000], num[200000], fin[200000], sum[rt][200000], PS[rt][200000], tree[200001]; pair<long long,long long> ans[rt][25000]; void add_tree(int n, int v) { for(++n;n<=200000;n+=n&-n) tree[n]+=v; } int get_sum(int n) { int ret=0; for(++n;n;n-=n&-n) ret+=tree[n]; return ret; } int get_sum(int s, int e) { return get_sum(e)-get_sum(s-1); } void dfs(int c) { num[c]=++node_cnt; for(auto n: adj[c]) dfs(n); fin[c]=node_cnt; } void dfs2(int c, int idx) { if(RV[c]==O[idx]) { sum[idx][c]++; PS[idx][num[c]]++; } for(auto n: adj[c]) { sum[idx][n]=sum[idx][c]; dfs2(n,idx); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); TEST(freopen("input.txt","r",stdin)); TEST(freopen("output.txt","w",stdout)); TEST(freopen("debug.txt","w",stderr)); int N, M, Q, p; cin>>N>>M>>Q>>RV[0]; R[--RV[0]].push_back(0); for(int i=1;i<N;i++) { cin>>p>>RV[i]; adj[--p].push_back(i); R[--RV[i]].push_back(i); } dfs(0); for(int i=0;i<M;i++) if(R[i].size()>=rt) { O.push_back(i); dfs2(0,O.size()-1); for(int j=0;j<O.size()-1;j++) { for(auto r: R[i]) ans[O.size()-1][O[j]].first+=sum[j][r]; for(auto r: R[O[j]]) ans[j][O.back()].first+=sum[O.size()-1][r]; } } for(int i=0;i<O.size();i++) { for(int j=1;j<N;j++) PS[i][j]+=PS[i][j-1]; for(int j=0;j<M;j++) if(R[j].size()<rt) for(auto r: R[j]) { ans[i][j].first+=PS[i][fin[r]]-(num[r] ? PS[i][num[r]-1]:0); ans[i][j].second+=sum[i][r]; } } while(Q--) { int a, b; cin>>a>>b; --a; --b; if(R[a].size()>=rt) cout<<ans[lower_bound(O.begin(),O.end(),a)-O.begin()][b].first<<'\n'; else if(R[b].size()>=rt) cout<<ans[lower_bound(O.begin(),O.end(),b)-O.begin()][a].second<<'\n'; else { long long ans=0; for(auto r: R[b]) add_tree(num[r],1); for(auto r: R[a]) ans+=get_sum(num[r],fin[r]); for(auto r: R[b]) add_tree(num[r],-1); cout<<ans<<'\n'; } cout.flush(); } return 0; }

Compilation message (stderr)

regions.cpp: In function 'int main()':
regions.cpp:73:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<O.size()-1;j++) {
               ~^~~~~~~~~~~
regions.cpp:78:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<O.size();i++) {
              ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...