uva 11922 Permutation Transformer 排列变换 Splay 维护序列 翻转操作

参考:

大白书 

http://blog.csdn.net/zstu_zlj/article/details/10242605

Splay 维护的序列操作,注意lelft不能为null,所以插入一个虚拟节点

此处第k大是指序列左数第k个数

细节见注释:

//#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 = 100010;

struct Node{
    Node* ch[2];
    int v;
    int s;
    int flip;
    Node(){}
    Node(int v, Node* null) : v(v){
        ch[0] = ch[1] = null;
        s = 1; flip = 0;
    }
    ///比较的是位序
    int cmp(int x)///
    {
        int ss = ch[0]->s;
        if (x == ss + 1) return -1;
        else return x < ss + 1 ? 0 : 1;
    }
    void maintain()
    {
        s = ch[0]->s + ch[1]->s + 1;
    }
    void pushdown()
    {
        if (flip)
        {
            flip = 0;
            swap(ch[0], ch[1]);
            ch[0]->flip ^= 1;
            ch[1]->flip ^= 1;
        }
    }
};

///Splay维护一个序列
struct Splay{
    Node *root, *null;
    void init()
    {
        null = new Node;
        null->ch[0] = null->ch[1] = null;
        null->v = null->s = null->flip = 0;
        root = null;
    }
    void build(Node* &o, int n)
    {
        if (n >= 0)///增设虚拟节点0,split 限制 left != null !
        {
            o = new Node(n, null);
            build(o->ch[0], n - 1);
            o->maintain();//maintain
        }
    }
    void rotate(Node* &rt, int d)
    {
        Node* k = rt->ch[d ^ 1]; rt->ch[d ^ 1] = k->ch[d]; k->ch[d] = rt;
        rt->maintain(); k->maintain(); rt = k;//maintain
    }
    ///找到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);
        }
    }

    ///合并left和right。假设left的所有元素比right小。注意:right可以为null,left不可以
    Node* merge(Node* left, Node* right)
    {
        splay(left, left->s);
        left->ch[1] = right;
        left->maintain();//maintain
        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->maintain();//maintain
    }
    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 out(Node* &rt)
    {
        if (rt == null) return ;
        rt->pushdown();///pushdown
        out(rt->ch[0]);
        if (rt->v > 0) printf("%d\n", rt->v);
        out(rt->ch[1]);
//        delete rt;
//        rt = null;
    }
    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]);
    }
}sp;


int main ()
{
    int n, m;
    int x, y;
    while (~scanf("%d%d", &n, &m))
    {
        sp.init();
        sp.build(sp.root, n);
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d", &x, &y);
            sp.solve(x, y);
        }
        sp.out(sp.root);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值