uva11922splay
题意:一个值1到n的数组,一种(多次)操作把l到r的区间反转,然后放到数组尾部
题解:裸的splay,用区间合并和区间分割,反转用lazy标记+pushdown就好了
#include<bits/stdc++.h>
#include<ext/rope>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct Node{
Node* ch[];
int v;
int s;
int flip;
int cmp(int x)const{
int d = x - ch[]->s;
if(d==)return -;
return d<= ? :;
}
void maintain()
{
s = + ch[]->s + ch[]->s;
}
void pushdown()
{
if(flip)//类似于线段树的lazy标记
{
flip=;
swap(ch[],ch[]);
ch[]->flip = !(ch[]->flip);
ch[]->flip = !(ch[]->flip);
}
}
};
Node* null = new Node();
void Rotate(Node* &o,int d)
{
Node* k = o->ch[d^];
o->ch[d^] = k->ch[d];
k->ch[d] = o;
o->maintain();k->maintain();
o = k;
}
void splay(Node* &o,int k)
{
o->pushdown();
int d = o->cmp(k);
if(d==)k -= o->ch[]->s + ;//利用二叉树性质
if(d!=-)
{
Node* p = o->ch[d];
p->pushdown();
int d2 = p->cmp(k);
int k2 = (d2== ? k:k-p->ch[]->s-);
if(d2!=-)
{
splay(p->ch[d2],k2);
if(d==d2)Rotate(o,d^);
else Rotate(o->ch[d],d);
}
Rotate(o,d^);
}
}
Node* Merge(Node* left,Node* right)
{
splay(left,left->s);//把排名最大的数splay到根
left->ch[] = right;
left->maintain();
return left;
}
void split(Node* o,int k,Node* &left,Node* &right)
{
splay(o,k);//把排名为k的节点splay到根,右侧子树所有节点排名比k大,左侧小
right = o->ch[];
o->ch[] = null;
left = o;
left->maintain();
}
struct SplayTree{
int n;
Node seq[N];
Node* root;
Node* build(int sz)
{
if(sz==)return null;
Node* l=build(sz/);
Node* o=&seq[++n];
o->v=n;
o->ch[]=l;
o->ch[]=build(sz-sz/-);
o->s = o->flip = ;
o->maintain();
return o;
}
void init(int sz)
{
n=;
null->s = ;
root = build(sz);
}
};
vector<int>ans;
void print(Node* o)
{
if(o!=null)
{
o->pushdown();
print(o->ch[]);
ans.pb(o->v);
print(o->ch[]);
}
}
void debug(Node* o)
{
if(o!=null)
{
o->pushdown();
debug(o->ch[]);
cout<<o->v<<endl;
debug(o->ch[]);
}
}
SplayTree ss;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
ss.init(n+);
// debug(ss.root);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
Node *o,*left,*mid,*right;
split(ss.root,a,left,o);//把ab整体右移一位,保证不会出现0
split(o,b-a+,mid,right);
mid->flip^=;
//把left+mid+right变成left+right+mid(fliped)
ss.root = Merge(Merge(left,right),mid);
}
print(ss.root);
for(int i=;i<ans.size();i++)
printf("%d\n",ans[i]-);
return ;
}
/************ ************/
uva11922splay的更多相关文章
随机推荐
- MySQL中的索引提示Index Hint
MySQL数据库支持索引提示(INDEX HINT)显式的高速优化器使用了哪个索引.以下是可能需要用到INDEX HINT的情况 a)MySQL数据库的优化器错误的选择了某个索引,导致SQL运行很慢. ...
- 0x08 MySQL 超详细-索引原理&慢查询优化【转-多图】(重点)
Content From——Egon's Blog http://www.cnblogs.com/linhaifeng/articles/7274563.html#top 0x01 介绍 为何要有索引 ...
- Python 学习之旅
流程图 第一章 Python简介 第二章 Python基础 第三章 流程控制 第四章 字符编码 第五章 文件处理 第六章 函数 第七章 模块与包 第八章 面向对象 第九章 异常处理 第十章 ...
- pycharm 用鼠标滚动调整字体大小
- What's the difference between 'Even if' and 'Even though'?
为一个英语词组语法写一篇Blog: even if 与 even though 这两个词组大致意思相当,但强调的侧重有所不同. even if与even though的区别: even if一般引导的 ...
- Js onload 解析
Js onload的使用方法. 1.在script中调用 window.onload = function(){ function1(); function2(); function3(); }; 或 ...
- yii2-lock-form 也许这就是你想要的,阻止表单多次提交
是不是被用户的行为所困扰? 一个表单用户点击提交按钮了N次,这也导致了数据提交了N次. 为了此受到了测试的欺辱,受到了老板的批评? 不用怕,它就是来拯救你的. 第一步:打开命令行,敲入 compose ...
- HTML+CSS理解
HTML+CSS1.对WEB标准以及W3C的理解与认识标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外 链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户所 ...
- 树莓派连接DHT11温湿度传感器(python)
介绍 DHT11作为一个廉价配件,同时包含了温度.湿度传感器,而且,编码使用也非常简单. 本文介绍如果在树莓派中使用 DHT11,代码是Python.如果有任何疑问,欢迎在下面留言. 接线 VCC接5 ...
- HTML学习笔记(上)
1. HTML介绍 1.1 什么是HTML HyperText Markup Language,超文本标记语言.简单来说,HTML文件本质上就是一个文本文件,但是这个文本文件是带有标签的. 不同的标签 ...