제출 #1013422

#제출 시각아이디문제언어결과실행 시간메모리
1013422vjudge1Regions (IOI09_regions)C++17
100 / 100
3053 ms39956 KiB
#include<bits/stdc++.h> using namespace std; #ifdef ONPC #include"debug.h" #else #define debug(...) 42 #endif #define endl '\n' #define ll long long #define pii pair<int,int> #define F first #define S second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } const int mod = 1e9 + 7; const int MAXN = 2e5 + 15; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const int C = 500; int frq[MAXN], col[MAXN]; int n, r, q; vector<int> deco; ll ans[503][25001]; pii upper; vector<int> adj[MAXN]; int dt[MAXN], ft[MAXN], gtime = 1; vector<int> dis_times[MAXN]; vector<int> nodes[MAXN]; void pre(int node){ dis_times[col[node]].pb(gtime); nodes[col[node]].pb(node); dt[node] = gtime++; for (auto to : adj[node]) pre(to); ft[node] = gtime - 1; } void dfs(int node, int cnt){ ans[upper.S][col[node]] += cnt; if (col[node] == upper.F) cnt++; for (auto to : adj[node]){ dfs(to, cnt); } } ll brut(int e1, int e2){ ll res = 0; for (auto node : nodes[e1]){ res += upper_bound(all(dis_times[e2]), ft[node]) - lower_bound(all(dis_times[e2]), dt[node]); } return res; } int main(){ //ios_base::sync_with_stdio(0); //cin.tie(0); cin >> n >> r >> q >> col[1]; for (int i = 2; i <= n; i++){ int p, c; cin >> p >> c; adj[p].pb(i); col[i] = c; frq[c]++; } pre(1); for (int i = 0; i <= 25000; i++){ if (frq[i] >= C){ deco.pb(i); } } //for (int i = 1; i <= r; i++){ //upper = {i, i}; //dfs(1, 0); //} for (int i = 0; i < sz(deco); i++){ upper = {deco[i], i}; dfs(1, 0); } while (q--){ int e1, e2; cin >> e1 >> e2; if (frq[e1] >= C){ cout << ans[lower_bound(all(deco), e1) - deco.begin()][e2] << endl; } else { cout << brut(e1, e2) << endl; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...