제출 #1213525

#제출 시각아이디문제언어결과실행 시간메모리
1213525vaneaRegions (IOI09_regions)C++20
55 / 100
8095 ms25504 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mxN = 2e5+10; const int mxR = 3e4+10; int reg[mxN], bit[mxN]; vector<int> adj[mxN], emp[mxR]; int tin[mxN], tout[mxN], timer = 0; void dfs(int node, int p) { tin[node] = ++timer; for(auto it : adj[node]) { if(it == p) continue; dfs(it, node); } tout[node] = timer; } void upd(int k, int x) { for(k; k < mxN; k += ((k) & (-k))) { bit[k] += x; } } int qry(int k) { int ans = 0; for(; k >= 1; k -= ((k) & (-k))) { ans += bit[k]; } return ans; } int main() { int n, r, q; cin >> n >> r >> q; cin >> reg[1]; emp[reg[1]].push_back(1); for(int i = 2; i <= n; i++) { int p; cin >> p >> reg[i]; emp[reg[i]].push_back(i); adj[p].push_back(i); adj[i].push_back(p); } dfs(1, 0); while(q--) { int r1, r2; cin >> r1 >> r2; int ans = 0; for(auto it : emp[r2]) { upd(tin[it], 1); } for(auto it : emp[r1]) { ans += qry(tout[it])-qry(tin[it]-1); } for(auto it : emp[r2]) { upd(tin[it], -1); } cout << ans << '\n'; cout.flush(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...