Submission #524199

#TimeUsernameProblemLanguageResultExecution timeMemory
524199keertanRegions (IOI09_regions)C++17
100 / 100
4660 ms118412 KiB
// Check sqrt decomposition CPH to understand this solution well // Basically combined two solutions, one works well for regions with smaller size // The other works well for regions with larger size // Set the line between larger and smaller as sqrt(n) or so and apply the appropriate algo for best results #include <bits/stdc++.h> using namespace std; const int maxn = 2e5+10; bool vis[25001]; int n,r,q,cnt=1, a[maxn], st[maxn], en[maxn], B[maxn]; unordered_map<int,int> calc[25001]; vector<int> adj[maxn], v[25001]; void dfs(int s, int p = -1) { st[s] = cnt++; for(auto u : adj[s]) if(u!=p) dfs(u,s); en[s] = cnt-1; } int32_t main() { cin >> n >> r >> q >> a[1]; for(int i = 2; i <= n; i++) { int x; cin >> x >> a[i]; adj[x].push_back(i); } int K = sqrt(n); dfs(1); for(int i = 1; i <= n; i++) B[st[i]]=i; for(int i = 1; i <= n; i++) v[a[B[i]]].push_back(st[B[i]]); for(int i = 1; i <= n; i++) { if(vis[a[B[i]]]) continue; if(v[a[B[i]]].size()<=K) continue; vis[a[B[i]]]=true; int pref[n+2]{0}; for(auto u : v[a[B[i]]]) { int l = st[B[u]], r = en[B[u]]; pref[l]++, pref[r+1]--; } for(int j = 1; j <= n; j++) pref[j]+=pref[j-1], calc[a[B[j]]][a[B[i]]]+=pref[j]; } while(q--) { int a, b, ans = 0; cin >> a >> b; if(v[a].size()<=K){ for(auto u : v[a]){ int l = st[B[u]], r = en[B[u]]; auto itr = upper_bound(v[b].begin(), v[b].end(), r)-v[b].begin(); auto itr2 = upper_bound(v[b].begin(), v[b].end(), l)-v[b].begin(); if(itr) itr--, ans+=max((int)(itr-itr2)+1, 0); } } else ans = calc[b][a]; cout << ans << endl; } }

Compilation message (stderr)

regions.cpp: In function 'int32_t main()':
regions.cpp:34:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   34 |         if(v[a[B[i]]].size()<=K) continue;
      |            ~~~~~~~~~~~~~~~~~^~~
regions.cpp:48:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   48 |         if(v[a].size()<=K){
      |            ~~~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...