题目描述

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

分析

不懂splay可以看一下我的博客:【传送门】
这道题目就是用splay来实现区间反转的,这个东西听说好像是LCT用splay的原因,我也不清楚没有学过LCT。
很明显,我们这道题目维护的不是权值,而是区间的编号(虽然好像还是权值),那么翻转操作就是交换两个子树的儿子的关系,但是如果每一次都暴力翻转\(O(mlog^2n)\),就做一个懒标记。
如果一个区间被旋转了两次,那么很明显,这个区间又变回去了,那么我们就维护一个标记表示表示以下的区间是否被翻转过。
那么剩下来的答案其实就是二叉查找树的中序遍历(BST的性质)。

ac代码

#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define N 100005
using namespace std;
template <typename T>
inline void read(T &x) {
    x = 0; T fl = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {
        if (ch == '-') fl = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x *= fl;
}
struct Splay {
    int rt, tot;
    struct node {
        int ch[2], fa, val, sz, fg;
        void init(int nod, int ft) {
            fa = ft;
            ch[0] = ch[1] = 0;
            sz = 1;
            val = nod;
        }
    }tr[N << 1];
    Splay() {
        ms(tr, 0);
        rt = tot = 0;
    }
    void pushup(int nod) {
        tr[nod].sz = tr[tr[nod].ch[0]].sz + tr[tr[nod].ch[1]].sz + 1;
    }
    void pushdown(int nod) {
        if (!tr[nod].fg) return;
        tr[tr[nod].ch[0]].fg ^= 1;
        tr[tr[nod].ch[1]].fg ^= 1;
        tr[nod].fg = 0;
        swap(tr[nod].ch[0], tr[nod].ch[1]);
    }
    void rotate(int nod) {
        int fa = tr[nod].fa, gf = tr[fa].fa, k = tr[fa].ch[1] == nod;
        tr[gf].ch[tr[gf].ch[1] == fa] = nod;
        tr[nod].fa = gf;
        tr[fa].ch[k] = tr[nod].ch[k ^ 1];
        tr[tr[nod].ch[k ^ 1]].fa = fa;
        tr[nod].ch[k ^ 1] = fa;
        tr[fa].fa = nod;
        pushup(fa);
        pushup(nod);
    }
    void splay(int nod, int goal) {
        while (tr[nod].fa != goal) {
            int fa = tr[nod].fa, gf = tr[fa].fa;
            if (gf != goal) {
                if ((tr[gf].ch[0] == fa) ^ (tr[fa].ch[0] == nod)) rotate(nod);
                else rotate(fa);
            }
            rotate(nod);
        }
        if (goal == 0) rt = nod;
    }
    int kth(int k) {
        int u = rt;
        while (1) {
            pushdown(u);
            int lc = tr[u].ch[0];
            if (tr[lc].sz >= k) u = lc;
            else if (tr[lc].sz + 1 == k) return u;
            else k -= tr[lc].sz + 1, u = tr[u].ch[1];
        }
    }
    void insert(int x) {
        int u = rt, ft = 0;
        while (u) {
            ft = u;
            u = tr[u].ch[x > tr[u].val];
        }
        u = ++ tot;
        if (ft) tr[ft].ch[x > tr[ft].val] = u;
        tr[u].init(x, ft);
        splay(u, 0);
    }
    void solve(int l, int r) {
        l = kth(l);
        r = kth(r + 2);
        splay(l, 0);
        splay(r, l);
        tr[tr[tr[rt].ch[1]].ch[0]].fg ^= 1;
    }
}splay;
int n, m;
void dfs(int nod) {
    splay.pushdown(nod);
    if (splay.tr[nod].ch[0]) dfs(splay.tr[nod].ch[0]);
    if (splay.tr[nod].val >= 2 && splay.tr[nod].val <= n + 1) printf("%d ", splay.tr[nod].val - 1);
    if (splay.tr[nod].ch[1]) dfs(splay.tr[nod].ch[1]);
}
int main() {
    read(n); read(m);
    for (int i = 1; i <= n + 2; i ++) splay.insert(i);
    while (m --) {
        int l, r;
        read(l); read(r);
        splay.solve(l, r);
    }
    dfs(splay.rt);
    return 0;
}

[luogu3391][bzoj3223]文艺平衡树【splay】的更多相关文章

  1. BZOJ3223 文艺平衡树(splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  2. JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树

    http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...

  3. [bzoj3223]文艺平衡树(splay区间反转模板)

    解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  4. [bzoj3223]文艺平衡树——splay

    题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...

  5. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  6. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

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

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

  8. bzoj3223 文艺平衡树 (treap or splay分裂+合并)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3313  Solved: 1883 [Submit][S ...

  9. Tyvj P1729 文艺平衡树 Splay

    题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...

随机推荐

  1. Windows 下 Mysql8.0.12 的安装方法

    1. 之前在windows 上面安装了 mysql 5.6 还有 mysql 5.7 遇到了几个坑 , 最近想直接安装最新版的 mysql 8.0.12(较新) 发现还是有坑 跟之前的版本不一样 这里 ...

  2. 谷歌浏览器报错 Active resource loading counts reached to a per-frame

    Active resource loading counts reached to a per-frame limit while the tab is in background. Network ...

  3. yml中driver-class-name: com.mysql.jdbc.Driver 解析不到的问题

    当在idea中使用springboot的快捷创建方式时,选中了mysql 和jdbc 那么pom文件中会直接有 <dependency> <groupId>mysql</ ...

  4. 莫烦keras学习自修第二天【backend配置】

    keras的backend包括tensorflow和theano,tensorflow只能在macos和linux上运行,theano可以在windows,macos及linux上运行 1. 使用配置 ...

  5. Yii的数值比较验证器

    该验证器比对两个特定输入值之间的关系 是否与 operator 属性所指定的相同. compareAttribute:用于与原属性相比对的属性名称. 当该验证器被用于验证某目标属性时, 该属性会默认为 ...

  6. PHPWord插件详解

    一下载PHPWorld并配置项目 1.PHPWord框架文件如下: 二使用word模板并使用PHPWord生成doc文件 例如:源代码如下: <?php require_once '../PHP ...

  7. 面对AI

    面对AI,我们应该怎么做? 李开复博士的一段话: 1. 我们应该具有战略性思维,并以人工智能无法取代的工作为目标.我们应该致力于终身学习,更新我们的技能,了解新趋势,并寻找新机遇. 2. 我们应该鼓励 ...

  8. Oracle minus用法详解及应用实例

    本文转载:https://blog.csdn.net/jhon_03/article/details/78321937 Oracle minus用法 “minus”直接翻译为中文是“减”的意思,在Or ...

  9. PLSQL 错误问题:Datebase character set (AL32UTF-8) and Client character set (ZHS16GBK) are different.

    (解决不了,网上用的是Orecal,我用的只是客户端.) 网上找到解决方法 打开注册表(ctr+R,输入regedit),根据报错提示找到注册表位置,但本机是win10 64位系统,根据以上路径找不到 ...

  10. time模块 转换关系图

    import time t = time.time() #获取目前时间 t_struck = time.localtime(t) #time.gmtime() utc时区 t_str = time.s ...