题目链接

思路

splay区间操作的裸题。

假如要对l-r这段区间操作,那么就先把l-1伸展到根节点,然后把r +1伸展为根的儿子。这样r + 1的左儿子就是要操作的区间了。只要在上面打上标记,以后每次查询的时候pushdown一下。

然后对于l-1和r+1节点可能不存在,所以可以放两个标兵元素。就是一个极小值一个极大值(合适即可)。

对于这道题。只要每次修改的时候,按照上面的方法进行伸展。然后打标机即可。打标记的方法就是用0/1来表示当前区间是否被翻转,每次翻转就将标记^1即可。

代码

/*
* @Author: wxyww
* @Date: 2018-12-09 08:43:41
* @Last Modified time: 2018-12-09 11:00:52
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<bitset>
using namespace std;
#define ls TR[cur].ch[0]
#define rs TR[cur].ch[1]
typedef long long ll;
const int N = 100000 + 100;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int rt,tot,n,m;
struct node {
int val,ch[2],lazy,siz,pre;
}TR[N];
void pushup(int cur) {
TR[cur].siz = TR[ls].siz + TR[rs].siz + 1;
}
void pushdown(int cur) {
if(TR[cur].lazy) {
TR[ls].lazy ^= 1;
TR[rs].lazy ^= 1;
TR[cur].lazy ^= 1;
swap(ls,rs);
}
}
int build(int l,int r,int father) {
if(l > r) return 0;
int cur = (l + r) >> 1;
TR[cur].val = cur - 1;
TR[cur].pre = father;
TR[cur].siz = 1;
ls = build(l,cur - 1,cur);
rs = build(cur + 1,r,cur);
pushup(cur);
return cur;
}
int getwh(int cur) {
return TR[TR[cur].pre].ch[1] == cur;
}
void rotate(int cur) {
int fa = TR[cur].pre,gr = TR[fa].pre;
int f = getwh(cur);
TR[gr].ch[getwh(fa)] = cur;
TR[cur].pre = gr;
TR[fa].ch[f] = TR[cur].ch[f ^ 1];
TR[TR[cur].ch[f ^ 1]].pre = fa;
TR[fa].pre = cur;
TR[cur].ch[f ^ 1] = fa;
pushup(fa);
pushup(cur);
}
void splay(int cur,int to) {
while(TR[cur].pre != to) {
if(TR[TR[cur].pre].pre != to) {
if(getwh(TR[cur].pre) != getwh(cur)) rotate(TR[cur].pre);
else rotate(cur);
}
rotate(cur);
}
if(!to) rt = cur;
}
int kth(int now) {
int cur = rt;
while(cur) {
pushdown(cur);
if(now <= TR[ls].siz) cur = ls;
else if(now > TR[ls].siz + 1) now -= TR[ls].siz + 1,cur = rs;
else return cur;
}
}
void work(int l,int r) {
int k1 = kth(l),k2 = kth(r + 2);
splay(k1,0);
splay(k2,k1);
TR[TR[k2].ch[0]].lazy ^= 1;
pushdown(TR[k2].ch[0]);
}
void print(int cur) {
if(!cur) return;
pushdown(cur);
print(ls);
if(TR[cur].val != 0 && TR[cur].val != n + 1)
printf("%d ",TR[cur].val);
print(rs);
}
int main() {
n = read(),m = read();
rt = build(1,n + 2,0);
while(m--) {
int l = read(),r = read();
work(l,r);
}
print(rt);
return 0;
}

一言

有伤害人的人存在的话,也会有能抚慰伤痕的人

[luogu3391][文艺平衡树]的更多相关文章

  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. BZOJ 3223: Tyvj 1729 文艺平衡树

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

  4. 【Splay】bzoj3223-Tyvj1729文艺平衡树

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

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

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

  6. [bzoj3224]普通平衡树/3223文艺平衡树

    这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...

  7. tyvj 1729 文艺平衡树

    文艺平衡树 From admin 背景 Background 此为平衡树系列第二道:文艺平衡树 描述 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以 ...

  8. Tyvj P1729 文艺平衡树 Splay

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

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

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

随机推荐

  1. jackson使用问题:mapper.readValue()将JSON字符串转反序列化为对象失败或异常

    问题根源:转化目标实体类的属性要与被转JSON字符串总的字段 一 一对应!字符串里可以少字段,但绝对不能多字段. 先附上我这段出现了问题的源码: // 1.接收并转化相应的参数.需要在pom.xml中 ...

  2. C# 中那些常用的工具类(Utility Class)(三)

    今天来接着写这个系列的文章,这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候 ...

  3. 如何抓取电商的数据 & Python

    如何抓取电商的数据 & Python https://www.zhihu.com/question/40720286 https://www.zhihu.com/question/382455 ...

  4. 转载 --mysql函数大全

    控制流函数 IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境 ...

  5. Redux学习(2) ----- 异步和中间件

    Redux中间件,其实就是一个函数, 当我们发送一个action的时候,先经过它,我们就可以对action进行处理,然后再发送action到达reducer, 改变状态,这时我们就可以在中间件中,对a ...

  6. codeforces478C

    Table Decorations CodeForces - 478C 你有r个红的,g个绿的和b个蓝的气球.要为宴会布置一张桌子,你恰好需要三个气球.附在桌子上的三个气球不应该有相同的颜色.如果我们 ...

  7. Servlet篇 之 web服务器

    创建web项目,在web项目中创建html页面,然后把项目部署到web服务器里面,启动服务器之后,可以使用浏览器通过URL地址的方式,访问到web项目中的html页面了 Web服务器: 常用tomca ...

  8. .net core 2.0 Redis的基本使用

    存Session 先配置`appsetting.json`文件 "ConnectionStrings": { "Redis": "ip:6379,ab ...

  9. IDEA Failed to prepare an update: Temp directory inside installation

    具体错误: Connection Error Failed to prepare an update: Temp directory inside installation: F:\IDEA_Tool ...

  10. Json.net 反序列化 部分对象

    主要通过 Jobject获取想要序列化的部分对象. 直接上代码 static void Main(string[] args) { //先反序列化看看 string json = "{\&q ...