Pagini recente » Cod sursa (job #1642483) | Cod sursa (job #2917859) | Cod sursa (job #1182867) | Cod sursa (job #3154685) | Cod sursa (job #3201442)
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define ptt pair < Treap*, Treap* >
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ifstream in("secv8.in");
ofstream out("secv8.out");
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;
int marime(Treap *a)
{
if(a == NULL)
return 0;
return a->mar;
}
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};
}
}
inline void inser(int k, int e)
{
ptt r = split(root, k - 1);
root = join(r.first, join(new Treap(e, rng()), r.second));
}
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);
}
inline void revers(int i, int 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));
}
inline void delet(int i, int j)
{
ptt r1 = split(root, i - 1);
ptt r2 = split(r1.second, j - i + 1);
root = join(r1.first, r2.second);
}
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;
inser(k, e);
}
else if(ch == 'A')
{
int k;
in >> k;
out << acces(root, k) << '\n';
}
else if(ch == 'R')
{
int i, j;
in >> i >> j;
revers(i, j);
}
else
{
int i, j;
in >> i >> j;
delet(i, j);
}
}
afis(root);
}