BZOJ 3223 Splay区间翻转
思路:
区间翻转的裸题 终于tm理解splay了……
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 155555
int n,m,root,size[N],ch[N][2],fa[N],rev[N],v[N];
void push_up(int x){size[x]=size[ch[x][0]]+size[ch[x][1]]+1;}
int build(int l,int r){
if(l>r)return 0;
if(l==r){size[l]=1,v[l]=l-1;return l;}
int mid=(l+r)>>1;v[mid]=mid-1;
ch[mid][0]=build(l,mid-1);fa[ch[mid][0]]=mid;
ch[mid][1]=build(mid+1,r);fa[ch[mid][1]]=mid;
push_up(mid);
return mid;
}
void rotate(int p){
int q=fa[p],y=fa[q],f=(ch[q][1]==p);
ch[q][f]=ch[p][!f],fa[ch[q][f]]=q;
ch[p][!f]=q;fa[q]=p;fa[p]=y;
if(y){
if(ch[y][0]==q)ch[y][0]=p;
else ch[y][1]=p;
}push_up(q);
}
void Splay(int x,int tp){
for(int y;y=fa[x];rotate(x)){
if(y==tp)break;
if(fa[y]!=tp){
if((ch[y][0]==x)^(ch[fa[y]][0]==y))rotate(x);
else rotate(y);
}
}push_up(x);
if(!tp)root=x;
}
void push_down(int x){
if(rev[x]){
rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
rev[x]=0;
}
}
int find(int x,int num){
push_down(x);
int sz=size[ch[x][0]];
if(sz+1==num)return x;
else if(sz+1>num)return find(ch[x][0],num);
else return find(ch[x][1],num-sz-1);
}
void reverse(int l,int r){
int y=find(root,l);Splay(y,0);
Splay(find(root,r+2),y);
rev[ch[ch[root][1]][0]]^=1;
}
void dfs(int x){
push_down(x);
if(ch[x][0])dfs(ch[x][0]);
if(v[x]&&v[x]!=n+1)printf("%d ",v[x]);
if(ch[x][1])dfs(ch[x][1]);
}
int main(){
scanf("%d%d",&n,&m);
root=build(1,n+2);
for(int i=1;i<=m;i++){
int l,r;
scanf("%d%d",&l,&r);
reverse(l,r);
}
dfs(root);
}
BZOJ 3223 Splay区间翻转的更多相关文章
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- bzoj 3223 splay模板题3
水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...
- hdu-1890-Robotic Sort splay区间翻转
题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...
- splay区间翻转
原题P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: ...
- Splay(区间翻转)&树套树(Splay+线段树,90分)
study from: https://tiger0132.blog.luogu.org/slay-notes P3369 [模板]普通平衡树 #include <cstdio> #inc ...
- hdu1890 Robotic Sort (splay+区间翻转单点更新)
Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...
- 2019牛客国庆集训派对day1 K题 双向链表练习题 splay区间翻转
题目链接: 解法: 先建n颗平衡树,合并的时候将a中最右的结点翻转到根节点,b中最左的结点翻转到根节点,对合并后的根节点进行标记. #include <bits/stdc++.h> usi ...
- BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3223 [题目大意] 给出一数列,问m次区间翻转后的结果. [题解] Splay 区间翻 ...
随机推荐
- [Hyperapp] Interact with the State Object through Hyperapp Action functions
Hyperapp is an ultra lightweight (1kb), minimal, functional, JavaScript library for building UIs. It ...
- node17
一.复习 cookie是在res中设置,req中读取的.第一次的访问没有cookie. cookie的存储大小有限,kv对.对用户可见,用户可以禁用.清除Cookie.可以被篡改. cookie用来制 ...
- 通过setInterval函数在地图上每隔1s打一次点
<?php echo <<<_END <!doctype html> <html> <head> <meta charset=&quo ...
- Android 手势
GestureDetector:手势监听类,通常在View的setOnTouchListener方法中设置TouchListener,在TouchListener的onTouch函数中把MotionE ...
- jq弹窗(获取页面宽高,滚轮高度,始终居中)
jq写一个弹窗,效果如上图所示, 点击按钮弹窗弹出,右上角关闭. 弹窗始终显示在页面中间,无论放大缩小窗口,滚轮滚动. 代码如下: html: <br><br><br&g ...
- Android Handling back press when using fragments in Android
In MainActivity: getSupportFragmentManager().beginTransaction().replace(R.id.gif_contents, gifPageTw ...
- Ubuntu 14.04下安装CUDA8.0
配置环境如下: 系统:Ubuntu14.04 64bit 显卡:Nvidia K620M 显卡驱动:Nvidia-Linux-x86_64-375.66.run CUDA8.0 + cudnn8.0 ...
- mybastis_20190323
1 数据表 items.user.orders.orderdetail user id,username,birthday,sex,address; 使用原生态的jdbc的问题总结? 1 数据库链接问 ...
- php查询字符串是否存在 strpos
/*** 查询字符是否存在于某字符串** @param $haystack 字符串* @param $needle 要查找的字符* @return bool*/function str_exists( ...
- h5 input失去焦点软键盘把页面顶起
var broswer=localStorage.getItem('temp') //浏览器环境 var u = navigator.userAgent var isiOS = !!u.match(/ ...