#include <stdlib.h>
#include <stdio.h>
#define MAXK 1000006
static int K;
static struct STATE{
int len;
char letter;
STATE *prev[20];
} *state[MAXK];
void Init() {}
STATE *make(int len, char letter, STATE *prev)
{
STATE *ret=(STATE*)malloc(sizeof(STATE));
ret->len = len; ret->letter = letter;
for (int i=0;i<20;i++) ret->prev[i] = NULL;
ret->prev[0] = prev;
for (int i=0;i<19&&ret->prev[i];i++){
ret->prev[i+1] = ret->prev[i]->prev[i];
}
return ret;
}
void TypeLetter(char letter)
{
STATE *tmp;
if (state[K]) tmp = make(state[K]->len+1,letter,state[K]);
else tmp = make(1,letter,NULL);
state[++K] = tmp;
}
void UndoCommands(int steps)
{
state[++K] = state[K-steps-1];
}
char GetLetter(int pos)
{
STATE *now=state[K];
int bef=now->len-pos-1,i;
for (i=20;i--;) if ((bef>>i)&1){
now = now->prev[i];
}
return now->letter;
}
I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCiNkZWZpbmUgTUFYSyAxMDAwMDA2CgpzdGF0aWMgaW50IEs7CgpzdGF0aWMgc3RydWN0IFNUQVRFewogICAgaW50IGxlbjsKICAgIGNoYXIgbGV0dGVyOwogICAgU1RBVEUgKnByZXZbMjBdOwp9ICpzdGF0ZVtNQVhLXTsKCnZvaWQgSW5pdCgpIHt9CgpTVEFURSAqbWFrZShpbnQgbGVuLCBjaGFyIGxldHRlciwgU1RBVEUgKnByZXYpCnsKICAgIFNUQVRFICpyZXQ9KFNUQVRFKiltYWxsb2Moc2l6ZW9mKFNUQVRFKSk7CiAgICByZXQtPmxlbiA9IGxlbjsgcmV0LT5sZXR0ZXIgPSBsZXR0ZXI7CiAgICBmb3IgKGludCBpPTA7aTwyMDtpKyspIHJldC0+cHJldltpXSA9IE5VTEw7CiAgICByZXQtPnByZXZbMF0gPSBwcmV2OwogICAgZm9yIChpbnQgaT0wO2k8MTkmJnJldC0+cHJldltpXTtpKyspewogICAgICAgIHJldC0+cHJldltpKzFdID0gc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