/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
cout<<(x?"YES":"NO")<<endl;
}
const int N=2e5+5,R=25005,B=447;
vector<int>adj[N],arr[R];
int n,r,q,mark=1,t[N],s[N],e[N];
void dfs(int node,int par){
s[node]=mark++;
for(auto i:adj[node])
if(i!=par)
dfs(i,node);
e[node]=mark-1;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
cin>>n>>r>>q;
cin>>t[1];arr[t[1]].push_back(1);
for(int i=2;i<=n;i++){
int x;cin>>x;
cin>>t[i];
arr[t[i]].push_back(i);
adj[x].push_back(i);
}
dfs(1,1);
map<pair<int,int>,int>mp;
for(int i=1;i<=r;i++){
if(arr[i].size()>=B){
for(int j=1;j<=r;j++){
vector<int>v;
int cnt=0;
for(auto x:arr[j])v.push_back(s[x]);
sort(all(v));
for(auto g:arr[i]){
int x=upper_bound(all(v),s[g])-v.begin();
int y=upper_bound(all(v),e[g])-v.begin();
cnt+=y-x;
}
mp[{i,j}]=cnt;
}
}
}
for(int j=1;j<=r;j++){
if(arr[j].size()>=B){
vector<int>v;
for(auto x:arr[j])v.push_back(s[x]);
sort(all(v));
for(int i=1;i<=r;i++){
int cnt=0;
for(auto g:arr[i]){
int x=upper_bound(all(v),s[g])-v.begin();
int y=upper_bound(all(v),e[g])-v.begin();
cnt+=y-x;
}
mp[{i,j}]=cnt;
}
}
}
while(q--){
int a,b;cin>>a>>b;
if(arr[a].size()>=B || arr[b].size()>=B){
cout<<mp[{a,b}]<<endl;
continue;
}
vector<int>v;
for(auto i:arr[b])v.push_back(s[i]);
sort(all(v));
int ans=0;
for(auto i:arr[a]){
int x=upper_bound(all(v),s[i])-v.begin();
int y=upper_bound(all(v),e[i])-v.begin();
ans+=y-x;
}
cout<<ans<<endl;
}
return 0;
}
Compilation message
regions.cpp: In function 'int main()':
regions.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen("input.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen("output.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
2 ms |
5972 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
1 ms |
5720 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
1 ms |
5720 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
1 ms |
5720 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
2 ms |
5720 KB |
Unexpected end of file - int32 expected |