Submission #1270927

#TimeUsernameProblemLanguageResultExecution timeMemory
1270927thewizardmanRegions (IOI09_regions)C++20
100 / 100
3420 ms32864 KiB
#include <bits/stdc++.h> using namespace std; #define thres 500 int n, r, q, p, h[200001], r1, r2, timer, tin[200001], tout[200001], pre[500][25001], cnt[500]; vector<int> adj[200001]; vector<int> v1[200001], v2[25001], big; int comp(int x) { return lower_bound(big.begin(), big.end(), x) - big.begin(); } void dfs(int u, int p) { tin[u] = timer++; for (int i = 0; i < big.size(); i++) pre[i][h[u]] += cnt[i]; if (v1[h[u]].size() >= thres) cnt[comp(h[u])]++; for (auto v: adj[u]) if (v != p) dfs(v, u); if (v1[h[u]].size() >= thres) cnt[comp(h[u])]--; tout[u] = timer; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> r >> q; cin >> h[1]; v1[h[1]].push_back(1); for (int i = 2; i <= n; i++) { cin >> p >> h[i]; adj[p].push_back(i); v1[h[i]].push_back(i); } for (int i = 1; i <= r; i++) if (v1[i].size() >= thres) big.push_back(i); dfs(1, 0); for (int i = 1; i <= n; i++) v2[h[i]].push_back(tin[i]); for (int i = 1; i <= r; i++) sort(v2[i].begin(), v2[i].end()); for (int i = 0; i < q; i++) { int ans = 0; cin >> r1 >> r2; if (v1[r1].size() < thres) { for (auto e: v1[r1]) ans += lower_bound(v2[r2].begin(), v2[r2].end(), tout[e]) - lower_bound(v2[r2].begin(), v2[r2].end(), tin[e]); cout << ans << endl; } else { cout << pre[comp(r1)][r2] << endl; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...