Yoda: Visit I will the cloners on Kamino... And see this army they have created for the Republic.
Cloners from the Kamino planet breed some of the finest clones. Such good results are due to the careful management over the clones’ evolution. The Kaminuans are now busy working out a new study technology that lets increase the clones’ effectiveness. The cloners have come up with a new control system CVS (Clone Version System) that makes managing the progress of experiments easier. The system is quite simple to use.
The Kaminuans have some set of educational programs at their disposal. A clone’s effectiveness depends on which programs and in which order he has learned. The Kaminuans can teach any clone a program as long as this clone hasn’t learned it already.
To make the experiments even easier to conduct, the Kaminuans enabled canceling the changes made by the last program the clone has learned. In this case, the clone’s knowledge returns to the level when the program hasn’t yet been studied. Then this clone can study this program in the future. You can cancel programs at any time unless the clone is at the basic knowledge level.
Besides the ‘roll back’ function, a clone can ‘re-learn’ a program. If one cancels some program by mistake, he can cancel the cancellation. The CVS keeps record of each clone’s canceled programs. After a program is canceled, the CVS adds another record. After a clone re-learn a program, the record is deleted. If a clone learn (not relearn) a program, all cancellation record history of this clone is deleted. You can use the re-learn function as long as the record history for this clone contains any records.
Finally, the system has a ‘clone’ option. If a Kaminuan likes the current variant of a clone, he can clone the clone, that is, create a new clone with the same sequence of taught programs and cancellation history.
Initially the Kaminuans have a single clone with basic knowledge. Help them analyze the progress of the experiments.
Input
The first line of the input contains numbers
n — the number of queries — and
m — the number of educational programs (1 ≤
n,
m ≤ 5·10
5). Each of the following
n lines has one of the formats given below.
- learn ci pi. Teach clone ci program pi (1 ≤ pi ≤ m).
- rollback ci. Cancel the last learned program for clone ci.
- relearn ci. Apply ‘re-learn’ function to clone ci.
- clone ci. Clone the clone ci.
- check ci. Display the last program clone ci has learned and knows at the moment.
It is guaranteed that
rollback won’t be applied to the clone that is at the basic knowledge level.
learn is always applied with the program a clone doesn’t already know.
relearn is only applied if the cancellation history of a clone is not empty. It is also guaranteed that only the clones that already exist can occur in the queries. The numbers are assigned to the clones in the order the clones appear. The Kaminuans started their experiments from clone number one.
Output
For each
check c
i query display the result on a single line. If some clone has only basic knowledge, print
basic, otherwise print the number of the last learned program.
Example
input | output |
9 10learn 1 5learn 1 7rollback 1check 1clone 1relearn 2check 2rollback 1check 1 | 57basic |
#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
#define mp(i,j) make_pair(i,j)
#define ft first
#define sd second
typedef long long LL;
typedef pair<int, int> pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const int M = 1e4 + 1;
const double eps = 1e-10;
int T, n ,m, x, y;
int l[N],r[N],u[N],nt[N],sz,cnt;
char s[M];
int main()
{
while(~intwo(n,m))
{
l[1]=r[1]=-1; cnt=1; sz=0;
rep(i,1,n)
{
scanf("%s",s);
if (!strcmp(s,"learn"))
{
intwo(x,y);
u[sz]=y; nt[sz]=l[x]; l[x]=sz++; r[x]=-1;
}
else if (!strcmp(s,"clone"))
{
inone(x);
++cnt; l[cnt]=l[x]; r[cnt]=r[x];
}
else if (!strcmp(s,"rollback"))
{
inone(x); y=u[l[x]];
l[x]=nt[l[x]];
u[sz]=y; nt[sz]=r[x]; r[x]=sz++;
}
else if (!strcmp(s,"relearn"))
{
inone(x); y=u[r[x]];
r[x]=nt[r[x]];
u[sz]=y; nt[sz]=l[x]; l[x]=sz++;
}
else
{
inone(x);
if (l[x]==-1) printf("basic\n");
else printf("%d\n",u[l[x]]);
}
}
}
return 0;
}