https://www.luogu.org/problem/P4567
事实证明无旋Treap是不是不可能会比Splay快?
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls(p) ch[p][0]
#define rs(p) ch[p][1]
const int MAXN = 2400000 + 5;
char val[MAXN];
int ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;
int cur;
bool rev[MAXN];
void Init() {
root = 0, tot = 0;
}
inline void PushUp(int p) {
siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
}
inline void PushDown(int p) {
if(rev[p]) {
swap(ls(p), rs(p));
if(ls(p))
rev[ls(p)] ^= 1;
if(rs(p))
rev[rs(p)] ^= 1;
rev[p] = 0;
}
}
void SplitRank(int p, int rk, int &x, int &y) {
if(!p) {
x = y = 0;
return;
}
PushDown(p);
if(rk <= siz[ls(p)]) {
y = p;
SplitRank(ls(p), rk, x, ls(p));
PushUp(y);
} else {
x = p;
SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
PushUp(x);
}
}
int Merge(int x, int y) {
if(!x || !y)
return x | y;
if(rnd[x] < rnd[y]) {
PushDown(x);
rs(x) = Merge(rs(x), y);
PushUp(x);
return x;
} else {
PushDown(y);
ls(y) = Merge(x, ls(y));
PushUp(y);
return y;
}
}
int NewNode(int v) {
int p = ++tot;
ch[p][0] = ch[p][1] = 0;
val[p] = v, rnd[p] = rand();
siz[p] = 1;
rev[p]=false;
return p;
}
//O(n)建树,返回新树的根
int st[MAXN], stop;
char buf[MAXN];
inline int Build(int n) {
stop = 0;
for(int i = 0; i < n; ++i) {
int tmp = NewNode(buf[i]), last = 0;
while(stop && rnd[st[stop]] > rnd[tmp]) {
last = st[stop];
PushUp(last);
st[stop--] = 0;
}
if(stop)
rs(st[stop]) = tmp;
ls(tmp) = last;
st[++stop] = tmp;
}
while(stop)
PushUp(st[stop--]);
return st[1];
}
inline void Move() {
scanf("%d", &cur);
}
inline void Insert(int &root) {
int x = 0, y = 0, z = 0, n;
SplitRank(root, cur, x, z);
scanf("%d", &n);
getchar();
buf[n] = ' ';
for(int i = 0; i < n; ++i){
buf[i] = getchar();
}
y = Build(n);
root = Merge(Merge(x, y), z);
}
inline void Delete(int &root) {
int x = 0, y = 0, z = 0, n;
SplitRank(root, cur, x, y);
scanf("%d", &n);
SplitRank(y, n, y, z);
//会不会太慢了
//UnBuild(y);
//y=Merge(ls(y),rs(y));
root = Merge(x, z);
}
void Show(int p) {
if(!p)
return;
PushDown(p);
Show(ls(p));
if(val[p]!='
')
putchar(val[p]);
else
printf("\n");
Show(rs(p));
}
inline void Get(int &root) {
int x = 0, y = 0, z = 0, n = 0;
SplitRank(root, cur, x, y);
SplitRank(y, 1, y, z);
putchar(val[y]);
if(val[y]!='
')
putchar('
');
root = Merge(Merge(x, y), z);
}
inline void Prev() {
--cur;
}
inline void Next() {
++cur;
}
void Reverse(int &root) {
int l = cur+1, r;
scanf("%d", &r);
r = l + r - 1;
//cout<<"l="<<l<<" r="<<r<<endl;
int x = 0, y = 0, z = 0;
SplitRank(root, l - 1, x, y);
SplitRank(y, r + 1 - l, y, z);
rev[y] ^= 1;
root = Merge(Merge(x, y), z);
}
char op[20];
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int t;
scanf("%d", &t);
Init();
while(t--) {
scanf("%s", op);
switch(op[0]) {
case 'M':
Move();
break;
case 'I':
Insert(root);
break;
case 'D':
Delete(root);
break;
case 'R':
Reverse(root);
break;
case 'G':
Get(root);
break;
case 'P':
Prev();
break;
case 'N':
Next();
break;
}
/*printf("op=%s
",op);
int x=0,y=0;
SplitRank(root,cur,x,y);
Show(x);
putchar('|');
Show(y);
putchar('
');
root=Merge(x,y);*/
}
return 0;
}