Cod sursa(job #3160346)

Utilizator matei0000Neacsu Matei matei0000 Data 23 octombrie 2023 19:06:49
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.64 kb
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#include <stdio.h>
#include <ctype.h>
using namespace std;
#define ptt pair < Treap*, Treap* >
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
class InParser{private:FILE *fin;char *buff;int sp;char read_ch() {++sp;if (sp == 4096) {sp = 0;fread(buff, 1, 4096, fin);}return buff[sp];}public:InParser(const char* nume) {fin = fopen(nume, "r");buff = new char[4096]();sp = 4095;}InParser& operator >> (int &n) {char c;while (!isdigit(c = read_ch()) && c != '-');int sgn = 1;if (c == '-') {n = 0;sgn = -1;} else {n = c - '0';}while (isdigit(c = read_ch())) {n = 10 * n + c - '0';}n *= sgn;return *this;}InParser& operator >> (long long &n) {char c;n = 0;while (!isdigit(c = read_ch()) && c != '-');long long sgn = 1;if (c == '-') {n = 0;sgn = -1;} else {n = c - '0';}while (isdigit(c = read_ch())) {n = 10 * n + c - '0';}n *= sgn;return *this;}InParser& operator >> (char &n){while(n = read_ch() , (n < 'A' || 'Z' < n));return *this;}};
class OutParser {private:FILE *fout;char *buff;int sp;void write_ch(char ch) {if (sp == 50000) {fwrite(buff, 1, 50000, fout);sp = 0;buff[sp++] = ch;} else {buff[sp++] = ch;}}public:OutParser(const char* name) {fout = fopen(name, "w");buff = new char[50000]();sp = 0;}~OutParser() {fwrite(buff, 1, sp, fout);fclose(fout);}OutParser& operator << (int vu32) {if (vu32 <= 9) {write_ch(vu32 + '0');} else {(*this) << (vu32 / 10);write_ch(vu32 % 10 + '0');}return *this;}OutParser& operator << (long long vu64) {if (vu64 <= 9) {write_ch(vu64 + '0');} else {(*this) << (vu64 / 10);write_ch(vu64 % 10 + '0');}return *this;}OutParser& operator << (char ch) {write_ch(ch);return *this;}OutParser& operator << (const char *ch) {while (*ch) {write_ch(*ch);++ch;}return *this;}};
struct Treap{int prio, mar, val, lazy;Treap *st, *dr;Treap(int v, int p){prio = p;mar = 1;val = v;lazy = 0;st = dr = NULL;}};
Treap *root;
inline int marime(Treap *a)
{
    if(a == NULL)
        return 0;
    return a->mar;
}
inline void update(Treap *a)
{
    if(a == NULL)
        return;
    a->mar = marime(a->st) + marime(a->dr) + 1;
}
void propag(Treap *a)
{
    if(a == NULL)
        return;
    if(a->lazy)
    {
        swap(a->st, a->dr);
        if(a->st != NULL)
            a->st->lazy ^= 1;
        if(a->dr != NULL)
            a->dr->lazy ^= 1;
        a->lazy = 0;
    }
}
Treap* join(Treap *a, Treap *b)
{
    propag(a);
    propag(b);
    if(a == NULL)
        return b;
    if(b == NULL)
        return a;
    if(a->prio > b->prio)
    {
        a->dr = join(a->dr, b);
        update(a);
        return a;
    }
    else
    {
        b->st = join(a, b->st);
        update(b);
        return b;
    }
}
ptt split(Treap *a, int x)
{
    propag(a);

    if(a == NULL)
        return {NULL, NULL};
    if(x == 0)
        return {NULL, a};
    if(x == a->mar)
        return {a, NULL};

    if(x <= marime(a->st))
    {
        ptt rez = split(a->st, x);
        a->st = rez.second;
        update(a);
        return {rez.first, a};
    }
    else
    {
        ptt rez = split(a->dr, x - marime(a->st) - 1);
        a->dr = rez.first;
        update(a);
        return {a, rez.second};
    }
}
InParser in("secv8.in");
OutParser out("secv8.out");
inline int acces(Treap *nod, int x)
{
    propag(nod);
    if(x == marime(nod->st) + 1)
        return nod->val;
    if(x <= marime(nod->st))
        return acces(nod->st, x);
    return acces(nod->dr, x - marime(nod->st) - 1);
}
void afis(Treap *nod)
{
    if(nod == NULL)
        return;
    propag(nod);
    afis(nod->st);
    out << nod->val << " ";
    afis(nod->dr);
}
int main()
{
    int q, useless;
    in >> q >> useless;
    for(int rt = 0; rt < q; rt ++)
    {
        char ch;
        in >> ch;
        if(ch == 'I')
        {
            int k, e;
            in >> k >> e;
            ptt r = split(root, k - 1);
            root = join(r.first, join(new Treap(e, rng()), r.second));
        }
        else if(ch == 'A')
        {
            int k;
            in >> k;
            out << acces(root, k) << '\n';
        }
        else if(ch == 'R')
        {
            int i, j;
            in >> i >> j;
            ptt r1 = split(root, i - 1);
            ptt r2 = split(r1.second, j - i + 1);
            r2.first->lazy ^= 1;
            root = join(r1.first, join(r2.first, r2.second));
        }
        else
        {
            int i, j;
            in >> i >> j;
            ptt r1 = split(root, i - 1);
            ptt r2 = split(r1.second, j - i + 1);
            root = join(r1.first, r2.second);
        }
    }
    afis(root);
}