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 pb push_back
#define ll long long
const int mxN = 2e5 + 5000;
vector<ll> adj[mxN];
ll hd[mxN];
map<ll,vector<ll>> same;
vector<map<ll,ll>> seg(mxN*4);
ll st[mxN],en[mxN],lt[mxN],tin=-1;
void answer(ll ans){
cout.flush()<<ans<<'\n';
cout.flush();
}
void build(ll node,ll start,ll end){
if(start==end){
seg[node][hd[lt[start]]]=1;
return;
}
ll mid=start+(end-start)/2;
build(node*2+1,start,mid);
build(node*2+2,mid+1,end);
for(auto it:seg[node*2+1]) seg[node][it.first]=it.second;
for(auto it:seg[node*2+2]) seg[node][it.first]+=it.second;
}
void dfs(ll u=1,ll par=-1){
st[u]=++tin;
lt[tin]=u;
for(auto it:adj[u]){
if(it^par){
dfs(it,u);
}
}
en[u]=tin;
}
ll qry(ll node,ll start,ll end,ll l,ll r,ll x){
if(start>r||end<l) return 0;
if(start>=l&&end<=r) return seg[node][x];
ll mid=start+(end-start)/2;
return qry(node*2+1,start,mid,l,r,x)+qry(node*2+2,mid+1,end,l,r,x);
}
void Solve(){
ll n,k,q,r1,r2,ans=0,super;cin>>n>>k>>q;
cin>>hd[1];
same[hd[1]].pb(1);
for(ll i=2;i<=n;++i){
cin>>super>>hd[i];
adj[i].pb(super);
adj[super].pb(i);
same[hd[i]].pb(i);
}
dfs();
build(0,0,tin);
while(q--){
cin>>r1>>r2;
ans=0;
for(auto it:same[hd[r1]]){
ans+=qry(0,0,tin,st[it],en[it],r2);
}
answer(ans);
}
}
void testing(){
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
}
int main() {
//testing();
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
Solve();
return 0;
}
Compilation message (stderr)
regions.cpp: In function 'void testing()':
regions.cpp:79:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen("input.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
regions.cpp:80:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | freopen("output.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |