This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pb push_back
#define pii pair<int, int>
#define sz(v) (int)v.size()
#define fi first
#define se second
#define INF 999999992000000023
#define MOD 1000000007
#define cint(x) int x;cin >> x;
#define cinarr(a, n) int a[n]; for (int i = 0; i < n; i++) cin >> a[i];
#define coutarr(a) for (auto d : a){cout << d << " ";} cout << endl;
#define coutarrD(a) for (auto d : a) {cout << d.fi << "," << d.se << " "; }cout << endl;
#define BERKAY_TUP ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define ld long double
#define mid (start + end) / 2
#define vvi vector<vector<int>>
int t=1;
int interactive=1;
int usaco=0;
int testCase=0;
int type[200023];
vector<int> edges[200023];
bool seen[200023];
int dt[200023][2];
const int B=300;
int ct=1;
int ordr[400023];
vector<int> ans[25023];
int clr[25023];
int tree[400023];
vector<pii> z[25023];
vector<int> zr[25023];
void update(int x,int v){
while (x<400023){
tree[x]+=v;
x+=x&-x;
}
}
int query(int x){
int ans=0;
while (x>0){
ans+=tree[x];
x-=x&-x;
}return ans;
}
void dfs(int x){
if (seen[x]) return;
seen[x]=true;
dt[x][0]=ct;
ordr[ct]=x;
zr[type[x]].pb(ct);
ct++;
for (int d:edges[x]) dfs(d);
dt[x][1]=ct;
z[type[x]].pb({dt[x][0],dt[x][1]});
ordr[ct]=x;
ct++;
}
void solve(){
int n,m,q;
cin>>n>>m>>q;
cin>>type[1];
clr[type[1]]++;
for (int i=2;i<=n;i++){
int x,c;
cin>>x>>c;
type[i]=c;
clr[c]++;
edges[i].pb(x);
edges[x].pb(i);
}
dfs(1);
for (int i=1;i<=m;i++){
if (clr[i]>=B){
for (int j=0;j<=m;j++) ans[i].pb(0);
for (int i=0;i<400023;i++) tree[i]=0;
for (int j=1;j<=2*n;j++){
if (type[ordr[j]]==i){
if (dt[ordr[j]][0]==j){
update(j,1);
}
else{
update(dt[ordr[j]][0],-1);
}
}
else{
if (dt[ordr[j]][0]==j){
ans[i][type[ordr[j]]]+=query(j);
}
}
}
}
}
for (int i=1;i<=m;i++) sort(z[i].begin(),z[i].end());
while (q--){
int r1,r2;
cin>>r1>>r2;
if (clr[r1]>=B){
cout<<ans[r1][r2]<<endl;
cout<<flush;
}
else{
int ans=0;
for (auto d:z[r1]){
ans+=lower_bound(zr[r2].begin(),zr[r2].end(),d.se)-lower_bound(zr[r2].begin(),zr[r2].end(),d.fi);
}
cout<<ans<<endl;
cout<<flush;
}
}
}
int32_t main(){
BERKAY_TUP;
if (usaco)
{
freopen("help.in", "r", stdin);
freopen("help.out", "w", stdout);
}
if (!interactive)
{
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
// freopen("wormsort.out", "w", stdout);
#endif
}
if (t == 1)
solve();
else
{
cin >> t;
while (t--)
{
testCase++;
solve();
}
}
return 0;
}
Compilation message (stderr)
regions.cpp: In function 'int32_t main()':
regions.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen("help.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | freopen("help.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |