Submission #931678

#TimeUsernameProblemLanguageResultExecution timeMemory
931678LOLOLORegions (IOI09_regions)C++17
100 / 100
3360 ms72708 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define f first #define s second #define pb push_back #define ep emplace #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin()) #define mem(f,x) memset(f , x , sizeof(f)) #define sz(x) (int)(x).size() #define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b)) #define mxx *max_element #define mnn *min_element #define cntbit(x) __builtin_popcountll(x) #define len(x) (int)(x.length()) const int N = 2e5 + 100; const int lim = 500; vector <int> lst[N], ed[N], all, e[N]; map <int, int> ans[N]; int c[N], in[N], ou[N], timer = 1, cnt[N]; void dfs(int u) { cnt[c[u]]++; in[u] = ++timer; for (auto x : ed[u]) { dfs(x); } cnt[c[u]]--; for (auto x : all) { ans[x][c[u]] += cnt[x]; } ou[u] = timer; } int main() { int n, r, q; cin >> n >> r >> q; cin >> c[1]; for (int i = 2; i <= n; i++) { int x; cin >> x; cin >> c[i]; ed[x].pb(i); } for (int i = 1; i <= n; i++) { lst[c[i]].pb(i); } for (int i = 1; i <= r; i++) { if (sz(lst[i]) >= lim) all.pb(i); } dfs(1); for (int i = 1; i <= n; i++) { e[c[i]].pb(in[i]); } for (int i = 1; i <= r; i++) { sort(all(e[i])); } for (int i = 1; i <= q; i++) { int r1, r2; cin >> r1 >> r2; if (sz(lst[r1]) >= lim) { cout << ans[r1][r2] << '\n'; } else { int cnt = 0; for (auto x : lst[r1]) { int l = lower_bound(all(e[r2]), in[x]) - e[r2].begin(), r = upper_bound(all(e[r2]), ou[x]) - e[r2].begin() - 1; cnt += (r - l + 1); } cout << cnt << '\n'; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...