Submission #1300772

#TimeUsernameProblemLanguageResultExecution timeMemory
1300772tolgaRegions (IOI09_regions)C++20
0 / 100
52 ms20584 KiB
#include <iostream> #include <vector> #include <algorithm> typedef long long ll; const int maxn = 5 + 2e5, maxr = 25005; std::vector<int> edges[maxn], reg[maxr]; 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() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr), std::cout.tie(nullptr); int n, r, q; std::cin >> n >> r >> q; std::cin >> vert[1]; reg[vert[1]].push_back(1); for (int i = 2; i <= n; i++) { int par; std::cin >> par; edges[par].push_back(i); std::cin >> vert[i]; reg[vert[i]].push_back(i); } dfs(1); auto cmp = [](int &a, int &b) { return tin[a] < tin[b]; }; for (int i = 1; i <= r; i++) std::sort(reg[i].begin(), reg[i].end(), cmp); auto cmp1 = [](int &node, int val) { return tin[node] < val; }; auto cmp2 = [](int val, int &node) { return val < tin[node]; }; while (q--) { int r1, r2; std::cin >> r1 >> r2; auto &v1 = reg[r1], &v2 = reg[r2]; ll ans = 0; for (int &u : v1) { int left = std::lower_bound(v2.begin(), v2.end(), tin[u], cmp1) - v2.begin(); int right = std::upper_bound(v2.begin(), v2.end(), tout[u], cmp2) - v2.begin(); ans += right - left; } std::cout << ans << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...