Submission #1125693

#TimeUsernameProblemLanguageResultExecution timeMemory
1125693chaoslongRegions (IOI09_regions)C++20
100 / 100
3339 ms36632 KiB
// Calm down. // Think three times, code twice. #include "bits/stdc++.h" #define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a) #define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);) #define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a) #define st first #define nd second #define ll long long #define ull unsigned long long #define pii pair <int,int> #define pll pair <ll,ll> #define piii pair <int,pii> #define vi vector <int> #define pb push_back #define mp make_pair #define all(x) begin(x),end(x) #define mask(i) (1LL << (i)) #define bit(x, i) (((x) >> (i)) & 1) #define bp __builtin_popcountll #define file "test" using namespace std; const int N = 2e5 + 5; const int mod = 1e9 + 7; // 998244353 const ll oo = 1e18; int n, r, q, tin[N], tout[N], pos[N], timer = 1, h[N], prec[450][N], id[N]; vector<int> a[N]; vector<int> comp[N]; void dfs(int u, int pre) { tin[u] = timer; pos[timer++] = u; comp[h[u]].pb(tin[u]); for(int v: a[u]) { if(v == pre) continue; dfs(v, u); } tout[u] = timer - 1; } void precal(int u, int pre, int cur_r, int dem_cur) { if(h[u] == cur_r) dem_cur++; for(int v: a[u]) { if(v == pre) continue; prec[id[cur_r]][h[v]] += dem_cur; // cout << cur_r << " " << u << " " << v << " " << prec[id[cur_r]][h[v]] << "\n"; precal(v, u, cur_r, dem_cur); } } void to_nho_cau() { cin >> n >> r >> q; forr(i, 1, n) { if(i == 1) { int x; cin >> x; h[i] = x; } else { int x, y; cin >> x >> y; a[x].pb(i); h[i] = y; } } dfs(1, 1); int block = (int)(sqrt(n)); int dem = 1; forr(i, 1, r) { if(comp[i].size() > block) { id[i] = dem++; precal(1, 1, i, 0); } } while(q--) { int x, y; cin >> x >> y; if(id[x]) { cout << prec[id[x]][y] << endl; } else { int ans = 0; for(int v: comp[x]) { int l = lower_bound(all(comp[y]), v) - comp[y].begin(); int r = upper_bound(all(comp[y]), tout[pos[v]]) - comp[y].begin(); r--; if(l == n+1 || l > r) continue; ans += (r - l + 1); } cout << ans << endl; } } } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0); #ifdef LOCAL freopen(file".inp","r",stdin); freopen(file".out","w",stdout); #endif int t = 1; //cin >> t; while(t--) to_nho_cau(); } /* 1.self check: 2.long long 3.size of array 4.code for testing 5.initializing 6.modulo number */ /** ∧__∧ (`•ω• )づ__∧ (つ  /( •ω•。) しーJ (nnノ) pat pat **/ /** /\_/\ * (= ._.) * / >☕ \>💻 **/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...