# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1026970 | Summers | Regions (IOI09_regions) | C++14 | 3369 ms | 120808 KiB |
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>
#include<vector>
using namespace std;
vector<long long> v[1000000],d[1000000], ans[1000000],c[1000000];
long long t=1, p[1000000], tin[1000000], tout[1000000];
void dfs(long long vr)
{
tin[vr]=t++;
d[p[vr]].push_back(tin[vr]);
for (int i=0;i<v[vr].size();i++)
{
dfs(v[vr][i]);
}
tout[vr]=t;
}
void dfs1(long long br, long long vr,long long val)
{
if(val==p[vr])br++;
else ans[val][p[vr]]+=br;
for(int i=0;i<v[vr].size();i++)
{
dfs1(br, v[vr][i], val);
}
}
int main()
{
long long i,j,r,q,n,h,s,a,b;
cin>>n>>r>>q;
cin>>h;
p[1]=h;
c[h].push_back(1);
for(i=2;i<=n;i++)
{
cin>>s>>h;
p[i]=h;
v[s].push_back(i);
c[h].push_back(i);
}
dfs(1);
for(i=1;i<=r;i++)
{
if(d[i].size()>=1000)
{
ans[i].resize(r+1);
dfs1(0,1,i);
}
}
while(q--)
{
cin>>a>>b;
if(d[a].size()>=1000)
{
cout<<ans[a][b]<<endl;
cout.flush();
}
else
{
long long an=0;
for (long long i=0;i<c[a].size();i++)
{
auto it1 = upper_bound(d[b].begin(), d[b].end(), tout[c[a][i]]-1);
it1--;
auto it2 =lower_bound(d[b].begin(), d[b].end(), tin[c[a][i]]);
an+=it1-it2+1;
}
cout<<an<<endl;
cout.flush();
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |