Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <vector>
- #include <map>
- using namespace std;
- const int maxn = 1000000 + 1;
- struct cvor{
- int roditelj;
- char slovo;
- cvor(){ roditelj = 0; slovo = 'a'; }
- cvor( int r, char c ){
- roditelj = r;
- slovo = c;
- }
- };
- int N, n;
- char s[ 3 ], c;
- cvor stablo[ maxn ];
- vector< int > pocetak;
- map< int, int > dubina;
- int dp[ maxn ][ 26 ];
- int x = 0, y = 0, p = 0;
- int main( void ){
- scanf( "%d", &N );
- pocetak.push_back( 0 );
- for( int i = 0; i < N; i ++ ){
- scanf( "%s ", s );
- if( s[ 0 ] == 'T' ){
- scanf( "%c", &c );
- x = pocetak[ pocetak.size() - 1 ];
- p ++;
- cvor a = cvor( x, c );
- stablo[ p ] = a;
- pocetak.push_back( p );
- dubina[ p ] = dubina[ x ] + 1;
- dp[ p ][ 0 ] = x;
- for( int j = 1; j <= 26; j ++ )
- dp[ p ][ j ] = dp[ dp[ p ][ j - 1 ] ][ j - 1 ];
- }
- else if( s[ 0 ] == 'U' ){
- scanf( "%d", &n );
- pocetak.push_back( pocetak[ pocetak.size() - n - 1 ] );
- }
- else if( s[ 0 ] == 'G' ){
- scanf( "%d", &n ); n ++;
- int d = dubina[ pocetak[ pocetak.size() - 1 ] ] - n;
- int x1 = pocetak[ pocetak.size() - 1 ];
- for( int j = 0; d != 0 && x1 >= 0; d >>= 1, j ++ )
- if( d & 1 ) x1 = dp[ x1 ][ j ];
- printf( "%c\n", stablo[ x1 ].slovo );
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement