Submission #413440

#TimeUsernameProblemLanguageResultExecution timeMemory
413440JerryLiu06Regions (IOI09_regions)C++17
0 / 100
3324 ms31984 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = long double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define mp make_pair #define f first #define s second #define sz(x) (int)(x).size() #define bg(x) begin(x) #define all(x) bg(x), end(x) #define sor(x) sort(all(x)) #define ft front() #define bk back() #define pb push_back #define pf push_front #define lb lower_bound #define ub upper_bound #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--) #define R0F(i, a) ROF(i, 0, a) #define EACH(a, x) for (auto& a : x) const int MOD = 1e9 + 7; const int MXN = 2e5 + 10; const int MXR = 25010; const ll INF = 1e18; const int BLOCK = 450; int N, R, Q; vi adj[MXN]; int L[MXN]; // L[i] = Region of i int timer = 1, in[MXN], out[MXN]; vi S1[MXR]; // Sorted list that divide into intervals vi S2[MXR]; // Sorted list of all int active[MXR]; vi large; int up[BLOCK][MXR], down[BLOCK][MXR]; int ind[MXR]; // Preorder Traversal ~ O(N) void DFS(int X, int P) { in[X] = timer++; active[L[X]]++; S2[L[X]].pb(X); EACH(Y, adj[X]) if (Y != P) DFS(Y, X); out[X] = timer - 1; active[L[X]]--; if (!active[L[X]]) S1[L[X]].pb(X); } // Get the answer when queried (A, B) ~ O(N) int genAns(int A, int B) { int ans = 0; int L = 0, R = 0; int last = 0; while (L < sz(S1[A]) || R < sz(S2[B])) { if (L < sz(S1[A]) && (R >= sz(S2[B]) || in[S1[A][L]] < in[S2[B][R]])) last = out[S1[A][L++]]; else { if (out[S2[B][R]] <= last) ans++; R++; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> R >> Q >> L[1]; FOR(i, 2, N + 1) { int A; cin >> A >> L[i]; adj[A].pb(i), adj[i].pb(A); } DFS(1, 0); int cur = 0; FOR(i, 1, R + 1) if (sz(S2[i]) >= BLOCK) large.pb(i), ind[i] = cur++; F0R(i, sz(large)) { FOR(j, 1, R + 1) { up[i][j] = genAns(large[i], j); down[i][j] = genAns(j, large[i]); } } F0R(i, Q) { int A, B; cin >> A >> B; if (sz(S2[A]) >= BLOCK) cout << up[ind[L[A]]][B] << "\n"; else if (sz(S2[B]) >= BLOCK) cout << down[ind[L[A]]][B] << "\n"; else cout << genAns(A, B) << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...