Submission #1300761

#TimeUsernameProblemLanguageResultExecution timeMemory
1300761tolgaRegions (IOI09_regions)C++20
85 / 100
8058 ms20704 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 5 + 2e5; vector<int> edges[maxn]; int vert[maxn], tin[maxn], tout[maxn], timer = 0; void dfs(int u) { tin[u] = ++timer; for (int v : edges[u]) dfs(v); tout[u] = timer; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int n, r, q; cin >> n >> r >> q; vector<vector<int>> reg(r + 5); cin >> vert[1]; reg[vert[1]].push_back(1); for (int i = 2; i <= n; i++) { int par; cin >> par; edges[par].push_back(i); cin >> vert[i]; reg[vert[i]].push_back(i); } dfs(1); for (int i = 1; i <= r; i++) sort(reg[i].begin(), reg[i].end(), [&](int a, int b) { return tin[a] < tin[b]; }); while (q--) { int r1, r2; cin >> r1 >> r2; auto &v1 = reg[r1], &v2 = reg[r2]; ll ans = 0; static auto cmp1 = [&](int node, int val) { return tin[node] < val; }; static auto cmp2 = [&](int val, int node) { return val < tin[node]; }; for (auto &u : v1) { int left = lower_bound(v2.begin(), v2.end(), tin[u], cmp1) - v2.begin(); int right = upper_bound(v2.begin(), v2.end(), tout[u], cmp2) - v2.begin(); ans += right - left; } cout << ans << endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...