제출 #1300769

#제출 시각아이디문제언어결과실행 시간메모리
1300769tolgaRegions (IOI09_regions)C++20
85 / 100
8086 ms20612 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); for (int i = 1; i <= r; i++) std::sort(reg[i].begin(), reg[i].end(), [&](int a, int b) { return tin[a] < tin[b]; }); 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 (auto &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 << std::endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...