Advertisement
Mlxa

ALGO Persistent Stack

Feb 18th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. namespace Mlxa {
  5.     using namespace std;
  6.     typedef long long               ll;
  7.     typedef long long      ull;
  8.    
  9.     #define all(x)              begin(x), end(x)
  10.     #define eol                 '\n'
  11.     #define read                cin
  12.    
  13.     template <ostream &out, class A> inline void
  14.     write (A a)         { out << a; }
  15.     template <ostream &out, class A, class... B> inline void
  16.     write (A a, B... b) { out << a << ' '; write<out>(b...); }
  17.     template <ostream &out, class I> void
  18.     writeseq (I b, I e) { for(I i=b;i!=e;++i) out<<*i<<' '; }
  19.    
  20.     #define print(...)          write<cout>(__VA_ARGS__)
  21.     #define println(...)        print(__VA_ARGS__, eol);
  22.     #define printseq(...)       writeseq<cout>(__VA_ARGS__), print(eol);
  23.     #ifdef AC
  24.         #define addlog(...)     write<cerr>("[D]\t\t\t\t", __VA_ARGS__, eol);
  25.     #else
  26.         #define addlog(...)     (void(0))
  27.     #endif
  28. }   using namespace Mlxa;
  29.  
  30.  
  31.  
  32.         const ull MAX_V = 2000000;
  33.         const ull LOG_V = 21;
  34.        
  35.         struct Node
  36.         {
  37.             ll value;
  38.             ull len;
  39.             Node *up[LOG_V];
  40.             Node (ll val, Node *p) : value(val)
  41.             {
  42.                 up[0] = p;
  43.                 for (ull i = 1; i < LOG_V && up[i - 1]; ++ i)
  44.                     up[i] = up[i - 1]->up[i - 1];
  45.                 if (p) len = p->len + 1;
  46.                 else   len = 0;
  47.             }
  48.             Node (ll val) : Node(val, nullptr) {}
  49.         }; typedef Node *Ptr;
  50.  
  51.         Ptr ver[MAX_V];
  52.         ull current = 0;
  53.  
  54.         void push (ll x)
  55.         {
  56.             ull v = current;
  57.             ver[++ current] = new Node(x, ver[v]);
  58.         }
  59.  
  60.         void pop ()
  61.         {
  62.             ull v = current;
  63.             if (ver[v])
  64.             ver[++ current] = ver[v]->up[0];
  65.         }
  66.  
  67.         void undo (ull k)
  68.         {
  69.             ull v = current;
  70.             if (k <= v)
  71.                 ver[++ current] = ver[v - k];
  72.         }
  73.        
  74.         Ptr jump (Ptr c, ull j)
  75.         {
  76.             if (j <= 0) return c;
  77.             if (!c)     return c;
  78.            
  79.             for (ull k = LOG_V; k != (ull)-1; -- k) {
  80.                 if ( (1ll << k) <= j )
  81.                     return jump(c->up[k], j - (1ull << k));
  82.             }
  83.             return c;
  84.         }
  85.  
  86.  
  87.         void init ()
  88.         {
  89.             ver[0] = new Node(0);
  90.         }
  91.  
  92.  
  93. int main ()
  94. {
  95.     freopen("scrivener.in", "r", stdin);
  96.     freopen("scrivener.out", "w", stdout);
  97.     ios_base::sync_with_stdio(false);
  98.     cin.tie(nullptr);
  99.    
  100.     init();
  101.    
  102.     char command;
  103.     ull x, q;
  104.     char c;
  105.     string answer;
  106.     read >> q;
  107.    
  108.     for (ull rep = 0; rep < q; ++ rep) {
  109.         read >> command;
  110.         if (command == 'T') {
  111.             read >> c;
  112.             push((ull)c);
  113.         } if (command == 'U') {
  114.             read >> x;
  115.             undo(x);
  116.         } if (command == 'P') {
  117.             read >> x;
  118.             Ptr cur = ver[current];
  119.             if (cur) {
  120.                 ull len = cur->len - 1;
  121.                 Ptr res = jump(ver[current], len - x);
  122.                 if (res)
  123.                     answer += ((char)res->value);
  124.             }
  125.         }
  126.     }
  127.    
  128.     println(answer);
  129.    
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement