Cod sursa(job #2458907)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 21 septembrie 2019 19:59:13
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.54 kb
#include <bits/stdc++.h>
#define lll long long
#define pii pair<int,int>
#define pll pair<lll,lll>
#define pnn pair<nod*,nod*>
#define fi first
#define se second
#define inf (INT_MAX/2-1)
#define infl (1LL<<60)
#define vi vector<int>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a),end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
#define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
#define dbgs(x) cerr<<(#x)<<"[stl]: ";for(int _:x)cerr<<_<<' ';cerr<<'\n',aaa

using namespace std;

ifstream fin ("secv8.in");
ofstream fout ("secv8.out");

int ronce () {
  srand(time(NULL));
  return rand();
}
//mt19937 generator (1222);
mt19937 generator (ronce());
uniform_int_distribution<int> dis(1, inf-1);

struct nod {
  int val, csa, rev, seed;
  nod* lson; nod* rson;
  nod () {
    val = 0; csa = 1; rev = 0; seed = 0;
    lson = rson = NULL;
  }
};

void ininod (nod* u, int val_, int csa_, int rev_, int seed_) {
  u->val = val_; u->csa = csa_; u->rev = rev_; u->seed = seed_;
}

nod* root;

int tcnt (nod* u) {
  if (!u) return 0;
  return u->csa;
}

void tupdcnt (nod* u) {
  if (!u) return;
  u->csa = 1 + tcnt(u->lson) + tcnt(u->rson);
}

void tpropag (nod* u) {
  if(!u) return;
  if (u->rev) {
    u->rev = 0;
    if(u->lson) u->lson->rev ^= 1;
    if(u->rson) u->rson->rev ^= 1;
    swap (u->lson, u->rson);
  }
}

pnn tsplit (nod* u, int loc) {///loc in subarbore
  if (!u) return {NULL, NULL};
  tpropag(u);
  pnn v, w;
  if (tcnt(u->lson)+1 <= loc) {
    v.fi = u;
    w = tsplit(u->rson, loc-tcnt(u->lson)-1);
    v.fi->rson = w.fi;
    v.se = w.se;
  } else {
    v.se = u;
    w = tsplit(u->lson, loc);
    v.se->lson = w.se;
    v.fi = w.fi;
  }
  tupdcnt(v.fi);
  tupdcnt(v.se);
  return v;
}

nod* tmerge (nod* a, nod* b) {
  if (!a) return b;
  if (!b) return a;
  tpropag(a); tpropag(b);
  nod* u;
  if (a->seed > b->seed) {
    a->rson = tmerge(a->rson, b);
    u = a;
  } else {
    b->lson = tmerge(a, b->lson);
    u = b;
  }
  tupdcnt(u);
  return u;
}

void tinsert (nod* cur, nod* u, int loc) {
  pnn v = tsplit(cur, loc-1);
  cur = tmerge(tmerge(v.fi, u), v.se);
  tupdcnt(cur);
}

void tdelete (int l, int r) {
  pnn v = tsplit(root, r);
  pnn w = tsplit(v.fi, l-1);
  root = tmerge(w.fi, v.se);
  tupdcnt(root);
}

void treverse (int l, int r) {
  pnn v = tsplit(root, r);
  pnn w = tsplit(v.fi, l-1);
  if (w.se) w.se->rev ^= 1;
  root = tmerge(tmerge(w.fi, w.se), v.se);
}

void tkth (nod* u, int loc) { ///loc in subarb actual
  if (!u) return;
  tpropag(u);
  if (1+tcnt(u->lson) == loc) { fout << u->val << '\n'; return; }
  if (1+tcnt(u->lson) < loc) tkth(u->rson, loc-tcnt(u->lson)-1);
  else tkth(u->lson, loc);
}

void tdfs (nod* u) {
  if (!u) return;
  tpropag(u);
  tdfs(u->lson);
  if(u != root) fout << u->val << ' ';
  tdfs(u->rson);
}

int main () {
  root = new nod();
  root->seed = inf;
  int t, amrev; fin >> t >> amrev;
  char ch;
  int nr, poz, l, r;
  while(t--) {
    fin >> ch;
    if (ch == 'I') {
      fin >> poz >> nr; ///invers..
      nod* u = new nod();
      ininod(u, nr, 1, 0, dis(generator));
      tinsert(root, u, poz);
    } else if (ch == 'A') {
      fin >> poz;
      tkth(root, poz);
    } else if (ch == 'R') {
      fin >> l >> r;
      treverse(l, r);
    } else {
      fin >> l >> r;
      tdelete(l, r);
    }
  }
  tdfs(root); fout << '\n';
  return 0;
}