#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
const int mxN = 2e5 + 5000;
ll st[mxN],en[mxN],hd[mxN],tin=0;
set<ll> adj[mxN];
map<ll,vector<ll>> same;
map<ll,ll> seg[mxN*4];
void answer(ll ans){
cout.flush()<<ans<<'\n';
cout.flush();
}
void dfs(ll u=0,ll par=-1){
st[u]=++tin;
for(auto it:adj[u]){
if(it^par){
dfs(it,u);
}
}
en[u]=tin;
}
void upd(ll node,ll start,ll end,ll idx,ll x){
++seg[node][x];
if(start==end) return;
ll mid=start+(end-start)/2;
if(idx<=mid) upd(node*2+1,start,mid,idx,x);
else upd(node*2+2,mid+1,end,idx,x);
}
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 testing(){
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
}
signed main() {
//testing();
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll n,k,q,r1,r2,ans=0,super;cin>>n>>k>>q;
cin>>hd[0];
same[hd[0]].pb(0);
for(ll i=1;i<n;++i){
cin>>super>>hd[i];--super;
adj[i].insert(super);
adj[super].insert(i);
same[hd[i]].pb(i);
}
dfs();
for(ll i=1;i<=n;++i){
upd(0,1,n,st[i],hd[i]);
}
while(q--){
cin>>r1>>r2;
ans=0;
for(auto it:same[hd[r1]]){
ans+=qry(0,1,n,st[it],en[it],r2);
}
answer(ans);
}
return 0;
}
Compilation message
regions.cpp: In function 'void testing()':
regions.cpp:49:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("input.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
regions.cpp:50:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen("output.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
48472 KB |
Output isn't correct |
2 |
Incorrect |
21 ms |
48472 KB |
Output isn't correct |
3 |
Incorrect |
22 ms |
48472 KB |
Output isn't correct |
4 |
Incorrect |
26 ms |
48772 KB |
Output isn't correct |
5 |
Incorrect |
30 ms |
49332 KB |
Output isn't correct |
6 |
Incorrect |
39 ms |
50592 KB |
Output isn't correct |
7 |
Incorrect |
64 ms |
53328 KB |
Output isn't correct |
8 |
Incorrect |
84 ms |
55636 KB |
Output isn't correct |
9 |
Incorrect |
185 ms |
66988 KB |
Output isn't correct |
10 |
Incorrect |
339 ms |
88144 KB |
Output isn't correct |
11 |
Runtime error |
976 ms |
131072 KB |
Execution killed with signal 9 |
12 |
Runtime error |
993 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Runtime error |
629 ms |
131072 KB |
Execution killed with signal 9 |
14 |
Runtime error |
632 ms |
131072 KB |
Execution killed with signal 9 |
15 |
Runtime error |
570 ms |
131072 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
414 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Runtime error |
430 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Runtime error |
309 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Runtime error |
579 ms |
131072 KB |
Execution killed with signal 9 |
5 |
Runtime error |
608 ms |
131072 KB |
Execution killed with signal 9 |
6 |
Runtime error |
521 ms |
131072 KB |
Execution killed with signal 9 |
7 |
Runtime error |
351 ms |
131072 KB |
Execution killed with signal 9 |
8 |
Runtime error |
251 ms |
131072 KB |
Execution killed with signal 9 |
9 |
Runtime error |
308 ms |
131072 KB |
Execution killed with signal 9 |
10 |
Runtime error |
264 ms |
131072 KB |
Execution killed with signal 9 |
11 |
Runtime error |
296 ms |
131072 KB |
Execution killed with signal 9 |
12 |
Runtime error |
342 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Runtime error |
292 ms |
131072 KB |
Execution killed with signal 9 |
14 |
Runtime error |
311 ms |
131072 KB |
Execution killed with signal 9 |
15 |
Runtime error |
279 ms |
131072 KB |
Execution killed with signal 9 |
16 |
Runtime error |
258 ms |
131072 KB |
Execution killed with signal 9 |
17 |
Runtime error |
249 ms |
131072 KB |
Execution killed with signal 9 |