[Tvyj 1729]文艺平衡树

题目

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是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

5 3
1 3
1 3
1 4

OUTPUT

4 3 2 1 5

解题报告

板子题,Splay,fhq-Treap什么的,我半个都不会呢= =
上板子= =
 #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(){;}
代码极其漂(chou)亮(lou),请慢(gan)慢(jin)欣(tui)赏(chu)

[补档][Tvyj 1729]文艺平衡树的更多相关文章

  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. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

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

  5. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  6. 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1347  Solved: 724[Submit][Stat ...

  7. bzoj3223Tyvj 1729 文艺平衡树 splay

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

  8. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

  9. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

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

随机推荐

  1. ionic 的缓存 和局部刷新

    最近两天在做项目时,发现ionic的缓存功能非常方便好用,提高了再低端手机特别是android比较低版本上的流畅性!可是,后来发现,整体的缓存整个页面并不是一个一劳永逸的办法,结合局部刷新功能,感觉就 ...

  2. C#继承的执行顺序

    自己对多态中构造函数.函数重载执行顺序和过程一直有些不理解,经过测试,对其中的运行顺序有了一定的了解,希望对初学者有些帮助. eg1: public class A { public A() { Co ...

  3. Linux中的apache的服务命令

    1. 启动apachesudo service httpd start 2. 停止服务apachesudo service httpd stop 3. 重新启动apachesudo service h ...

  4. 【LeetCode】110. Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  5. 3.ubuntu如何安装搜狗输入法

    1.http://blog.csdn.net/qq_21792169/article/details/53152700 2.http://jingyan.baidu.com/article/54b6b ...

  6. 7.java的请求转发和请求重定向

    1.请求重定向:是客户端的行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次的请求对象不会保存,地址栏的URL地址会改变,一次新的转发. 2.请求转发:是服务器的行 ...

  7. 微信小程序开发者注册流程

    一,首先打开浏览器,搜索微信公众平台 点击进入,此时还没有注册微信小程序开发账号,我们需要点击注册 进入注册页面,会出现四种账号,我们选择小程序账号 然后根据提示就可以进行注册了 注册时,需填写一下个 ...

  8. Image和字节数组互转

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...

  9. ecshop中smarty比较操作符(eq,ne,neq)含义

    eq相等, ne.neq不相等, gt大于, lt小于, gte.ge大于等于, lte.le 小于等于, not非, mod求模.  is [not] div by是否能被某数整除, is [not ...

  10. KBEngine简单RPG-Demo源码解析(1)

    一:环境搭建1. 确保已经下载过KBEngine服务端引擎,如果没有下载请先下载          下载服务端源码(KBEngine):              https://github.com ...