Submission #321092

#TimeUsernameProblemLanguageResultExecution timeMemory
321092super_j6Regions (IOI09_regions)C++14
35 / 100
7187 ms40548 KiB
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <vector> using namespace std; //#define endl '\n' #define ll long long #define pi pair<int, int> #define f first #define s second const int mxn = 200001, m = 450; int n, k, q, t; int a[mxn], b[mxn], p[mxn], dp[mxn], l[mxn], r[mxn], bit[mxn]; ll f1[m][mxn], f2[m][mxn]; vector<int> g[mxn], w[mxn]; void add(int x, int v){ for(x++; x <= n; x += x & -x) bit[x] += v; } int qry(int x){ int ret = 0; for(x++; x; x -= x & -x) ret += bit[x]; return ret; } int dfs(int c){ r[c] = l[c]; for(int i : g[c]) if(i != p[c]) l[i] = r[c] + 1, r[c] = dfs(i); return r[c]; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> q; for(int i = 0; i < n; i++){ if(i) cin >> p[i]; cin >> a[i]; g[--p[i]].push_back(i), w[--a[i]].push_back(i); } for(int i = 0; i < k; i++){ if(w[i].size() > m){ b[i] = t++; for(int j = 0; j < n; j++){ f2[i][j] += (dp[j] = a[j] == i + (~p[j] ? dp[p[j]] : 0)); } memset(dp, 0, sizeof(dp)); for(int j = n - 1; ~j; j--){ f1[i][j] += (dp[j] += a[j] == i); if(~p[j]) dp[p[j]] += dp[j]; } }else{ b[i] = -1; } } for(int i = 0; i < k; i++){ } dfs(0); while(q--){ int x, y; cin >> x >> y; x--, y--; if(~b[x]){ cout << f1[b[x]][y] << endl; }else if(~b[y]){ cout << f2[b[y]][x] << endl; }else{ ll ret = 0; for(int i : w[y]) add(l[i], 1); for(int i : w[x]) ret += qry(r[i]) - qry(l[i] - 1); for(int i : w[y]) add(l[i], -1); cout << ret << endl; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...