1、题目大意:维护序列,只有区间翻转这个操作

2、分析:splay的经典操作就是实现区间翻转,就是在splay中有一个标记,表示这个区间被翻转了

然后就是记得各种的操作访问某个点时,记得下传,顺便交换一下左右子树的左右子树(我语文不好

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
    Node *ch[2];
    int f, v, s;
    int cmp(int x){
        if(x < v) return 0;
        else if(x == v) return -1;
        else return 1;
    }
    int cmp1(int x){
        int k = 1;
        if(ch[0]) k += ch[0] -> s;
        if(x < k) return 0;
        else if(x == k) return -1;
        else if(x > k) return 1;
    }
    void maintain(){
        s = 1;
        if(ch[0]) s += ch[0] -> s;
        if(ch[1]) s += ch[1] -> s;
    }
    void pushdown(){
        if(f == 1){
            if(ch[0] != NULL){
                ch[0] -> f ^= 1;
                swap(ch[0] -> ch[0], ch[0] -> ch[1]);
            }
            if(ch[1] != NULL){
                ch[1] -> f ^= 1;
                swap(ch[1] -> ch[0], ch[1] -> ch[1]);
            }
            f = 0;
        }
    }
};
struct splay_tree{
    Node ft[2000000];
    Node *root;
    int a[200000], tot;
    void rotate(Node* &o, int d){
        Node *k = o -> ch[d ^ 1];
        k -> pushdown();
        o -> ch[d ^ 1] = k -> ch[d];
        k -> ch[d] = o;
        o -> maintain();
        k -> maintain();
        o = k;
    }
    void splay(Node* &o, int k){
        if(!o) return;
        o -> pushdown();
        if(o -> ch[0]) o -> ch[0] -> pushdown();
        if(o -> ch[1]) o -> ch[1] -> pushdown();
        int d = o -> cmp1(k);
        if(d == 1 && o -> ch[0]) k = k - o -> ch[0] -> s - 1;
        else if(d == 1) k --;
        if(d != -1){
            Node* w = o -> ch[d];
            int d2 = w -> cmp1(k);
            int k2 = k;
            if(d2 == 1){
                k2 --;
                if(w -> ch[0]) k2 -= w -> ch[0] -> s;
            }
            if(d2 != -1){
                splay(w -> ch[d2], k2);
                if(d == d2) rotate(o, d ^ 1);
                else rotate(o -> ch[d], d);
            }
            rotate(o, d ^ 1);
        }
        return;
    }
    Node* merge(Node *left, Node *right){
        if(left == NULL) return right;
        if(right == NULL) return left;
        splay(left, left -> s);
        left -> ch[1] = right;
        left -> maintain();
        return left;
    }
    void split(Node* &o, int k, Node* &left, Node* &right){
        if(k == 0){
            left = NULL;
            right = o;
            return;
        }
        splay(o, k);
        left = o;
        right = o -> ch[1];
        left -> ch[1] = NULL;
        left -> maintain();
        return;
    }
    void flip(int l, int r){
        Node *left, *mid, *right;
        split(root, l - 1, left, mid);
        split(mid, r - l + 1, mid, right);
        swap(mid -> ch[0], mid -> ch[1]);
        mid -> f ^= 1;
        root = merge(left, merge(mid, right));
    }
    int value(int l){
        Node *left, *mid, *right;
        split(root, l - 1, left, mid);
        split(mid, 1, mid, right);
        int ret = mid -> v;
        root = merge(left, merge(mid, right));
        return ret;
    }
    void insert(Node* &o, int l, int r){
        if(r < l) return;
        int mid = (l + r) / 2;
        o = &ft[tot]; tot ++;
        o -> ch[0] = o -> ch[1] = NULL;
        o -> v = mid;
        o -> f = 0;
        if(l != r){
            insert(o -> ch[0], l, mid - 1);
            insert(o -> ch[1], mid + 1, r);
        }
        o -> maintain();
        return;
    }
} wt;
int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i ++) wt.a[i] = i;
    wt.insert(wt.root, 1, n);
    for(int i = 1; i <= m; i ++){
        int l, r;
        scanf("%d%d", &l, &r);
        wt.flip(l, r);
    }
    for(int i = 1; i <= n; i ++){
        printf("%d ", wt.value(i));
    }
    return 0;
}

BZOJ3223——Tyvj 1729 文艺平衡树的更多相关文章

  1. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  2. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  3. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2202  Solved: 1226[Submit][Sta ...

  4. 2018.08.05 bzoj3223: Tyvj 1729 文艺平衡树(非旋treap)

    传送门 经典的平衡树问题,之前已经用splay写过一次了,今天我突发奇想,写了一发非旋treap的版本,发现挺好写的(虽然跑不过splay). 代码: #include<bits/stdc++. ...

  5. bzoj3223: Tyvj 1729 文艺平衡树 splay裸题

    splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...

  6. 【Splay】bzoj3223 Tyvj 1729 文艺平衡树

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...

  7. BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap

    一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...

  8. 【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树

    让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using nam ...

  9. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

随机推荐

  1. 忘记常访问网站密码怎么办?教你如何查看浏览器已保存的密码,如何简单查看Chome浏览器保存的密码?

    利用场景: 同事或朋友外出有事,电脑未锁屏离开座位.可以利用这一间隙,查看Ta在Chrome浏览器上保存的账号密码 查看逻辑: 当我们要查看Chrome浏览器上保存的密码时,点击显示,会弹出一个对话框 ...

  2. 利用WireShark进行DNS协议分析

    一.准备工作 系统是Windows 8.1Pro 分析工具是WireShark1.10.8 Stable Version 使用系统Ping命令发送ICMP报文. 二.开始工作 打开CMD.exe键入: ...

  3. C#------DateTime自定义格式

    var text = Convert.ToString(DateTime.Now.ToString("yyyy/MM/dd"));

  4. mvc-1

  5. CodeForces 716A Crazy Computer

    题目链接:http://codeforces.com/problemset/problem/716/A 题目大意: 输入 n c, 第二行 n 个整数,c表示时间间隔 秒. 每个整数代表是第几秒.如果 ...

  6. Http请求之--C#的HttpWebRequest实现POST方式请求

    1.添加头信息和请求方法.有两种方式添加             req = (HttpWebRequest)WebRequest.Create("http://zhidao.baidu.c ...

  7. zepto.js的事件处理

    能够深入理解zepto对事件的处理,那么整个JS的事件处理就应该差不多合格了,事件处理是JS语言的一个难点. 1. 首先来看$.event函数. JS中有很多事件,都是已经定义好了,我们直接调用就可以 ...

  8. 总结CSS面试题目的考察点及常见布局问题整理

    整理网上流传的若干份面试题目,突发奇想,总结关于CSS面试题目的考察点,发现问题大多围绕几个属性和几种题目,水平有限,仅供参考. 写这个博文内心有种莫名奇妙的自我谴责感,实在不应该把面试层叠样式“应试 ...

  9. 配置git密钥,然后新建仓库

    Generating SSH keys (打开下面的链接) https://help.github.com/articles/generating-ssh-keys/ 完成配置后 开始在github上 ...

  10. document.body.scrollTop用法

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...