This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, r, q;
map <int, int> mp[200005];
vector <int> adj[200005];
int A[200005];
map <int, int> fin[25005];
int S[200005], E[200005];
const int sz = 500;
int big[25005];
vector <int> stuf[25005], stuf2[25005];
void dfs(int x, int par){
mp[x][A[x]]++;
for(auto i : adj[x]){
if(i == par)continue;
dfs(i, x);
if(mp[i].size() > mp[x].size())swap(mp[x], mp[i]);
for(auto [j, k] : mp[i])mp[x][j] += k;
}
if(big[A[x]] >= 1000)for(auto [i, j] : mp[x])fin[A[x]][i] += j;
}
int cnt = 1;
void dfs2(int x, int par){
S[x] = cnt++;
for(auto i : adj[x]){
if(i == par)continue;
dfs2(i, x);
}
E[x] = cnt - 1;
}
int find(int idx, int l, int r){
return upper_bound(stuf2[idx].begin(), stuf2[idx].end(), r) - lower_bound(stuf2[idx].begin(), stuf2[idx].end(), l);
}
void solve(){
cin >> n >> r >> q;
cin >> A[1];
for(int i=2;i<=n;i++){
int x; cin >> x >> A[i];
adj[x].push_back(i);
big[A[i]]++;
}
if(1){
dfs(1, -1);
dfs2(1, -1);
for(int i=1;i<=n;i++)stuf[A[i]].push_back(i), stuf2[A[i]].push_back(S[i]);
for(int i=1;i<=r;i++)sort(stuf2[i].begin(), stuf2[i].end());
int ans =0 ;
while(q--){
ans = 0;
int x, y; cin >> x >> y;
if(big[x] >= 1000){
cout << fin[x][y] << endl;
continue;
}
for(auto i : stuf[x]){
ans += find(y, S[i], E[i]);
}
cout << ans << endl;
}
return;
}
dfs(1, -1);
while(q--){
int x, y; cin >> x >> y;
cout << fin[x][y] << endl;
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
regions.cpp:82:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
82 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |