[BZOJ3223]文艺平衡树 无旋Treap
3223: Tyvj 1729 文艺平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
Input
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
Output
输出一行n个数字,表示原始序列经过m次变换后的结果
Sample Input
1 3
1 3
1 4
Sample Output
HINT
N,M<=100000
题解:
我为什么先做了维修数列再来做这道题……
本题是我之前一篇博文[您有新的未分配科技点] 无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )的例题的其中一个操作内容(翻转),
如果想看讲解的话,上面的链接有详细讲解,这里不再赘述,仅附上代码:
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
const int N=;
int n;
struct Treap
{
Treap *ch[];
int val,key,flip,size;
Treap(){size=flip=val=;key=rand();ch[]=ch[]=NULL;}
inline void update()
{size=ch[]->size++ch[]->size;}
}*null=new Treap(),*root=null,*stack[N],*x,*last;
typedef pair<Treap*,Treap*> D;
inline Treap* newTreap(int val)
{
Treap *o=new Treap();
o->size=;o->val=val;
o->ch[]=o->ch[]=null;
return o;
}
inline void pushdown_flip(Treap *o)
{
if(o==null)return;
if(o->flip)
{
o->flip^=;
if(o->ch[]!=null)o->ch[]->flip^=;
if(o->ch[]!=null)o->ch[]->flip^=;
swap(o->ch[],o->ch[]);
}
}
Treap* merge(Treap *a,Treap *b)
{
if(a==null)return b;
if(b==null)return a;
pushdown_flip(a);pushdown_flip(b);
if(a->key < b->key)
{a->ch[]=merge(a->ch[],b);a->update();return a;}
else
{b->ch[]=merge(a,b->ch[]);b->update();return b;}
}
D split(Treap *o,int k)
{
if(o==null)return D(null,null);
pushdown_flip(o);D y;
if(o->ch[]->size>=k)
{y=split(o->ch[],k);o->ch[]=y.second;o->update();y.second=o;}
else
{y=split(o->ch[],k-o->ch[]->size-);o->ch[]=y.first;o->update();y.first=o;}
return y;
}
inline Treap* build()
{
int p=;
for(int i=;i<=n;i++)
{
x=newTreap(i);last=null;
while(p&&stack[p]->key > x->key)
{stack[p]->update();last=stack[p];stack[p--]=null;}
if(p)stack[p]->ch[]=x;
x->ch[]=last;stack[++p]=x;
}
while(p)stack[p--]->update();
return stack[];
}
inline void dfs(Treap *o)
{
if(o==null)return;
pushdown_flip(o);
if(o->ch[]!=null)dfs(o->ch[]);
printf("%d ",o->val);
if(o->ch[]!=null)dfs(o->ch[]);
}
inline void reserve()
{
int l,r;scanf("%d%d",&l,&r);
D x=split(root,r);
D y=split(x.first,l-);
if(y.second!=null)y.second->flip^=;
root=merge(merge(y.first,y.second),x.second);
}
int main()
{
int m;scanf("%d%d",&n,&m);
root=build();
while(m--)reserve();
dfs(root);
}
[BZOJ3223]文艺平衡树 无旋Treap的更多相关文章
- BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap
一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ3678 wangxz与OJ (平衡树 无旋treap)
题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...
- BZOJ3223文艺平衡树——非旋转treap
此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...
- 无旋Treap - BZOJ1014火星人 & 可持久化版文艺平衡树
!前置技能&概念! 二叉搜索树 一棵二叉树,对于任意子树,满足左子树中的任意节点对应元素小于根的对应元素,右子树中的任意节点对应元素大于根对应元素.换言之,就是满足中序遍历为依次访问节点对应元 ...
- [Bzoj3223][Tyvj1729] 文艺平衡树(splay/无旋Treap)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3223 平衡树处理区间问题的入门题目,普通平衡树那道题在维护平衡树上是以每个数的值作为维护 ...
- BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)
题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...
- 洛谷 - P3391 【模板】文艺平衡树(Splay) - 无旋Treap
https://www.luogu.org/problem/P3391 使用无旋Treap维护序列,注意的是按顺序插入的序列,所以Insert实际上简化成直接root和Merge合并,但是假如要在序列 ...
- [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...
随机推荐
- day 10 字典dict
添加 xxx[新的key] = value 删除 del xx[key] 修改 xxx[已存在的key] = new_value 查询 xxx.get(key) 1. dict 字典 #### lis ...
- NES像素风格的Raspberry
周末小实践,vue+树莓派+一言API 一直有个想法,让树莓派做后端,实现一个有趣的网络服务.可是,苦于不会前端,迟迟无法动手.最近由于工作任务需要研究了一下前端. 问过前端大佬们,个个都说你得用vu ...
- Nginx之一:Nginx的编译安装
一.Nginx简介 官方网址:http://nginx.org/ Nginx是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发工作最早从2002年开始, ...
- 【python 3.6】笨办法取出列表中的字典的value
#python 3.6 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'BH8ANK' x = {'RegionSet': [{' ...
- UUID.randomUUID()简单介绍
UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...
- ORA-28000: the account is locked 查哪个具体ip地址造成
查系统默认的策略,连续验证10次错误帐户即会被锁 SQL> select resource_name, limit from dba_profiles where profile='DEFAUL ...
- django的htpp请求之WSGIRequest
WSGIRequest对象 Django在接收到http请求之后,会根据http请求携带的参数以及报文信息创建一个WSGIRequest对象,并且作为视图函数第一个参数传给视图函数.这个参数就是dja ...
- 子元素设置margin-top后,父元素跟随下移的问题
子元素设置margin-top后,父元素跟随下移的问题 <!DOCTYPE html> <html lang="en"> <head> < ...
- 关于css文字的扩展
1.不换行: .title{ white-space:nowrap; text-overflow:ellipsis; } 2.超出变三点 .title{ white-space:nowrap; tex ...
- Longge's problem(欧拉函数应用)
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...