Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #include<bits/stdc++.h>
- #define pb push_back
- #define be(a) a.begin(), a.end()
- #define rbe(a) a.rbegin(), a.rend()
- #define L(x, n) for (int x = 0; x < n; ++x)
- #define cin(a) for (auto &i : a) cin >> i
- #define cout(a, x) for (auto &i : a) cout << i << x; cout << '\n'
- #define F first
- #define S second
- #pragma GCC optimize("Ofast")
- using namespace std;
- using namespace __gnu_pbds;
- typedef pair <int, int> pii;
- typedef unsigned int ui;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- template <class T> using oset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
- template <class T> using pqueue = priority_queue <T, vector <T>, greater <T>>;
- //I love my wife - Elizaveta Minchakova
- struct node {
- vector <int> history, learned;
- int parentH = -1, H = -1, parentL = -1, L = -1;
- } r;
- int pos[500001];
- int main() {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n, m;
- cin >> n >> m;
- int cnt = 0;
- pos[++cnt] = 0;
- vector <node> a = {r};
- while (n--) {
- string op;
- int c, p;
- cin >> op >> c;
- int oldC = c;
- c = pos[c];
- if (op[0] == 'l') {
- cin >> p;
- a[c].learned.push_back(p);
- a[c].history = {};
- a[c].parentH = a[c].H = -1;
- } else if (op[0] == 'c') {
- if (op[1] == 'h') {
- if (a[c].learned.size()) {
- cout << a[c].learned.back() << '\n';
- } else if (a[c].parentL != -1) {
- cout << a[a[c].parentL].learned[a[c].L] << '\n';
- } else {
- cout << "basic\n";
- }
- } else {
- if (a[c].history.size()) {
- r.parentH = c;
- r.H = a[c].history.size() - 1;
- } else {
- r.parentH = a[c].parentH;
- r.H = a[c].H;
- }
- if (a[c].learned.size()) {
- r.parentL = c;
- r.L = a[c].learned.size() - 1;
- } else {
- r.parentL = a[c].parentL;
- r.L = a[c].L;
- }
- pos[oldC] = a.size();
- a.pb(r);
- pos[++cnt] = a.size();
- a.pb(r);
- }
- } else if (op[1] == 'e') {
- if (a[c].history.size()) {
- a[c].learned.push_back(a[c].history.back());
- a[c].history.pop_back();
- } else {
- a[c].learned.push_back(a[a[c].parentH].history[a[c].H--]);
- if (a[c].H == -1) {
- a[c].parentH = a[a[c].parentH].parentH;
- if (a[c].parentH != -1) {
- a[c].H = a[a[c].parentH].history.size() - 1;
- }
- }
- }
- } else {
- if (a[c].learned.size()) {
- a[c].history.push_back(a[c].learned.back());
- a[c].learned.pop_back();
- } else {
- a[c].history.push_back(a[a[c].parentL].learned[a[c].L--]);
- if (a[c].L == -1) {
- a[c].parentL = a[a[c].parentL].parentL;
- if (a[c].parentL != -1) {
- a[c].L = a[a[c].parentL].learned.size() - 1;
- }
- }
- }
- }
- //cout << endl;
- /*for (int i = 1; i <= cnt; ++i) {
- cout << i << ": " << pos[i] << '\n';
- }
- for (auto &i : a) {
- cout << "learned: ";
- cout(i.learned, ' ');
- cout << "history: ";
- cout(i.history, ' ');
- cout << i.parentL << ' ' << i.L << '\n';
- cout << i.parentH << ' ' << i.H << '\n' << endl;
- }*/
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment