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<cstdio>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
#define for1(i,n) for(int i=1;i<=(int)n;i++)
#define for0(i,n) for(int i=0;i<=(int)n;i++)
#define forn(i,n) for(int i=n;i>=1;i--)
#define fo(i,x,y) for(int i=x;i<=(int)y;i++)
#define fr(i,x,y) for(int i=x;i>=(int)y;i--)
#define pb push_back
#define mp make_pair
#define LL long long
const LL Mod=1000*1000*1000+7;
char last;
int OP_NOW;
int letter_par[1000002];
int par[1000002];
int up[1000002][30];
int height[1000002];
int maxhi=0;
int qan=0;
void Init() {}
void TypeLetter(char L) {
    OP_NOW++;
    qan++;
    par[OP_NOW]=qan;
    letter_par[qan]=(L-'a');
    height[qan]=height[par[OP_NOW-1]]+1;
    up[qan][0]=par[OP_NOW-1];
    maxhi=max(maxhi,height[qan]);
    for1(i,log2(maxhi))
        up[qan][i]=up[up[qan][i-1]][i-1];
     //cout<<OP_NOW<<" "<<qan<<endl;
}
void UndoCommands(int U) {
    OP_NOW++;
    par[OP_NOW]=par[OP_NOW-U-1];
    //cout<<OP_NOW<<" "<<qan<<endl;
}
char GetLetter(int P) {
    int N=height[par[OP_NOW]];
    //cout<<par[OP_NOW]<<endl;
    if((P+1)==N)
    {
        return (letter_par[par[OP_NOW]]+'a');
    }
    int pos=par[OP_NOW];
    //cout<<pos<<" "<<up[pos][0]<<" "<<up[pos][1]<<" "<<up[pos][2]<<" "<<up[pos][3]<<endl;
    fr(i,log2(maxhi),0)
    {
        //cout<<up[pos][i]<<" "<<height[up[pos][i]]<<endl;
        if(height[up[pos][i]]>=(P+1))
        {
            //cout<<i<<"dddd"<<endl;
            pos=up[pos][i];
        }
    }
    return (letter_par[pos]+'a');
}
/*
1000
T a
T b
T d
P 1
*/
| # | 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... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |