# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
238304 | Andrei_Cotor | Ball Machine (BOI13_ballmachine) | C++11 | 1095 ms | 18424 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<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int P[20][100005];
vector<int> Sons[100005];
int Val[100005],Nr[100005];
bool Ball[100005];
set<pair<int,int> > S;
void dfs(int nod)
{
Val[nod]=nod;
for(auto son:Sons[nod])
{
dfs(son);
Val[nod]=min(Val[nod],Val[nod]);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,q;
cin>>n>>q;
int root=0;
for(int i=1; i<=n; i++)
{
cin>>P[0][i];
Sons[P[0][i]].push_back(i);
if(P[0][i]==0)
root=i;
}
for(int i=1; i<=16; i++)
{
for(int j=1; j<=n; j++)
P[i][j]=P[i-1][P[i-1][j]];
}
dfs(root);
for(int i=1; i<=n; i++)
{
if(Sons[i].size()==0)
S.insert({Val[i],i});
}
for(int i=1; i<=q; i++)
{
int tip,x;
cin>>tip>>x;
if(tip==1)
{
int rez=0;
for(int j=1; j<=x; j++) //suma x-ilor va fi max n+q
{
int nod=(*S.begin()).second;
S.erase(S.begin());
Ball[nod]=1;
rez=nod;
if(nod!=root)
{
int p=P[0][nod];
Nr[p]++;
if(Nr[p]==Sons[p].size())
S.insert({Val[p],p});
}
}
cout<<rez<<"\n";
}
else
{
int rez=0;
for(int i=16; i>=0; i--)
{
if(Ball[P[i][x]])
{
rez+=(1<<i);
x=P[i][x];
}
}
cout<<rez<<"\n";
Ball[x]=0;
if(x!=root)
{
int p=P[0][x];
if(Nr[p]==Sons[p].size())
{
Nr[p]--;
S.erase({Val[p],p});
}
}
S.insert({Val[x],x});
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |