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 ll int
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
if(x<y)
{
x=y;
return 1;
}
return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
if(x>y)
{
x=y;
return 1;
}
return 0;
}
void online()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.inp", "r", stdin);
freopen("output.out","w", stdout);
#else
#endif
}
static const ll N=2e5+10,Block=450;
ll in[N],out[N],Psol[Block+10][N],Prep[N],e[N],n,r,q,cnt,Pcnt;
vector<ll> gr[N],Reg[N],Member[N];
void dfs(ll u)
{
in[u]=++cnt;
Member[e[u]].push_back(in[u]);
for(auto v:gr[u]) dfs(v);;
out[u]=cnt;
}
void dfs1(ll u,ll head,ll crr)
{
if(e[u]==head) crr++;
Psol[Pcnt][e[u]]+=crr;
for(auto v:gr[u]) dfs1(v,head,crr);
}
int main()
{
// online();
cin>>n>>r>>q;
cin>>e[1];
Reg[e[1]].push_back(1);
for(int i=2;i<=n;i++)
{
ll v,c;
cin>>v>>c;
gr[v].push_back(i);
e[i]=c;
Reg[c].push_back(i);
}
dfs(1);
for(int i=1;i<=r;i++)
{
if(Reg[i].size()<Block) continue;
Prep[i]=++Pcnt;
dfs1(1,i,0);
}
// for(int i=1;i<=r;i++)
// {
// for(auto v:Member[i]) cout<<v<<' ';
// cout<<'\n';
// }
while(q--)
{
ll a,b;
cin>>a>>b;
if(Prep[a]) cout<<Psol[Prep[a]][b]<<endl;
else
{
ll res=0;
for(auto v:Reg[a])
{
res+=lower_bound(Member[b].begin(),Member[b].end(),out[v]+1)-Member[b].begin();
res-=lower_bound(Member[b].begin(),Member[b].end(),in[v]) -Member[b].begin();
}
cout<<res<<endl;
}
}
}
Compilation message (stderr)
regions.cpp: In function 'void online()':
regions.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen("output.out","w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |