Cod sursa(job #2745989)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 27 aprilie 2021 12:45:36
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.38 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define nozerous(x) (x & -x)
#define MOD 1000000007
#define M_PI           3.14159265358979323846
#define EPS 0.0001
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = (1 << 30), NMAX(1005);
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;
using Point = array<int, 2>;
using ll = long long;

void BUNA(const string& task = "")
{
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
                freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PA()
{
    exit(0);
};

struct treap{
    int prior, value, cnt;
    bool rev;
    treap *l, *r;
};
treap* nill = new treap{0, 0, 0, false, NULL, NULL};
int cnt(treap * it){
    return (it != nill ? it->cnt : 0);
}
void upd_cnt(treap *it){
    if(it != nill)
        it->cnt = cnt(it->l) + cnt(it->r) + 1;
}
void push(treap *it){
    if(it != nill && it->rev){
        it->rev = false;
        swap(it->l, it->r);
        if(it->l != nill)
            it->l->rev ^= true;
        if(it->r != nill)
            it->r->rev ^= true;
    }
}
void merge(treap * &t, treap *l, treap *r){
    push(l);
    push(r);
    if(l == nill || r == nill)
        t = (l != nill ? l : r);
    else if(l->prior > r->prior)
        merge(l->r, l->r, r), t = l;
    else merge(r->l, l, r->l), t = r;
    upd_cnt(t);
}
void split(treap *t, treap * &l, treap* &r, int key){
    if(t == nill)
        return void(l = r = nill);
    push(t);
    if(cnt(t->l) < key)
        split(t->r, t->r, r, key - cnt(t->l) - 1), l = t;
    else split(t->l, l, t->l, key), r = t;
    upd_cnt(t);
}
void reverse(treap * t, int l, int r){
    treap *t1, *t2, *t3;
    split(t, t1, t2, l - 1);
    split(t2, t2, t3, r - l + 1);
    t2->rev ^= 1;
    merge(t, t1, t2);
    merge(t, t, t3);
}
void insert(treap * &t, int val, int poz){
    treap *t1, *t2;
    treap* node = new treap{(int)(1LL * rng() % MOD), val, 1, false, nill, nill};
    split(t, t1, t2, poz - 1);
    merge(t1, t1, node);
    merge(t, t1, t2);
    upd_cnt(t);
}
int search(treap *t, int poz){
    push(t);
    if(t->l->cnt + 1 == poz)
        return t->value;
    if(t->l->cnt >= poz)
        return search(t->l, poz);
    return search(t->r, poz - t->l->cnt - 1);
}
void del(treap * &t, int st, int dr){
    treap *t1, *t2, *t3;
    split(t, t1, t2, st - 1);
    split(t2, t2, t3, dr - st + 1);
    merge(t, t1, t3);
}
void output(treap *t){
    if(t == nill)
        return;
    push(t);
    output(t->l);
    cout << t->value << ' ';
    output(t->r);
}
treap* root = nill;
int main()
{
    BUNA("secv8");
    int n, c;
    cin >> n >> c;
    for(int i = 1; i <= n; ++i){
        string op;
        cin >> op;
        if(op == "I"){
            int k, e;
            cin >> k >> e;
            insert(root, e, k);
        }
        else if(op == "A"){
            int x;
            cin >> x;
            cout << search(root, x) << '\n';
        }
        else if(op == "R"){
            int a, b;
            cin >> a >> b;
            reverse(root, a, b);
        }
        else {
            int a, b;
            cin >> a >> b;
            del(root, a, b);
        }
    }
    output(root);
    PA();
}