[补档][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 ...
随机推荐
- .net 4.0 中的特性总结(二):默认参数、命名参数
1.在方法定义的时候为参数指定一个默认值.调用方法的时候既可以像平时那样传入参数,也可以直接跳过不传入,这样的话,就使用默认值传到方法里.例如: 2.在之前版本的C#中,方法定义的参数顺序必须与方法调 ...
- jsp图片上传
1.要实现图片上传,首先需要一个组件,这里我用的是smartupload.jar可以到这里下载http://download.csdn.net/detail/mengdecike/8279247 2. ...
- mybatis if test加筛选条件
最近在项目使用mybatis中碰到个问题 <if test="type=='y'"> and status = 0 </if> 当传入的type的值为y的时 ...
- 简单总结下关于blob的图片上传
我是从一本书上看到了Azure Blob,然后在网上浪了一会儿,发现了这篇文章,里面他已经把重点的则重地方讲完了,"飞机票:http://www.cnblogs.com/sparkdev/p ...
- 关于MATLAB处理大数据坐标文件
原先有3000条测试数据,MATLAB表现出来强大的数据处理能力,十几秒就可以把数据分类.分装并储存,这次共有10万条坐标数据,MATLAB明显后劲不足,显示内存不足 自我认识:以前MATLAB数据处 ...
- jquery获取当前选项的属性值a
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- cvCvtColor与cvtColor区别
用到了rgb转灰度图功能,查到两个函数,发现名字很像,功能也一样,但是参数类型不一样. 记录一下. 可以看声明,cvCvtColor是c语言风格接口. /* Converts input array ...
- 从ConcurrentHashMap的演进看Java多线程核心技术 Java进阶(六)
本文分析了HashMap的实现原理,以及resize可能引起死循环和Fast-fail等线程不安全行为.同时结合源码从数据结构,寻址方式,同步方式,计算size等角度分析了JDK 1.7和JDK 1. ...
- 我的第一篇博文:C++最初的路-经典的小游戏走迷宫
写在开始:这个博客建于大二下学期.2年多的学习,从网上借鉴的大牛经验,代码,指导数不胜数,而其中大部分来自别人的博客,于是期待有一天也能把自己在学习过程中的一些经验拿出来与大家分享. 其实我凝望了C+ ...
- 2017寒假零基础学习Python系列之函数之 定义默认参数
在定义函数时,可以有默认参数例如Python自带的函数int(),其实就是两个参数,我们既可以传一个参数,又可以传两个参数 int(') >>>123 int(',8) 83 int ...