Submission #854924

#TimeUsernameProblemLanguageResultExecution timeMemory
854924rxlfd314Regions (IOI09_regions)C++17
100 / 100
3523 ms127692 KiB
#include <bits/stdc++.h> using namespace std; constexpr int mxN = 200069, BSZ = 125; // wtf 125 works??? this is so cringe int N, R, Q, H[mxN], rgnOrd[mxN>>3], revOrd[mxN>>3]; vector<int> adj[mxN], rgnLst[mxN>>3], rgnLst2[mxN>>3]; int timer, tin[mxN], tout[mxN]; int tdall[BSZ][BSZ], td[mxN][BSZ]; void dfs(int f) { tin[f] = timer++; for (int i = 0; i < min(R, BSZ) && revOrd[H[f]] < BSZ; i++) { tdall[revOrd[H[f]]][i] += td[f][i]; } for (int nf : adj[f]) { for (int i = 0; i < min(R, BSZ); i++) { td[nf][i] = td[f][i] + (rgnOrd[i] == H[f]); } dfs(nf); } tout[f] = timer-1; } signed main() { scanf("%d%d%d%d", &N, &R, &Q, &H[0]); rgnLst[--H[0]].push_back(0); for (int i = 1, p; i < N; i++) { scanf("%d%d", &p, &H[i]); adj[--p].push_back(i); rgnLst[--H[i]].push_back(i); } iota(rgnOrd, rgnOrd+R, 0); sort(rgnOrd, rgnOrd+R, [&](const int &a, const int &b) { return rgnLst[a].size() > rgnLst[b].size(); }); for (int i = 0; i < R; i++) { revOrd[rgnOrd[i]] = i; } dfs(0); for (int i = 0; i < R; i++) { for (int j : rgnLst[i]) { rgnLst2[i].push_back(tin[j]); } sort(rgnLst2[i].begin(), rgnLst2[i].end()); } for (int a, b, ans; Q--; ) { scanf("%d%d", &a, &b); a--, b--; ans = 0; if (revOrd[a] < BSZ && revOrd[b] < BSZ) { ans = tdall[revOrd[b]][revOrd[a]]; } else if (revOrd[a] < BSZ) { for (int i : rgnLst[b]) { ans += td[i][revOrd[a]]; } } else { for (int i : rgnLst[a]) { ans += upper_bound(rgnLst2[b].begin(), rgnLst2[b].end(), tout[i]) - lower_bound(rgnLst2[b].begin(), rgnLst2[b].end(), tin[i]); } } printf("%d\n", ans); fflush(stdout); } }

Compilation message (stderr)

regions.cpp: In function 'int main()':
regions.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |  scanf("%d%d%d%d", &N, &R, &Q, &H[0]);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:28:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |   scanf("%d%d", &p, &H[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
regions.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...