이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 d[sz];
ll st[sz],en[sz];
ll ans[sz];
ll a[sz];
vector<ll>col[25001];
vector<ll>to[25001];
ll up[17][sz];
ll dp[sz];
vector<ll>adj[sz];
const int mod=1e9+7;
void dfs(int u,int p){
col[a[u]].push_back(timer);
st[u]=timer++;
for(auto v:adj[u]){
if(v==p) continue;
dfs(v,u);
} en[u]=timer-1;
}
int main(){
cin>>n>>r>>q;
cin>>a[1];
to[a[1]].push_back(1);
for(int i=2;i<=n;i++){
ll u;cin>>u>>a[i];
adj[u].push_back(i);
to[a[i]].push_back(i);
} dfs(1,0);
for(int i=0;i<q;i++){
ll u,w,res;cin>>u>>w;
const auto &v=col[w];res=0;
for(auto p:to[u]){
int x=upper_bound(v.begin(),v.end(),en[p])-lower_bound(v.begin(),v.end(),st[p]);
res+=x;
} cout<<res<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |