Cod sursa(job #3226296)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 20 aprilie 2024 20:26:37
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.32 kb
#include <bits/stdc++.h>
using namespace std;

///----------TOGGLES
#define FIO
#define LONGER
//#define TESTS

///---------

#ifdef LONGER
    #define int long long
#endif // LONGER

#ifdef FIO
    const string fname="secv8";
    ifstream in(fname+".in");
    ofstream out(fname+".out");
    #define cin in
    #define cout out
#endif // FIO

///--------

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

///-------STL++-----
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) x.size()
#define pb(x,y) x.push_back(y)
#define mp(x,y) make_pair(x,y)
#define fr(i,n) for(int i=1;i<=n;i++)
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pdd = pair<double, double>;
using veci = vector<int>;
using vecp = vector<pii>;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T> >;

///-----CODE GOES HERE-----

struct Trip{
    int val=0,prio=0;
    int siz=0;
    bool rev=0;
    Trip *l=0 ,*r=0;
    Trip() : val(0), prio(rand()){}
    Trip(int val) : val(val), prio(rand()) {}
    Trip(int val, int prio) : val(val), prio(prio) {}
    ~Trip() { delete l; delete r;}
};
using ptrip = Trip*;

int siz(ptrip t)
{
    return !t?0 : t->siz;
}

void upSiz(ptrip t)
{
    if(t) t->siz= siz(t->l) + siz(t->r) + 1;
}

void push(ptrip t)
{
    if(t&&t->rev)
    {
        swap(t->l, t->r);
        if(t->l) t->l->rev ^=1;
        if(t->r) t->r->rev ^=1;
        t->rev=0;
    }
}

void split(ptrip t, ptrip &l, ptrip &r, int poz, int add=0)
{
    push(t);
    if(!t)l=r=0;
    else
    {
        int tpoz=siz(t->l)+add+1;
        if(poz<=tpoz) split(t->l, l, t->l, poz, add), r=t;
        else split(t->r, t->r, r, poz, tpoz), l=t;
    }
    upSiz(l);
    upSiz(r);
}

void merge(ptrip &t, ptrip l, ptrip r)
{
    push(l);
    push(r);
    if(!l||!r) t=l?l:r;
    else
    {
        if(l->prio>=r->prio) merge(l->r, l->r, r), t=l;
        else merge(r->l, l, r->l), t=r;
    }
    upSiz(t);
}

void insert(ptrip &t, ptrip x, int poz, int add=0)
{
    push(t);
    if(!t) t=x;
    else
    {
        if(t->prio<=x->prio)
        {
            split(t, x->l, x->r, poz, add);
            t=x;
        }
        else
        {
            int rpoz=siz(t->l)+ add+ 1;
            if(poz<=rpoz) insert(t->l, x, poz, add);
            else insert(t->r, x, poz, rpoz);
        }
    }
    upSiz(t);
}

int getVal(ptrip &t, int poz, int add=0)
{
    assert(t);
    push(t);
    int rpoz=siz(t->l) + add+1;
    //cerr<<"here "<<t->val<<' '<<siz(t->l)<<' '<<poz<<' '<<rpoz<<'\n';
    if(rpoz==poz) return t->val;
    else if(poz<rpoz) return getVal(t->l, poz, add);
    else return getVal(t->r, poz, rpoz);
}

void erase(ptrip &t, int st, int dr)
{
    ptrip l=0, mid=0, r=0;
    split(t, mid, r, dr+1);
    split(mid, l, mid, st);
    delete mid;
    merge(t, l, r);
}

void reverse(ptrip &t, int st, int dr)
{
    ptrip l=0, mid=0, r=0;
    split(t, mid, r, dr+1);
    split(mid, l, mid, st);
    //cerr<<"here "<<siz(l)<<' '<<siz(mid)<<' '<<siz(r)<<'\n';
    if(mid) mid->rev^=1;
    merge(t, l, mid);
    merge(t, t, r);
}

int q;
bool isR;
int n;

void solve()
{
    ptrip root=0;
    cin>>q>>isR;
    char c; int a,b;
    for(int i=1;i<=q;i++)
    {
        cin>>c;
        if(c=='I')
        {
            cin>>a>>b;
            insert(root, new Trip(b), a);
            n++;
        }
        else if(c=='A')
        {
            cin>>a;
            assert(1<=a&&a<=n);
            cout<<getVal(root, a)<<'\n';
        }
        else if(c=='D')
        {
            cin>>a>>b;
            assert(a<=b&&1<=a&&b<=n);

            //cerr<<"erasing  "<<a<<' '<<b<<'\n';
            n-=(b-a+1);
            erase(root, a, b);
        }
        else if(c=='R')
        {
            cin>>a>>b;
            assert(a<=b&&1<=a&&b<=n);
            //cerr<<"reversing  "<<a<<' '<<b<<'\n';
            reverse(root, a, b);
        }
        else assert(0);
    }
    for(int j=1;j<=n;j++)
    {
        cout<<getVal(root,j)<<' ';
        //cerr<<"end\n";
    }
}

///---MAIN FUNC-------

int32_t main()
{
    IOS;
    int t=1;
    #ifdef TESTS
        cin>>t;
    #endif // TESTS
    while(t--)
    {
        solve();
    }
    return 0;
}