bzoj3223 文艺平衡树 (treap or splay分裂+合并)
3223: Tyvj 1729 文艺平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 3313 Solved: 1883
[Submit][Status][Discuss]
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
Source
RunID
User | Problem | Result | Memory | Time | Language | Code_Length | Submit_Time | |
1538282 | ksq2013 | 3223 | Accepted | 5396 kb | 2576 ms | C++/Edit | 2878 B | 2016-07-08 20:57:17 |
区间翻转裸题,废话不多说,直接放代码:
- #include<cstdio>
- #include<iostream>
- #define INF 0x3f3f3f3f
- #define Key_value ch[ch[root][1]][0]
- using namespace std;
- bool rev[];
- int n,m,root,tot,size[],key[],pre[],ch[][];
- void NewNode(int &x,int father,int val)
- {
- x=++tot;
- size[x]=;
- key[x]=val;
- pre[x]=father;
- }
- void Update_Rev(int x)
- {
- rev[x]^=;
- swap(ch[x][],ch[x][]);
- }
- void Push_Up(int x)
- {
- size[x]=size[ch[x][]]+size[ch[x][]]+;
- }
- void Push_Down(int x)
- {
- if(rev[x]){
- Update_Rev(ch[x][]);
- Update_Rev(ch[x][]);
- rev[x]=;
- }
- }
- void build(int &x,int father,int l,int r)
- {
- if(l>r)return;
- int mid=(l+r)>>;
- NewNode(x,father,mid);
- build(ch[x][],x,l,mid-);
- build(ch[x][],x,mid+,r);
- Push_Up(x);
- }
- void Init()
- {
- NewNode(root,,INF);
- NewNode(ch[root][],root,INF);
- build(Key_value,ch[root][],,n);
- }
- int Get_Kth(int x,int k)
- {
- Push_Down(x);//mistaken codes;
- int t=size[ch[x][]]+;
- if(t==k)return x;
- if(t>k)return Get_Kth(ch[x][],k);
- return Get_Kth(ch[x][],k-t);
- }
- void Rotate(int x,int kind)
- {
- int y=pre[x];
- Push_Down(y);Push_Down(x);
- ch[y][!kind]=ch[x][kind];
- pre[ch[x][kind]]=y;
- if(pre[y])ch[pre[y]][ch[pre[y]][]==y]=x;//unfixed;
- pre[x]=pre[y];
- ch[x][kind]=y;
- pre[y]=x;
- Push_Up(y);
- }
- void Splay(int x,int goal)
- {
- Push_Down(x);
- while(pre[x]!=goal){
- if(pre[pre[x]]==goal){
- Push_Down(pre[x]);
- Push_Down(x);
- Rotate(x,ch[pre[x]][]==x);
- }
- else{
- int y=pre[x];
- int kind=ch[pre[y]][]==y;
- Push_Down(pre[y]);//mistaken codes&&一遇到向下的操作就Push_Down;
- Push_Down(y);//mistaken codes
- Push_Down(x);//mistaken codes
- if(ch[y][kind]==x){
- Rotate(x,!kind);
- Rotate(x,kind);
- }
- else{
- Rotate(y,kind);
- Rotate(x,kind);
- }
- }
- }
- Push_Up(x);
- if(goal==)root=x;
- }
- void Rev(int l,int r)
- {
- Splay(Get_Kth(root,l),);
- Splay(Get_Kth(root,r+),root);
- Update_Rev(Key_value);
- Push_Up(ch[root][]);
- Push_Up(root);
- }
- void dfs(int x)
- {
- if(!x)return;
- Push_Down(x);
- dfs(ch[x][]);
- if(<=key[x]&&key[x]<=n)printf("%d ",key[x]);
- dfs(ch[x][]);
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- Init();
- for(int x,y;m;m--){
- scanf("%d%d",&x,&y);
- Rev(x,y);
- }dfs(root);
- return ;
- }
新splay模板上线,更简洁
- #include<stdio.h>
- #include<stdlib.h>
- #include<limits.h>
- inline int Rin(){
- int x=,c=getchar(),f=;
- for(;c<||c>;c=getchar())
- if(!(c^))f=-;
- for(;c>&&c<;c=getchar())
- x=(x<<)+(x<<)+c-;
- return x*f;
- }
- struct node{
- node*ch[];
- int r,s,v;bool b;
- node (int v,node*k):v(v){
- r=rand();
- b=;s=;
- ch[]=ch[]=k;
- }
- void exc(node*&x,node*&y)
- {node*k=y;y=x;x=k;}
- void pu(){s=ch[]->s+ch[]->s+;}
- void pd(){
- if(b){
- b=;
- exc(ch[],ch[]);
- ch[]->b^=;
- ch[]->b^=;
- }
- }
- }*rt,*bf;
- inline void rot(node*&o,int d){
- node*k=o->ch[d^];o->ch[d^]=k->ch[d];k->ch[d]=o;
- o->pu();k->pu();o=k;
- }
- inline int cmk(node*o,int k){
- if(o->ch[]->s+==k)return -;
- if(o->ch[]->s>=k)return ;
- return ;
- }
- inline void ins(node*&o,int x){
- if(o==bf){o=new node(x,bf);return;}
- ins(o->ch[],x);
- o->ch[]->r>o->r?rot(o,):o->pu();
- }
- inline void splay(node*&o,int k){
- if(o==bf)return;
- o->pd();int d=cmk(o,k);
- if(!(d^))k-=o->ch[]->s+;
- if(d!=-&&o->ch[d]!=bf){
- node*p=o->ch[d];p->pd();
- int dd=cmk(p,k);
- if(dd!=-&&p->ch[dd]!=bf){
- int kk=(dd?k-p->ch[]->s-:k);
- splay(p->ch[dd],kk);
- d^dd?rot(o->ch[d],d):rot(o,d^);
- }
- rot(o,d^);
- }
- }
- inline node*mrg(node*l,node*r){
- splay(l,l->s);l->ch[]=r;
- l->pu();return l;
- }
- inline void slt(node*o,int k,node*&l,node*&r){
- if(!k){l=bf;r=o;return;}
- if(!(k^o->s)){l=o;r=bf;return;}
- splay(o,k);l=o;r=o->ch[];
- o->ch[]=bf;l->pu();
- }
- inline void ini(){
- bf=new node(,);
- bf->r=INT_MAX;bf->s=bf->v=;
- bf->ch[]=bf->ch[]=bf;rt=bf;
- ins(rt,);rt->r=-;
- }
- void opt(node*o){
- if(o==bf)return;
- o->pd();
- opt(o->ch[]);
- if(o->r!=-)printf("%d ",o->v);
- opt(o->ch[]);
- }
- int n,m;
- int main(){
- n=Rin(),m=Rin();ini();
- for(int i=;i<=n;i++)ins(rt,i);
- while(m--){
- int l=Rin(),r=Rin();
- node *ll,*rr,*mm;
- slt(rt,l,ll,rr);
- slt(rr,r-l+,mm,rr);
- mm->b^=;
- rt=mrg(mrg(ll,mm),rr);
- }
- opt(rt);
- return ;
- }
bzoj3223 文艺平衡树 (treap or splay分裂+合并)的更多相关文章
- BZOJ3223文艺平衡树——非旋转treap
此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...
- [BZOJ3223]文艺平衡树 无旋Treap
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- 文艺平衡树(区间splay)
文艺平衡树(luogu) Description 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列. 其中需要提供以下操作:翻转一个区间,例如原有序序列是 5\ 4\ 3\ 2\ ...
- JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树
http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...
- [luogu3391][bzoj3223]文艺平衡树【splay】
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 分析 ...
- [bzoj3223]文艺平衡树(splay区间反转模板)
解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- [bzoj3223]文艺平衡树——splay
题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...
随机推荐
- CoreDataManager-OC版-兼容iOS10以前的版本
头文件: #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> /** CoreData管理器 */ ...
- 基于SolrCloud的内容搜索和热点推送
➠更多技术干货请戳:听云博客 什么是热点 我认为热点有时效性和受众面 用户关注从低到高再到低的内容 .有公共热点和分类热点.例如医辽养老全民关注,科技汽车等只有特定的人群关注. 推送的条件 搜索频次达 ...
- c中的关键字、标识符、注释
一. 学习语法之前的提醒 1) C语言属于一门高级语言,其实,所有高级语言的基本语法组成部分都是一样的,只是表现形式不太一样 2) 就好像亚洲人和非洲人,大家都有人类的结构:2只 手.2只脚.1个头, ...
- myIsEqualToString
BOOL myisEqualToString(NSString * str1 , NSString * str2){ //1.如果两个字符串,指针地址相等,就说明一定是相等 if(str1 == st ...
- GIT在iOS开发中的使用
前言 在iOS开发中,很多公司对项目的版本控制管理都使用了git,当然也有部分公司使用的是svn.当年我最初接触的是svn,觉得使用起来挺方便的,但是每次切分支都需要下载一份新的代码起来,这实在太麻烦 ...
- .net C# 图片转Base64 Base64转图片
//图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...
- Sql Server之旅——终点站 nolock引发的三级事件的一些思考
曾今有件事情让我记忆犹新,那年刚来携程不久,马上就被安排写一个接口,供企鹅公司调用他们员工的差旅信息,然后我就三下五除二的给写好 了,上线之后,大概过了一个月...DBA那边报告数据库出现大量锁超时, ...
- 0013 Java学习笔记-面向对象-static、静态变量、静态方法、静态块、单例类
static可以修饰哪些成员 成员变量---可以修饰 构造方法---不可以 方法---可以修饰 初始化块---可以修饰 内部类(包括接口.枚举)---可以修饰 总的来说:静态成员不能访问非静态成员 静 ...
- 好用的排名函数~ROW_NUMBER(),RANK(),DENSE_RANK() 三兄弟
排名函数三兄弟,一看名字就知道,都是为了排名而生!但是各自有各自的特色!以下一个例子说明问题!(以下栗子没有使用Partition By 的关键字,整个结果集进行排序) RANK 每个值一个排名,同样 ...
- MySQL 调优基础(一) CPU与进程
一般而言,MySQL 的调优可以分为两个层面,一个是在MySQL层面上进行的调优,比如SQL改写,索引的添加,MySQL各种参数的配置:另一个层面是从操作系统的层面和硬件的层面来进行调优.操作系统的层 ...