#include<bits/stdc++.h>
using namespace std;
struct mergeSortTree{
int N,n;
vector<vector<int>>tree;
void build(int v,int l,int r,vector<int>&a){
if(l>=n) return;
if(r-l==1){
tree[v]={a[l]};
return;
}
int mid=(l+r)/2;
build(v<<1,l,mid,a);
build(v<<1|1,mid,r,a);
merge(tree[v<<1].begin(),tree[v<<1].end(),tree[v<<1|1].begin(),tree[v<<1|1].end(),back_inserter(tree[v]));
}
mergeSortTree(vector<int>&a){
n=(int)a.size();
N=1;
while(N<n) N<<=1;
tree.resize(2*N);
build(1,0,N,a);
}
// void update(int v,int l,int r,int id,int x){
// cout<<v;
// if(r-l==1){
// tree[v]+=x;
// return;
// }
// int mid=(l+r)/2;
// if(mid>id) update(v<<1,l,mid,id,x);
// else update(v<<1|1,mid,r,id,x);
// tree[v]=tree[v<<1]+tree[v<<1|1];
// }
// void update(int id,int x){
// return update(1,0,N,id,x);
// }
int get(int v,int l,int r,int ql,int qr,int x){
if(ql>=r || l>=qr) return 0;
if(l>=ql && qr>=r) return lower_bound(tree[v].begin(),tree[v].end(),x+1)-lower_bound(tree[v].begin(),tree[v].end(),x);
int mid=(l+r)/2;
return get(v<<1,l,mid,ql,qr,x)+get(v<<1|1,mid,r,ql,qr,x);
}
int get(int l,int r,int x){
return get(1,0,N,l,r,x);
}
};
int n,r,q,timer = 0;
vector<int>h,tin,tout,b;
vector<vector<int>>v,a,dp;
void dfs(int x){
tin[x]=timer++;
b.push_back(h[x]);
for(int z:v[x]){
dfs(z);
}
tout[x]=timer;
}
signed main(){
cin>>n>>r>>q;
h.resize(n+1);
a.resize(r+1);
cin>>h[1];
v.assign(n+1,vector<int>(0));
// dp.assign(r+1,vector<int>(r+1,-1));
for(int i=2,x;i<=n;i++){
cin>>x>>h[i];
v[x].push_back(i);
}
for(int i=1;i<=n;i++){
a[h[i]].push_back(i);
}
tin.resize(n+1);
tout.resize(n+1);
dfs(1);
mergeSortTree S(b);
while(q--){
int r1,r2;
cin>>r1>>r2;
int ans=0;
for(int x:a[r1]){
ans+=S.get(tin[x],tout[x],r2);
}
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
13 ms |
600 KB |
Output is correct |
7 |
Correct |
23 ms |
596 KB |
Output is correct |
8 |
Correct |
26 ms |
856 KB |
Output is correct |
9 |
Correct |
71 ms |
1908 KB |
Output is correct |
10 |
Correct |
87 ms |
3064 KB |
Output is correct |
11 |
Correct |
212 ms |
4004 KB |
Output is correct |
12 |
Correct |
356 ms |
5980 KB |
Output is correct |
13 |
Correct |
265 ms |
6480 KB |
Output is correct |
14 |
Correct |
612 ms |
7588 KB |
Output is correct |
15 |
Correct |
1631 ms |
12500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
8087 ms |
21728 KB |
Time limit exceeded |
2 |
Execution timed out |
8026 ms |
21448 KB |
Time limit exceeded |
3 |
Execution timed out |
8086 ms |
25288 KB |
Time limit exceeded |
4 |
Correct |
682 ms |
7764 KB |
Output is correct |
5 |
Correct |
1080 ms |
11724 KB |
Output is correct |
6 |
Correct |
4863 ms |
13520 KB |
Output is correct |
7 |
Correct |
7491 ms |
20680 KB |
Output is correct |
8 |
Execution timed out |
8035 ms |
28896 KB |
Time limit exceeded |
9 |
Execution timed out |
8045 ms |
44228 KB |
Time limit exceeded |
10 |
Execution timed out |
8016 ms |
51140 KB |
Time limit exceeded |
11 |
Execution timed out |
8035 ms |
51616 KB |
Time limit exceeded |
12 |
Execution timed out |
8084 ms |
51772 KB |
Time limit exceeded |
13 |
Execution timed out |
8042 ms |
51756 KB |
Time limit exceeded |
14 |
Execution timed out |
8100 ms |
52932 KB |
Time limit exceeded |
15 |
Execution timed out |
8042 ms |
56260 KB |
Time limit exceeded |
16 |
Execution timed out |
8050 ms |
60104 KB |
Time limit exceeded |
17 |
Execution timed out |
8048 ms |
58960 KB |
Time limit exceeded |