Cod sursa(job #2653909)

Utilizator stefantagaTaga Stefan stefantaga Data 29 septembrie 2020 15:48:23
Problema Secv8 Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.34 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("secv8.in");
ofstream g("secv8.out");
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
struct nod{
    nod *st, *dr;
    int prior;
    int val;
    int sz;
    bool rev;
};
nod *nil= new nod;
nod *reverse1(nod *n)
{
    swap(n->st,n->dr);
    n->rev=n->rev^1;
    return n;
}
nod *propag (nod *n)
{
    if (n!=nil&&n->rev==true)
    {
        swap(n->st,n->dr);
        n->st->rev^=1;
        n->dr->rev^=1;
        n->rev=false;
    }
    return n;
}
nod *mod_fiu (nod *n , int care ,nod *fiu)
{
    if (care==0)
    {
        n->st=fiu;
    }
    else
    {
        n->dr=fiu;
    }
    n->sz=n->st->sz+1+n->dr->sz;
    return n;
}
nod *join (nod *st , nod *dr)
{
    st=propag(st);
    dr=propag(dr);
    if (st==nil)
    {
        return dr;
    }
    if (dr==nil)
    {
        return st;
    }
    if (st->prior>=dr->prior)
    {
        return mod_fiu(st,1,join(st->dr,dr));
    }
    else
    {
        return mod_fiu(dr,0,join(st,dr->st));
    }
}
pair <nod*,nod*> split (nod *n , int k)
{
    n=propag(n);
    if (n==nil)
    {
        return {nil,nil};
    }
    if (n->st->sz>=k)
    {
        /*n=[st]++[dr]
        n=[x]++[y]++[dr] , join(x,y)=n->st [x] are k elemente?
        */
        auto t=split( n->st, k);
        return make_pair(t.first,mod_fiu(n,0,t.second));
    }
    else
    {
        /*n=[st]++[x]++[y] , join(x,y)= n->dr [x] are k-n->st elemente
        */
        auto t=split(n->dr,k- n->st->sz -1);
        return make_pair(mod_fiu(n,1,t.first),t.second);
    }
}
int acces (nod *T, int poz) {
    T = propag(T);
    auto t = split(T, poz+1);
    auto t_ = split(t.first, poz);
    T = join(join(t_.first, t_.second), t.second);
    return t_.second->val;
}
nod *invers (nod *n, int x, int y) {
  //  n = propag(n);
    auto t = split(n, y+1);
    auto t_ = split(t.first, x);
    t_.second->rev=t_.second->rev^1;
    return join(join(t_.first, t_.second), t.second);
}
nod *adaug (nod *n, int poz, int val) {
    n = propag(n);
    pair <nod*, nod*> S = split(n, poz);
    nod *now = new nod;
    *now = nod{nil, nil, uniform_int_distribution <int>(1, 2000000000)(rng), val, 1, 0};
    return join(join(S.first, now), S.second);
}
nod *delete1(nod *n, int x,int y)
{
    n=propag(n);
    auto t=split (n,y+1);
    auto t_= split (t.first,x);
    return join(t_.first,t.second);
}
void afisare (nod *n)
{
    if (n==nil)
    {
        return;
    }
    afisare(n->st);
    g<<n->val<<" ";
    afisare(n->dr);
}
int q,i,poz,val,st,dr,p;
char tip;
int main()
{
    nod *n=nil;
    f>>q>>p;
    for (i=1;i<=q;i++)
    {
        f>>tip;
        if (tip=='I')
        {
            f>>poz>>val;
            poz--;
            n=adaug(n,poz,val);
        }
        else
        if (tip=='D')
        {
            f>> st >>dr;
            st--;
            dr--;
            n=delete1(n,st,dr);
        }
        else
        if (tip=='R')
        {
            f>> st>> dr;
            st--;
            dr--;
            n=invers(n,st,dr);
        }
        else
        if (tip=='A')
        {
            f>>poz;
            poz--;
            g<<acces(n,poz)<<'\n';
        }
    }
    afisare(n);
    return 0;
}