Cod sursa(job #3185648)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 19 decembrie 2023 19:43:43
Problema Secv8 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.94 kb
#include <fstream>
#include <random>
#include <ctime>
using namespace std;
ifstream cin("secv8.in");
ofstream cout("secv8.out");
using pii = pair<int,int>;
mt19937 mt(time(nullptr));
struct treap
{
    int val , pri , lz , nrd;
    treap *l , *r;
};
void init(treap *&root , int &val)
{
    root->val = val;
    root->nrd = 1;
    root->lz = 0;
    root->pri = mt();
    root->r = root->l = NULL;
}
void prop(treap *&root)
{
    if(root->l!=NULL) root->l->lz += root->lz;
    if(root->r!=NULL) root->r->lz += root->lz;
    if(root->lz%2)
    {
        swap(root->r,root->l);
    }
    root->lz = 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 val)
{
    if(root == NULL) return {NULL,NULL};
    prop(root);
    int len = 1;
    if(root->l!=NULL) len += root->l->nrd;
    if(len <= val)
    {
        pair<treap*,treap*>aux = split(root->r,val-len);
        root->r = aux.first; fix(root);
        return {root,aux.second};
    }
    else
    {
        pair<treap*,treap*>aux = split(root->l,val);
        root->l = aux.second; fix(root);
        return {aux.first,root};
    }
}
int sm(treap *root)
{
    prop(root);
    if(root->l == NULL) return root->val;
    return sm(root->l);
}
void afis(treap *root)
{
    if(root==NULL) return;
    prop(root);
    afis(root->l);
    cout << root->val << ' ';
    afis(root->r);
}
signed main()
{
    int n , a , b;
    cin >> n >> a;
    char op;
    cin >> op >> a >> b;
    treap *r = new treap;
    init(r,b);
    for(int i = 2 ; i <= n ; ++i)
    {
        cin >> op;
        if(op == 'I')
        {
            cin >> a >> b;
            pair<treap*,treap*>aux = split(r,a-1);
            treap *nw = new treap;
            init(nw,b);
            r = mrg(aux.first,mrg(nw,aux.second));
        }
        if(op == 'A')
        {
            cin >> a;
            pair<treap*,treap*>aux = split(r,a-1);
            cout << sm(r) << '\n';
            r = mrg(aux.first,aux.second);
        }
        if(op == 'R')
        {
            cin >> a >> b;
            pair<treap*,treap*>f = split(r,b);
            pair<treap*,treap*>s = split(f.first,a-1);
            if(s.second!=NULL) s.second->lz++;
            r = mrg(s.first,mrg(s.second,f.second));
        }
        if(op == 'D')
        {
            cin >> a >> b;
            pair<treap*,treap*>f = split(r,b);
            pair<treap*,treap*>s = split(f.first,a-1);
            r = mrg(s.first,f.second);
        }
    }
    afis(r);
    return 0;
}