Problem

有n个数,为1~n。有两种操作:
Cut x y z: 把x到y的区间切割下来后,放到改变后的序列的z位后
Flip x y: 把x到y的区间翻转

Solution

splay模板题

Notice

注意0

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 300000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int point, root, cnt, X[N + 5], Y[N + 5], n;
struct node
{
	int val[N + 5], son[2][N + 5], parent[N + 5], Size[N + 5], rev[N + 5];
	inline void up(int u)
	{
	    Size[u] = Size[son[0][u]] + Size[son[1][u]] + 1;
	}
	inline void down(int u)
	{
	    if (rev[u])
        {
            swap(son[0][u], son[1][u]);
            rev[son[0][u]] ^= 1, rev[son[1][u]] ^= 1;
            rev[u] = 0;
        }
	}
	void Newnode(int &u, int from, int v)
	{
	    u = ++point;
        val[u] = v;
        son[0][u] = son[1][u] = rev[u] = 0;
        parent[u] = from, Size[u] = 1;
	}
    void Build(int &u, int l, int r, int from)
    {
        int mid = (l + r) >> 1;
        Newnode(u, from, X[mid]);
        if (l < mid) Build(son[0][u], l, mid - 1, u);
        if (r > mid) Build(son[1][u], mid + 1, r, u);
        up(u);
    }
    void Init(int n)
    {
        rep(i, 1, n) X[i] = i;
        point = 0, cnt = 0;
        son[0][0] = son[1][0] = Size[0] = val[0] = parent[0] = rev[0] = 0;
        Newnode(root, 0, -1);
        Newnode(son[1][root], root, -1);
        Build(son[0][son[1][root]], 1, n, son[1][root]);
        up(son[1][root]), up(root);
    }

	void Rotate(int x, int &rt)
	{
		int y = parent[x], z = parent[y];
		down(y), down(x);
		int l = (son[1][y] == x), r = 1 - l;
		if (y == rt) rt = x;
		else if (son[0][z] == y) son[0][z] = x;
		else son[1][z] = x;
		parent[x] = z;
		parent[son[r][x]] = y, son[l][y] = son[r][x];
		parent[y] = x, son[r][x] = y;
		up(y), up(x);
	}
	void Splay(int x, int &rt)
	{
		while (x != rt)
		{
			int y = parent[x], z = parent[y];
			down(z), down(y);
			if (y != rt)
			{
				if ((son[0][z] == y) ^ (son[0][y] == x))
					Rotate(x, rt);
				else Rotate(y, rt);
			}
			Rotate(x, rt);
		}
	}

    void Out(int u)
    {
        if (!u || val[u] == 0) return;
        down(u);
        Out(son[0][u]);
        if (val[u] != -1) Y[++cnt] = val[u];
        Out(son[1][u]);
    }
	int Find(int u, int x)
	{
	    down(u);
		if (x <= Size[son[0][u]]) return Find(son[0][u], x);
		if (x > Size[son[0][u]] + 1) return Find(son[1][u], x - Size[son[0][u]] - 1);
		return u;
	}
	void Split(int l, int r)
	{
	    int x = Find(root, l - 1 + 1);
	    int y = Find(root, r + 1 + 1);
	    Splay(x, root);
	    Splay(y, son[1][root]);
	}

    void Cut(int x, int y, int z)
    {
        Split(x, y);
        int tt = son[0][son[1][root]];
        son[0][son[1][root]] = 0;
        up(son[1][root]), up(root);
        Split(z + 1, z);
        son[0][son[1][root]] = tt;
        parent[tt] = son[1][root];
        up(son[1][root]), up(root);
    }
    void Flip(int x, int y)
    {
        Split(x, y);
        rev[son[0][son[1][root]]] ^= 1;
    }
}Splay_tree;

int sqz()
{
    while(1)
    {
        n = read(); int q = read();
        if (n < 0 && q < 0) break;
        Splay_tree.Init(n);
        char op[10];
        int x, y, z;
        while (q--)
        {
            scanf("%s", op);
            if (op[0] == 'C')
            {
                x = read(), y = read(), z = read();
                Splay_tree.Cut(x, y, z);
            }
            else
            {
                x = read(), y = read();
                Splay_tree.Flip(x, y);
            }
        }
        Splay_tree.Out(root);
        rep(i, 1, n - 1) printf("%d ", Y[i]);
        printf("%d\n", Y[n]);
    }
    return 0;
}
posted on 2017-10-06 13:09  WizardCowboy  阅读(172)  评论(0编辑  收藏  举报