Apkawa

Untitled

Jun 18th, 2022
1,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <ext/pb_ds/assoc_container.hpp>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include<bits/stdc++.h>
  4. #define pb push_back
  5. #define be(a) a.begin(), a.end()
  6. #define rbe(a) a.rbegin(), a.rend()
  7. #define L(x, n) for (int x = 0; x < n; ++x)
  8. #define cin(a) for (auto &i : a) cin >> i
  9. #define cout(a, x) for (auto &i : a) cout << i << x; cout << '\n'
  10. #define F first
  11. #define S second
  12. #pragma GCC optimize("Ofast")
  13. using namespace std;
  14. using namespace __gnu_pbds;
  15. typedef pair <int, int> pii;
  16. typedef unsigned int ui;
  17. typedef long long ll;
  18. typedef unsigned long long ull;
  19. typedef long double ld;
  20. template <class T> using oset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
  21. template <class T> using pqueue = priority_queue <T, vector <T>, greater <T>>;
  22. //I love my wife - Elizaveta Minchakova
  23.  
  24. struct node {
  25.     vector <int> history, learned;
  26.     int parentH = -1, H = -1, parentL = -1, L = -1;
  27. } r;
  28.  
  29. int pos[500001];
  30.  
  31. int main() {
  32.     //freopen("input.txt", "r", stdin);
  33.     //freopen("output.txt", "w", stdout);
  34.     ios_base::sync_with_stdio(0);
  35.     cin.tie(0);
  36.  
  37.     int n, m;
  38.     cin >> n >> m;
  39.     int cnt = 0;
  40.     pos[++cnt] = 0;
  41.     vector <node> a = {r};
  42.     while (n--) {
  43.         string op;
  44.         int c, p;
  45.         cin >> op >> c;
  46.         int oldC = c;
  47.         c = pos[c];
  48.         if (op[0] == 'l') {
  49.             cin >> p;
  50.             a[c].learned.push_back(p);
  51.             a[c].history = {};
  52.             a[c].parentH = a[c].H = -1;
  53.         } else if (op[0] == 'c') {
  54.             if (op[1] == 'h') {
  55.                 if (a[c].learned.size()) {
  56.                     cout << a[c].learned.back() << '\n';
  57.                 } else if (a[c].parentL != -1) {
  58.                     cout << a[a[c].parentL].learned[a[c].L] << '\n';
  59.                 } else {
  60.                     cout << "basic\n";
  61.                 }
  62.             } else {
  63.                 if (a[c].history.size()) {
  64.                     r.parentH = c;
  65.                     r.H = a[c].history.size() - 1;
  66.                 } else {
  67.                     r.parentH = a[c].parentH;
  68.                     r.H = a[c].H;
  69.                 }
  70.                 if (a[c].learned.size()) {
  71.                     r.parentL = c;
  72.                     r.L = a[c].learned.size() - 1;
  73.                 } else {
  74.                     r.parentL = a[c].parentL;
  75.                     r.L = a[c].L;
  76.                 }
  77.                 pos[oldC] = a.size();
  78.                 a.pb(r);
  79.                 pos[++cnt] = a.size();
  80.                 a.pb(r);
  81.             }
  82.         } else if (op[1] == 'e') {
  83.             if (a[c].history.size()) {
  84.                 a[c].learned.push_back(a[c].history.back());
  85.                 a[c].history.pop_back();
  86.             } else {
  87.                 a[c].learned.push_back(a[a[c].parentH].history[a[c].H--]);
  88.                 if (a[c].H == -1) {
  89.                     a[c].parentH = a[a[c].parentH].parentH;
  90.                     if (a[c].parentH != -1) {
  91.                         a[c].H = a[a[c].parentH].history.size() - 1;
  92.                     }
  93.                 }
  94.             }
  95.         } else {
  96.             if (a[c].learned.size()) {
  97.                 a[c].history.push_back(a[c].learned.back());
  98.                 a[c].learned.pop_back();
  99.             } else {
  100.                 a[c].history.push_back(a[a[c].parentL].learned[a[c].L--]);
  101.                 if (a[c].L == -1) {
  102.                     a[c].parentL = a[a[c].parentL].parentL;
  103.                     if (a[c].parentL != -1) {
  104.                         a[c].L = a[a[c].parentL].learned.size() - 1;
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         //cout << endl;
  110.         /*for (int i = 1; i <= cnt; ++i) {
  111.             cout << i << ": " << pos[i] << '\n';
  112.         }
  113.         for (auto &i : a) {
  114.             cout << "learned: ";
  115.             cout(i.learned, ' ');
  116.             cout << "history: ";
  117.             cout(i.history, ' ');
  118.             cout << i.parentL << ' ' << i.L << '\n';
  119.             cout << i.parentH << ' ' << i.H << '\n' << endl;
  120.         }*/
  121.     }
  122.  
  123.     return 0;
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment