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;
typedef long long ll;
typedef long double ld;
const int sz=2e5+1;
ll timer=1;
int n,q,m,k,r;
ll st[sz],en[sz];
ll a[sz];
vector<array<ll,2>>to[25001];
ll up[17][sz];
ll par[sz];
vector<ll>adj[sz];
struct compare {
bool operator()(const array<ll,2>& value,
const int& key)
{
return (value[0] < key);
}
bool operator()(const int& key,
const array<ll,2>& value)
{
return (key < value[0]);
}
};
const int mod=1e9+7;
void dfs(int u){
to[a[u]].push_back({timer,u});
st[u]=timer++;
for(auto v:adj[u]){
if(v==par[u]) continue;
dfs(v);
} en[u]=timer-1;
}
int main(){
cin>>n>>r>>q;
cin>>a[1];
for(int i=2;i<=n;i++){
cin>>par[i]>>a[i];
adj[par[i]].push_back(i);
}
dfs(1);
for(int i=0;i<q;i++){
ll u,w,res;cin>>u>>w;
const auto &p=to[w];res=0;
if(to[w].size()>0 and to[u].size()>0){
int d=to[u][to[u].size()-1][1];
int e=to[w][0][1];
if(st[e]>=1+en[d]){
cout<<0<<endl;
continue;
}
d=to[w][to[w].size()-1][1];
e=to[u][0][1];
if(st[e]>=1+en[d]){
cout<<0<<endl;
continue;
}
}
for(auto v:to[u]){
int x=upper_bound(p.begin(),p.end(),en[v[1]],compare())-lower_bound(p.begin(),p.end(),st[v[1]],compare());
res+=x;
} cout<<res<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |