hdu 3487 Play with Chain (Splay树) 区间切割 插入 翻转
关键注意:down 和 up
find,kth,next,pre, RTO等当有flip时,都要down
切割之后,up
插入之后,修改pre
指针:
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FD(i, b, a) for(int i = (b); i >= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(a, v) memset(a, v, sizeof(a))
#define PB push_back
#define MP make_pair
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 310000;
#define ll x->ch[0]
#define rr x->ch[1]
#define KT (root->ch[1]->ch[0])
struct Node{
Node* ch[2];
int v;
int s;
int flip;
Node(){}
///比较的是位序
int cmp(int x)///
{
int ss = ch[0]->s;
if (x == ss + 1) return -1;
else return x < ss + 1 ? 0 : 1;
}
void up()
{
s = ch[0]->s + ch[1]->s + 1;
}
void reverse()
{
swap(ch[0], ch[1]);
flip ^= 1;
}
void pushdown()
{
if (flip)
{
ch[0]->reverse();
ch[1]->reverse();
flip = 0;
}
}
}N[maxn], *null = &N[0];
vector<int>ans;
///Splay维护一个序列
struct SplayTree{
Node *root;
int tot;
void Init(int n)
{
tot = 0;
null->ch[0] = null->ch[1] = null;
null->v = null->s = null->flip = 0;
root = null;
Newnode(root, -1);
Newnode(root->ch[1], -1);
Build(KT, 1, n);
root->ch[1]->up();
root->up();
}
void Newnode(Node* &x, int vv)
{
x = &N[++tot];
x->ch[0] = x->ch[1] = null;
x->v = vv;
x->s = 1;
x->flip = 0;
}
void Build(Node* &x, int l, int r)
{
if (l > r) return ;
int m = (l + r) >> 1;
Newnode(x, m);
Build(ll, l, m - 1);
Build(rr, m + 1, r);
x->up();//up
}
void Rotate(Node* &rt, int d)
{
Node* k = rt->ch[d ^ 1]; rt->ch[d ^ 1] = k->ch[d]; k->ch[d] = rt;
rt->up(); k->up(); rt = k;//up
}
///找到rt序列左数第k个元素并伸展到根
void Splay(Node* &rt, int k)
{
rt->pushdown();///pushdown
int d = rt->cmp(k);
if (d != -1)
{
if (d == 1) k -= rt->ch[0]->s + 1;
Node* p = rt->ch[d];
p->pushdown();///pushdown
int d2 = p->cmp(k);
if (d2 != -1)
{
int k2 = k;
if (d2 == 1) k2 -= p->ch[0]->s + 1;
Splay(p->ch[d2], k2);///!!!!!!!!!!!
if (d == d2) Rotate(rt, d ^ 1);
else Rotate(rt->ch[d], d);
}
Rotate(rt, d ^ 1);
}
}
void CUT()
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
Node *tmp;
Splay(root, x);
Splay(root->ch[1], y - x + 2);
tmp = KT;
KT = null;
root->ch[1]->up(); root->up();///!!!
Splay(root, z + 1);
Splay(root->ch[1], 1);
KT = tmp;
}
void FLIP()
{
int x, y, z;
scanf("%d%d", &x, &y);
Splay(root, x);
Splay(root->ch[1], y - x + 2);
KT->reverse();
}
void out(Node* x)
{
if (x == null) return ;
x->pushdown();///pushdown
out(ll);
if (x->v > 0) ans.push_back(x->v);
out(rr);
}
}sp;
int main ()
{
int n, m;
char op[5];
ans.clear();
while (~scanf("%d%d", &n, &m))
{
if (n == -1 && m == -1) break;
sp.Init(n);
for (int i = 1; i <= m; i++)
{
scanf("%s", op);
if (op[0] == 'C') sp.CUT();
else sp.FLIP();
}
sp.out(sp.root);
int nn = ans.size();
REP(i, nn)
{
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
ans.clear();
}
return 0;
}
/***
///合并left和right。假设left的所有元素比right小。注意:right可以为null,left不可以
Node* Merge(Node* left, Node* right)
{
splay(left, left->s);
left->ch[1] = right;
left->up();//up
return left;
}
///把rt的前k小的节点放在left里,其它放在right里。1<= k <= rt->s.当k = o->s时,right = null
void Split(Node* rt, int k, Node* &left, Node* &right)
{
splay(rt, k);
left = rt;
right = rt->ch[1];
left->ch[1] = null;///
left->up();//up
}
void solve(int a, int b)
{
Node *left, *right, *mid, *tmp_right;
split(root, a, left, tmp_right);///!!!
split(tmp_right, b - a + 1, mid, right);
mid->flip ^= 1;
root = merge(merge(left, right), mid);
}
void debug(Node* &o)
{
if(o == null) return ;
printf("%d(",o->v);
if(o->ch[0]!=null) printf("%d,",o->ch[0]->v);
else printf("null,");
if(o->ch[1]!=null) printf("%d",o->ch[1]->v);
else printf("null");
puts(")");
if(o->ch[0]!=null) debug(o->ch[0]);
if(o->ch[1]!=null) debug(o->ch[1]);
}
*/
数组:
#pragma comment(linker, "/STACK:102400000000,102400000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
typedef long long LL;
typedef vector <int> VI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const int maxn = 311111;
#define ll ch[x][0]
#define rr ch[x][1]
#define KT (ch[ch[root][1]][0])
vector<int>ans;
struct SplayTree{
int ch[maxn][2];
int pre[maxn], sz[maxn];
int val[maxn], flip[maxn];
int root, tot;
void Rotate(int x, int f)
{
int y = pre[x];
down(y); down(x);///
ch[y][!f] = ch[x][f];
pre[ch[x][f]] = y;
pre[x] = pre[y];
if (pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x;
ch[x][f] = y;
pre[y] = x;
up(y);///
}
void Splay(int x, int goal)
{
down(x);///
while (pre[x] != goal)
{
down(pre[pre[x]]); down(pre[x]); down(x); /// 翻转操作down!!!
if (pre[pre[x]] == goal) Rotate(x, ch[pre[x]][0] == x);
else
{
int y = pre[x], z = pre[y];
int f = ( ch[z][0] == y );
if (ch[y][f] == x) Rotate(x, !f), Rotate(x, f);
else Rotate(y, f), Rotate(x, f);
}
}
up(x);///
if (goal == 0) root = x;
}
void RTO(int k, int goal)
{
int x = root;
down(x);///!!!flip
while (sz[ll] + 1 != k)
{
if (k < sz[ll] + 1) x = ll;
else
{
k -= (sz[ll] + 1);
x = rr;
}
down(x);
}
Splay(x, goal);
}
void up(int x)
{
sz[x] = 1 + sz[ll] + sz[rr];
}
void Reverse(int x)///
{
// if (!x) return ;//????
swap(ll, rr);///!!!
flip[x] ^= 1;
}
void down(int x)
{
if (flip[x])
{
Reverse(ll); Reverse(rr);
flip[x] = 0;
}
}
void Newnode(int &x, int c, int f)///&
{
x = ++tot;
ll = rr = 0; pre[x] = f;
sz[x] = 1; flip[x] = 0;
val[x] = c;
}
void Build(int &x, int l, int r, int f)///&
{
if (l > r) return ;
int m = (l + r) >> 1;
Newnode(x, m, f);
Build(ll, l, m - 1, x);
Build(rr, m + 1, r, x);
up(x);
}
void Init(int n)
{
ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0;
val[0] = 0; flip[0] = 0;
root = tot = 0;
Newnode(root, -1, 0);
Newnode(ch[root][1], -1, root);
Build(KT, 1, n, ch[root][1]);///
up(ch[root][1]); up(root);///
}
///
void CUT()
{
int x, y, z;
scanf("%d%d%d",&x, &y, &z);
int tmp;
RTO(x, 0); RTO(y + 2, root);
tmp = KT; KT = 0; up(ch[root][1]); up(root);///up
RTO(z + 1, 0); RTO(z + 2, root);
KT = tmp; pre[tmp] = ch[root][1];///!!!修改pre
up(ch[root][1]); up(root);///up
}
void FLIP()
{
int x, y;
scanf("%d%d", &x, &y);
RTO(x, 0); RTO(y + 2, root);
Reverse(KT);
}
void out(int x)
{
if (!x) return ;
down(x);///!!!
out(ll);
if (val[x] > 0) ans.push_back(val[x]);
out(rr);
}
}sp;
int main ()
{
int n, m;
char op[5];
ans.clear();
int nn;
while (~scanf("%d%d", &n, &m))
{
if (n == -1 && m == -1) break;
sp.Init(n);
for (int i = 1; i <= m; i++)
{
scanf("%s", op);
if (op[0] == 'C') sp.CUT();
else sp.FLIP();
}
sp.out(sp.root);
nn = ans.size();
REP(i, nn)
{
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
ans.clear();
}
return 0;
}