Cod sursa(job #3178036)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 30 noiembrie 2023 20:47:11
Problema Secv8 Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.07 kb
#include <fstream>
#include <ctime>
#include <random>
using namespace std;
using pii = pair<int,int>;
const int nmax = 25e4 + 1;
ifstream cin("secv8.in");
ofstream cout("secv8.out");
mt19937 mt(time(nullptr));
int n , irr , a, b;
char op;
struct treap
{
    int x , nrd , pri , prop;
    treap *l , *r;
};
void init(treap *&a , int &val)
{
    a->x = val;
    a->nrd = 1;
    a->pri = mt();
    a->prop = 0;
    a->l = a->r = NULL;
}
void prop(treap *&root)
{
    if(root->prop&1)
    {
        swap(root->l,root->r);
    }
    if(root->l!=NULL) root->l->prop += root->prop;
    if(root->r!=NULL) root->r->prop += root->prop;
    root->prop = 0;
}
void fix(treap *&root)
{
    root->nrd = 1;
    if(root->l!=NULL) root->nrd += root->l->nrd;
    if(root->r!=NULL) root->nrd += root->r->nrd;
}
treap *mrg(treap *a , treap *b)
{
    if(a==NULL) return b;
    if(b==NULL) return a;
    prop(a);
    prop(b);
    if(a->pri < b->pri)
    {
        a->r = mrg(a->r,b); fix(a);
        return a;
    }
    else
    {
        b->l = mrg(a,b->l); fix(b);
        return b;
    }
}
pair<treap*,treap*>split(treap *root , int hm)
{
    if(root == NULL) return {NULL,NULL};
    prop(root);
    int l = 1;
    if(root->l != NULL) l += root->l->nrd;
    if(l<=hm)
    {
        pair<treap*,treap*>aux = split(root->r,hm-l);
        root->r = aux.first; fix(root);
        return {root,aux.second};
    }
    else
    {
        pair<treap*,treap*>aux = split(root->l,hm);
        root->l = aux.second; fix(root);
        return {aux.first,root};
    }
}
void ins(treap *&t , int &poz , int &val)
{
    pair<treap*,treap*>aux = split(t,poz-1);
    treap *ts = new treap; init(ts,val);
    t = mrg(aux.first,mrg(ts,aux.second));
}
int kth(treap *root, int poz)
{
    int l = 1;
    if(root->l!=NULL) l += root->l->nrd;
    if(poz==l) return root->x;
    else if(poz < l) return kth(root->l,poz);
    else return kth(root->r,poz-l);
}
void rev(treap *root , int &a , int &b)
{
    pair<treap*,treap*>f = split(root,b);
    pair<treap*,treap*>s = split(f.first,a-1);
    s.second->prop++;
    root = mrg(s.first,mrg(s.second,f.second));
}
void del(treap *root , int &a , int &b)
{
    pair<treap*,treap*>f = split(root,b);
    pair<treap*,treap*>s = split(f.first,a-1);
    root = mrg(s.first,f.second);
}
void afis(treap *root)
{
    if(root==NULL) return;
    prop(root);
    afis(root->l);
    cout << root->x << ' ';
    afis(root->r);
}
signed main()
{
    cin >> n >> irr;
    treap *t = new treap;
    cin >> op >> a >> b;
    init(t,b);
    for(int i = 2 ; i <= n ; ++i)
    {
        cin >> op;
        if(op == 'I')
        {
            cin >> a >> b;
            ins(t,a,b);
        }
        else if(op == 'A')
        {
            cin >> a;
            cout << kth(t,a) << '\n';
        }
        else if(op == 'R')
        {
            cin >> a >> b;
            rev(t,a,b);
        }
        else
        {
            cin >> a >> b;
            del(t,a,b);
        }
    }
    afis(t);
    return 0;
}