Cod sursa(job #3352176)

Utilizator IanisBelu Ianis Ianis Data 24 aprilie 2026 17:01:28
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.96 kb
#include <bits/stdc++.h>

using namespace std;

struct Treap {
   bool lazy = false;
   int val, pr, cnt = 1;
   Treap *l = nullptr, *r = nullptr;

   Treap(int val): val(val) {
      static mt19937 rng(time(0));
      pr = rng();
   }

   static Treap *update(Treap *t) {
      if (!t) return nullptr;
      t->cnt = size(t->l) + 1 + size(t->r);
      push(t);
      push(t->l);
      push(t->r);
      return t;
   }

   static Treap *push(Treap *t) {
      if (!t || t->lazy == false) return t;
      swap(t->l, t->r);
      t->lazy = false;
      if (t->l) t->l->lazy ^= 1;
      if (t->r) t->r->lazy ^= 1;
      return t;
   }

   static int size(Treap *t) {
      return t ? t->cnt : 0;
   }

   static Treap *join(Treap *a, Treap *b) {
      push(a);
      push(b);
      if (!a) return b;
      if (!b) return a;
      if (a->pr < b->pr) {
         a->r = join(a->r, b);
         return update(a);
      }
      b->l = join(a, b->l);
      return update(b);
   }

   static pair<Treap *, Treap *> split(Treap *t, int pos) {
      push(t);
      if (!t) return { nullptr, nullptr };
      if (pos <= size(t->l)) {
         auto [ l, r ] = split(t->l, pos);
         t->l = r;
         return { l, update(t) };
      }
      auto [ l, r ] = split(t->r, pos - size(t->l) - 1);
      t->r = l;
      return { update(t), r };
   }

   static int query(Treap *t, int pos) {
      push(t);
      if (pos == size(t->l) + 1) return t->val;
      if (pos <= size(t->l)) return query(t->l, pos);
      return query(t->r, pos - size(t->l) - 1);
   }

   static void print(Treap *t) {
      push(t);
      if (!t) return;
      print(t->l);
      cout << t->val << ' ';
      print(t->r);
   }
};

Treap *s = nullptr;

void upd_ins(int pos, int val) {
   auto [ l, r ] = Treap::split(s, pos - 1);
   s = Treap::join(Treap::join(l, new Treap(val)), r);
}

void upd_rev(int L, int R) {
   auto [ l, r ] = Treap::split(s, L - 1);
   auto [ rl, rr ] = Treap::split(r, R - L + 1);
   rl->lazy ^= 1;
   s = Treap::join(l, Treap::join(rl, rr));
}

void upd_del(int L, int R) {
   auto [ l, r ] = Treap::split(s, L - 1);
   auto [ rl, rr ] = Treap::split(r, R - L + 1);
   s = Treap::join(l, rr);
}

signed main() {
#ifdef LOCAL
   freopen("input.txt", "r", stdin);
#else
   freopen("secv8.in", "r", stdin);
   freopen("secv8.out", "w", stdout);
#endif

   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);


   int q, c;
   cin >> q >> c;
   while (q--) {
      char t;
      int l, r, pos, val;
      cin >> t;
      if (t == 'I') {
         cin >> pos >> val;
         upd_ins(pos, val);
      } else if (t == 'A') {
         cin >> pos;
         cout << Treap::query(s, pos) << '\n';
      } else if (t == 'R') {
         cin >> l >> r;
         upd_rev(l, r);
      } else if (t == 'D') {
         cin >> l >> r;
         upd_del(l, r);
      }
   }

   Treap::print(s);

   return 0;
}