Pagini recente » Cod sursa (job #2974683) | Cod sursa (job #1872701) | Cod sursa (job #2310206) | Cod sursa (job #2965326) | Cod sursa (job #1489517)
#include <bits/stdc++.h>
#define VAL 123456789
using namespace std;
ofstream fout("secv8.out");
struct T
{
T *left,*right;
int cnt,pr,val,inv;
T(int x)
{
left=right=0; val=x;
cnt=1; inv=0; pr=rand()%VAL;
}
} *root = 0;
inline int Cnt(T* nod)
{
if(!nod) return 0;
return nod->cnt;
}
inline void Upd(T *nod)
{
if(!nod) return;
nod->cnt=Cnt(nod->left)+Cnt(nod->right)+1;
}
inline void Push(T *nod)
{
if(!nod || !nod->inv) return;
nod->inv=0;
swap(nod->left,nod->right);
if(nod->left) nod->left->inv^=1;
if(nod->right) nod->right->inv^=1;
}
inline pair <T*,T*> Split(T *nod, int k)
{
pair <T*,T*> spl;
if(nod==0) return make_pair((T*) 0, (T*) 0);
Push(nod);
if(1+Cnt(nod->left)<=k)
{
spl=Split(nod->right,k-(1+Cnt(nod->left)));
nod->right=spl.first; spl.first=nod;
Upd(nod); return spl;
}
else
{
spl=Split(nod->left,k);
nod->left=spl.second; spl.second=nod;
Upd(nod); return spl;
}
}
inline T *Merge(T *L, T*R)
{
if(!L) return R;
if(!R) return L;
Push(L); Push(R);
if(L->pr < R->pr)
{
R->left=Merge(L,R->left);
Upd(R); return R;
}
else
{
L->right=Merge(L->right,R);
Upd(L); return L;
}
}
inline T *Find(T *nod, int k)
{
Push(nod);
if(1+Cnt(nod->left)==k) return nod;
if(1+Cnt(nod->left)>k) return Find(nod->left,k);
return Find(nod->right,k-(1+Cnt(nod->left)));
}
inline void Afis(T *nod)
{
if(!nod) return;
Afis(nod->left); fout<<nod->val<<" "; Afis(nod->right);
}
int main()
{
int n,x,y;
char tip;
ifstream fin("secv8.in");
srand(time(0));
fin>>n>>x;
while(n--)
{
fin>>tip;
if(tip=='A')
{
fin>>x; fout<<Find(root,x)->val<<"\n";
}
else
{
fin>>x>>y;
if(tip=='I')
{
pair <T*,T*> spl=Split(root,x-1);
root=Merge(spl.first,Merge(new T(y),spl.second));
}
if(tip=='D')
{
pair <T*,T*> spl=Split(root,x-1);
pair <T*,T*> spl1=Split(spl.second,y-x+1);
root=Merge(spl.first,spl1.second);
}
if(tip=='R')
{
pair <T*,T*> spl=Split(root,x-1);
pair <T*,T*> spl1=Split(spl.second,y-x+1);
spl1.first->inv^=1;
root=Merge(spl.first,Merge(spl1.first,spl1.second));
}
}
}
Afis(root);
return 0;
}