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>
#define NMAX 200005
#define pb push_back
#define eb emplace_back
#define MOD 100003
#define nl '\n'
#define INF 2147483647
#define LLONG_MAX 9223372036854775807
#define pii pair<int,int>
#define tpl tuple<int,int,int>
//#pragma GCC optimize("O3")
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
/*
*
*
================DEMONSTRATION===================
=====================END========================
*/
int n,r,q;
int r1,r2;
vector<int>tin(NMAX),tout(NMAX);
vector<int>h(NMAX),s(NMAX);
vector<vector<int>>G(NMAX);
vector<int>used(25005);
vector<vector<int>>ans(25005);
vector<vector<int>>reg(25005);
vector<int>R;
vector<vector<int>>aux(NMAX);
int timp;
void read_query()
{
cin>>r1>>r2;
}
void dfs(int node,int parent)
{
tin[node]=timp;
timp++;
used[h[node]]++;
for (auto x: G[node])
{
if (x != parent)
{
dfs(x, node);
}
}
for(auto x:R)
ans[x][h[node]]+=used[x];
used[h[node]]--;
tout[node]=timp;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>r>>q;
cin>>h[1];
reg[h[1]].pb(1);
for(int i=2;i<=n;++i)
{
cin>>s[i]>>h[i];
reg[h[i]].pb(i);
G[i].pb(s[i]);
G[s[i]].pb(i);
}
for(int i=1;i<=r;++i)
{
if(reg[i].size()>450)
{
R.pb(i);
ans[i].resize(r+1,0);
}
}
dfs(1,-1);
for(int i=1;i<=r;++i)
{
for(auto x:reg[i])
{
aux[i].pb(tin[x]);
}
sort(aux[i].begin(),aux[i].end());
}
while(q--)
{
read_query();
if(reg[r1].size()<=450)
{
int res=0;
for(auto x:reg[r1])
{
int st,dr;
st=upper_bound(aux[r2].begin(),aux[r2].end(),tin[x])-aux[r2].begin();
dr=lower_bound(aux[r2].begin(),aux[r2].end(),tout[x])-aux[r2].begin();
res+=(dr-st);
}
cout.flush()<<res<<nl;
cout.flush();
continue;
}
cout.flush()<<ans[r1][r2]<<nl;
cout.flush();
}
return 0;
}
Compilation message (stderr)
regions.cpp:8: warning: "LLONG_MAX" redefined
8 | #define LLONG_MAX 9223372036854775807
|
In file included from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:195,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:34,
from /usr/include/c++/10/climits:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:39,
from regions.cpp:1:
/usr/include/limits.h:135: note: this is the location of the previous definition
135 | # define LLONG_MAX __LONG_LONG_MAX__
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |