#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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
9 ms |
14396 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
10 ms |
14416 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
11 ms |
14416 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
9 ms |
14388 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
10 ms |
14416 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
11 ms |
14484 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
9 ms |
14476 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
11 ms |
14416 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
10 ms |
14416 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
14420 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
10 ms |
14416 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
9 ms |
14376 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
12 ms |
14416 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
10 ms |
14392 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
10 ms |
14416 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
10 ms |
14472 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
10 ms |
14480 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
11 ms |
14544 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
10 ms |
14416 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
10 ms |
14416 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
10 ms |
14468 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
9 ms |
14416 KB |
Unexpected end of file - int32 expected |