Cod sursa(job #1856450)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 24 ianuarie 2017 21:35:08
Problema Secv8 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <bits/stdc++.h>
using namespace std;

struct nod{
    int val, prior, sz, flip;
    nod *st, *dr; } *nil = new nod { 0, 0, 0, 0, nullptr, nullptr };

using ns = nod*;

ns prop(ns n){
    if(n != nil && n->flip){
        n->flip = 0;
        n->st->flip ^= 1, n->dr->flip ^= 1;
        swap(n->st, n->dr); }
    return n; }

ns mod_fiu(ns n, const int care, ns fiu){
    (care ? n->dr : n->st) = fiu;
    n->sz = 1 + n->st->sz + n->dr->sz;
    return n; }

ns join(ns st, ns dr){
    prop(st), prop(dr);
    return st == nil               ? dr :
           dr == nil               ? st :
           (st->prior > dr->prior) ? mod_fiu(st, 1, join(st->dr, dr)) :
                                     mod_fiu(dr, 0, join(st, dr->st)); }

struct pns { ns l, r; };

pns split(ns n, const int where){
    prop(n);
    pns t = {};
    const int lsz = n->st->sz;
    return where == 0     ? pns{nil, n} :
           where == n->sz ? pns{n, nil} :
           (where <= lsz) ? (t = split(n->st, where      ), t.r = mod_fiu(n, 0, t.r), t) :
                            (t = split(n->dr, where-lsz-1), t.l = mod_fiu(n, 1, t.l), t); }

void print(ns n, ofstream& g){
    if(n == nil) return;
    prop(n);
    print(n->st, g);
    g << n->val << ' ';
    print(n->dr, g); }

int main(){
    ifstream f("secv8.in");
    ofstream g("secv8.out");
    int n, useless;
    f >> n >> useless;

    ns rad = nil;
    
    while(n--){
        char ch;
        int a, b;
        f >> ch >> a;
        if(ch != 'A') f >> b;
        if(ch == 'I'){
            pns t = split(rad, a-1);
            rad = join(join(t.l, new nod { b, rand(), 1, 0, nil, nil }), t.r); }
        else if(ch == 'R'){
            pns p = split(rad, a-1), q = split(p.r, b-a+1);
            q.l->flip ^= 1;
            rad = join(join(p.l, q.l), q.r); }
        else if(ch == 'D'){
            pns p = split(rad, a-1), q = split(p.r, b-a+1);
            rad = join(p.l, q.r); }
        else{
            pns p = split(rad, a-1), q = split(p.r, 1);
            g << q.l->val << '\n';
            rad = join(join(p.l, q.l), q.r); } }
    print(rad, g);
    return 0; }