[补档][Tvyj 1729]文艺平衡树
[Tvyj 1729]文艺平衡树
题目
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1INPUT
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=nOUTPUT
输出一行n个数字,表示原始序列经过m次变换后的结果SAMPLE
INPUT
5 31 31 31 4OUTPUT
4 3 2 1 5
解题报告
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
inline int read(){
int sum();
char ch(getchar());
while(ch<''||ch>'')
ch=getchar();
while(ch>=''&&ch<=''){
sum=sum*+ch-'';
ch=getchar();
}
return sum;
}
int n,m,top;
struct node{
int v,key,size,mark;
node *ch[];
node(int x=):size(),key(rand()),mark(),v(x){
ch[]=ch[]=NULL;
}
inline void revs();
inline void pushup();
inline void pushdown(){
if(mark){
if(ch[])
ch[]->revs();
if(ch[])
ch[]->revs();
mark=;
}
}
}*root,*st[];
typedef pair<node*,node*>p;
inline void swp(node *x,node *y){
node *tmp(x);
x=y;
y=tmp;
}
inline int get_size(node *x){
if(x==NULL)
return ;
return x->size;
}
inline void node::revs(){
mark^=;
swap(ch[],ch[]);
}
inline void node::pushup(){
size=;
size+=get_size(ch[])+get_size(ch[]);
}
inline node* build(){
node *x,*las;
for(int i=;i<=n;i++){
x=new node(i);
las=NULL;
while(top&&st[top]->key>x->key){
st[top]->pushup();
las=st[top];
st[top--]=NULL;
}
if(top)
st[top]->ch[]=x;
x->ch[]=las;
st[++top]=x;
}
while(top)
st[top--]->pushup();
return st[];
}
inline node* merge(node *x,node *y){
if(x==NULL)
return y;
if(y==NULL)
return x;
if(x->key<y->key){
x->pushdown();
x->ch[]=merge(x->ch[],y);
x->pushup();
return x;
}
else{
y->pushdown();
y->ch[]=merge(x,y->ch[]);
y->pushup();
return y;
}
}
inline p split(node *x,int k){
if(!x)
return p(NULL,NULL);
p y;
x->pushdown();
if(get_size(x->ch[])>=k){
y=split(x->ch[],k);
x->ch[]=y.second;
x->pushup();
y.second=x;
}
else{
y=split(x->ch[],k-get_size(x->ch[])-);
x->ch[]=y.first;
x->pushup();
y.first=x;
}
return y;
}
inline int rk(node *rt,int x){
if(!rt)
return ;
return x<rt->v?rk(rt->ch[],x):rk(rt->ch[],x)+get_size(rt->ch[])+;
}
inline int kth(int k){
p x(split(root,k-)),y(split(x.second,));
node *tmp(y.first);
root=merge(merge(x.first,tmp),y.second);
return tmp->v;
}
inline void insert(int x){
int k(rk(root,x));
p tp(split(root,k));
node *tmp(new node(x));
root=merge(merge(tp.first,tmp),tp.second);
}
inline void print(node *x){
if(!x)
return;
x->pushdown();
print(x->ch[]);
printf("%d ",x->v);
print(x->ch[]);
}
inline int gg(){
srand(time(NULL));
n=read(),m=read();
root=build();
for(int i=;i<=m;i++){
int x(read()),y(read());
if(x==y)
continue;
p tmp1(split(root,x-)),tmp2(split(tmp1.second,y-x+));
tmp2.first->revs();
root=merge(tmp1.first,merge(tmp2.first,tmp2.second));
}
print(root);
}
int k(gg());
int main(){;}
[补档][Tvyj 1729]文艺平衡树的更多相关文章
- [BZOJ3223]Tyvj 1729 文艺平衡树
[BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
- 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1347 Solved: 724[Submit][Stat ...
- bzoj3223Tyvj 1729 文艺平衡树 splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5644 Solved: 3362[Submit][Sta ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
随机推荐
- jsp传到java的control层的方法
jsp传到java的control层的方法1.form表单 用<input type="submit">提交,提交到后台的参数在form表单内<form meth ...
- flask笔记一
最近学习flask,由于web开发方面接触的并不是很多,所以看官方文档有点焦头烂额,好多的概念不理解. <Flask web 开发>比较基础,先用这本书做个入门. 1.Flask实例化对象 ...
- 有关LinkedList常用方法的源码解析
上文里解析了有关ArrayList中的几个常用方法的源码——<有关ArrayList常用方法的源码解析>,本文将对LinkedList的常用方法做简要解析. LinkedList是基于链表 ...
- Administrator账户密码忘了怎么办
WIN7的Administrator账户密码忘了怎么办 1.准备一个带有winpe系统的系统光盘或者U盘 2.启动电脑,按F12选择U盘或者光盘启动,进入winpe操作系统 3.找到c:\wind ...
- C#—泛型_推迟一切可以推迟的东西
泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...
- Lua性能优化
原文:Lua Performance Tips 偶然找到<Lua Performance Tips>这篇关于Lua的优化文章,个人认为相较于多数泛泛而谈要好不少.尽管Lua已经到5.2版本 ...
- 9.session的生命周期
1.创建 当客户端第一次访问某个jsp或者Servlet的时候,服务器会为当前会话创建一个SessionId,每次客户端向服务端发送请求的时候,都会将此SessionId携带过去,服务端会对此Sess ...
- Java 获取字符串Hash值
Java 生成字符串的Hash值: /** * A hashing method that changes a string (like a URL) into a hash suitable for ...
- MAC 相关操作解析
MAC 相关操作解析 OS 显示桌面 f11 F1~F12 fn + F1~F12 撤销重做 command + z command + shift + z 图片预览 选择图片 空格 上下左右 svn ...
- Excel文件按照指定模板导入数据(用jxl.jar包)
本文中的方法只适合Excel2003,要读取Excel2007最好使用poi.jar,据说poi.jar还在更新,jxl.jar已经不更新了,处理Excel文件的读写问题最好还是学习poi.j ...