Submission #793499

#TimeUsernameProblemLanguageResultExecution timeMemory
793499phoebeRegions (IOI09_regions)C++17
100 / 100
4872 ms79304 KiB
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("03") // #define int long long #define ll long long #define pii pair<int, int> #define F first #define S second #define PB push_back #define ALL(x) x.begin(), x.end() #define FOR(i, n) for (int i = 0; i < n; i++) #define NYOOM ios::sync_with_stdio(0); cin.tie(0); // #define endl '\n' const int INF = 1e9 + 7; const ll LLINF = 1ll<<60; const int maxn = 2e5 + 10, block = 400, maxr = 25005; int n, r, q, h[maxn], arrive[maxn], leave[maxn], cnt[maxn / block][maxr] = {0}, // cnt[i][j] = # region i as manager of j timer = 0, cur[block] = {0}, get_idx[maxn], idx = 0; vector<int> adj[maxn], bruh[maxr], // bruh[i] = vector of arrive time of region i region[maxr]; // region[i] = vector of all nodes in region i void dfs(int v){ // O(n sqrt n) arrive[v] = timer++; bruh[h[v]].PB(arrive[v]); FOR(i, maxn / block) cnt[i][h[v]] += cur[i]; if (get_idx[h[v]] != -1) cur[get_idx[h[v]]]++; for (auto u : adj[v]){ dfs(u); } if (get_idx[h[v]] != -1) cur[get_idx[h[v]]]--; leave[v] = timer++; } signed main(){ NYOOM; fill(get_idx, get_idx + maxn, -1); cin >> n >> r >> q; cin >> h[1]; region[h[1]].PB(1); for (int i = 2; i <= n; i++){ int s; cin >> s >> h[i]; adj[s].PB(i); region[h[i]].PB(i); } for (int i = 1; i <= r; i++){ if (region[i].size() >= block) get_idx[i] = idx++; } dfs(1); FOR(i, q){ int r1, r2; cin >> r1 >> r2; if (get_idx[r1] != -1){ cout << cnt[get_idx[r1]][r2] << endl; continue; } int re = 0; for (auto v : region[r1]){ // o(sqrt n log n) int l = lower_bound(ALL(bruh[r2]), arrive[v]) - bruh[r2].begin(); int r = lower_bound(ALL(bruh[r2]), leave[v]) - bruh[r2].begin(); // cout << l << ' ' << r << endl; re += r - l; } cout << re << endl; } // total = o(n sqrt n + q sqrt n log n) }

Compilation message (stderr)

regions.cpp: In function 'void dfs(int)':
regions.cpp:29:47: warning: iteration 400 invokes undefined behavior [-Waggressive-loop-optimizations]
   29 |     FOR(i, maxn / block) cnt[i][h[v]] += cur[i];
      |                                          ~~~~~^
regions.cpp:13:37: note: within this loop
   13 | #define FOR(i, n) for (int i = 0; i < n; i++)
......
   29 |     FOR(i, maxn / block) cnt[i][h[v]] += cur[i];
      |         ~~~~~~~~~~~~~~~              
regions.cpp:29:5: note: in expansion of macro 'FOR'
   29 |     FOR(i, maxn / block) cnt[i][h[v]] += cur[i];
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...