保存两个光标的位置。插入,删除,翻转,splay基本操作。这题用G++交mle,用C++交就可以
看这里
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linxer, "/STACK:102400000,102400000")
#define LL long long
#define pii pair<int, int>
#define MP maxe_pair
#define lx i << 1
#define rx lx | 1
#define md (ll + rr >> 1)
#define lson ll, md, lx
#define rson md + 1, rr, rx
#define mod 1000000007
#define inf 25000
#define N 1000020
#define M 500010
int ch[N][2], pre[N], sz[N];
int n, tot, root;
short int a[N], val[N];
bool down[N], ok;
int creat(int v, int fa){
int x = ++tot;
pre[x] = fa;
ch[x][0] = ch[x][1] = 0;
val[x] = v;
sz[x] = 1;
down[x] = 0;
return x;
}
void push_up(int x){
sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
}
int build(int ll, int rr, int fa){
if(ll > rr) return 0;
int x = creat(a[md], fa);
ch[x][0] = build(ll, md - 1, x);
ch[x][1] = build(md + 1, rr, x);
push_up(x);
return x;
}
void push_down(int x){
if(down[x]){
swap(ch[x][0], ch[x][1]);
if(ch[x][0]) down[ch[x][0]] ^= 1;
if(ch[x][1]) down[ch[x][1]] ^= 1;
down[x] = 0;
}
}
void rotate(int x){
int y = pre[x], d = ch[y][1] == x;
ch[y][d] = ch[x][!d];
if(ch[x][!d]) pre[ch[x][!d]] = y;
ch[x][!d] = y;
pre[x] = pre[y];
pre[y] = x;
if(pre[x]) ch[pre[x]][ch[pre[x]][1] == y] = x;
push_up(y);
}
void P(int x){
if(pre[x])
P(pre[x]);
push_down(x);
}
void splay(int x, int goal){
P(x);
while(pre[x] != goal){
int f = pre[x], ff = pre[f];
if(ff == goal)
rotate(x);
else if((ch[ff][1] == f) == (ch[f][1] == x))
rotate(f), rotate(x);
else
rotate(x), rotate(x);
}
push_up(x);
if(goal == 0) root = x;
}
int Kth(int k){
int x = root;
while(x){
push_down(x);
if(sz[ch[x][0]] >= k) x = ch[x][0];
else{
k -= sz[ch[x][0]] + 1;
if(k == 0) return x;
x = ch[x][1];
}
}
return x;
}
void output(int x){
if(!x) return ;
push_down(x);
output(ch[x][0]);
if(val[x] < inf){
if(!ok) ok = 1;
else printf(" ");
printf("%d", val[x]);
}
output(ch[x][1]);
}
void Insert(int k, int v){
if(k == 0){
splay(Kth(1), 0);
ch[root][0] = creat(v, root);
push_up(root);
return ;
}
splay(Kth(k), 0);
splay(Kth(k+1), root);
int f = ch[root][1];
ch[f][0] = creat(v, f);
push_up(f);
push_up(root);
}
void Delete(int k){
if(k == 0){
splay(Kth(1), 0);
ch[root][0] = 0;
push_up(root);
return ;
}
splay(Kth(k), 0);
splay(Kth(k+2), root);
int f = ch[root][1];
ch[f][0] = 0;
push_up(f);
push_up(root);
}
int main(){
int cas;
scanf("%d", &cas);
while(cas--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
int v;
scanf("%d", &v);
a[i] = v;
}
tot = 0;
a[0] = inf, a[n+1] = inf;
root = build(0, n + 1, 0);
int L, R, m;
scanf("%d%d%d", &L, &R, &m);
char s[20];
while(m--){
scanf("%s", s);
if(s[0] == 'M' && s[4] == 'L'){
scanf("%s", s);
if(s[0] == 'L')
L--;
else R--;
}
else if(s[0] == 'M' && s[4] == 'R'){
scanf("%s", s);
if(s[0] == 'L')
L++;
else R++;
}
else if(s[0] == 'I'){
int x;
scanf("%s%d", s, &x);
if(s[0] == 'L')
Insert(L, x), R++;
else
Insert(R + 1, x), R++;
//output(root), puts("");
//printf("%d %d\n", L, R);
}
else if(s[0] == 'D'){
scanf("%s", s);
if(s[0] == 'L')
Delete(L), R--;
else
Delete(R), R--;
//output(root), puts("");
//printf("%d %d\n", L, R);
}
else{
splay(Kth(L), 0);
splay(Kth(R+2), root);
int x = ch[ch[root][1]][0];
down[x] ^= 1;
}
}
ok = 0;
output(root);
puts("");
}
return 0;
}