Splay(区间翻转) 模板
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=;
int n,m,tp,root,CNT;
int key[MAXN],lz[MAXN],fa[MAXN],sz[MAXN],val[MAXN],q[MAXN],Q[MAXN],ch[MAXN][];
void update(int k){ sz[k]=sz[ch[k][]]+sz[ch[k][]]+; }
void rotate(int x,int &y)
{
int old=fa[x],oldf=fa[old],op=ch[old][]==x;
if(old==y) y=x;
else ch[oldf][ch[oldf][]==old]=x;
fa[x]=oldf;
fa[ch[x][op^]]=old; ch[old][op]=ch[x][op^];
fa[old]=x; ch[x][op^]=old;
update(old); update(x);
}
void down(int x)
{
if(lz[x])
{
lz[x]^=; lz[ch[x][]]^=; lz[ch[x][]]^=;
swap(ch[x][],ch[x][]);
}
}
void splay(int x,int &y)
{
int now=x,old,oldf;Q[++tp]=now;
while(now!=y) Q[++tp]=fa[now] , now=fa[now];
while(tp--) down(Q[tp]);
while(x!=y)
{
old=fa[x],oldf=fa[old];
if(old!=y){
if((ch[old][]==x)^(ch[oldf][]==old)) rotate(x,y);
else rotate(old,y);
}
rotate(x,y);
}
}
void Build(int &k,int ll,int rr,int FA)
{
int mid=(ll+rr)/;
k=++CNT; fa[k]=FA; key[k]=val[mid];
if(mid>ll) Build(ch[k][],ll,mid-,k);
if(mid<rr) Build(ch[k][],mid+,rr,k);
update(k);
}
int findx(int x)//注意下放。
{
int now=root;
while()
{
down(now);
if(x<=sz[ch[now][]]) now=ch[now][];
else
{
x-=sz[ch[now][]]+;
if(x==) return now;
else now=ch[now][];
}
}
}
void rev(int L,int R)
{
int ll=findx(L-),rr=findx(R+);
splay(ll,root);
splay(rr,ch[root][]);
lz[ch[ch[root][]][]]^=;
}
void dfs(int u)
{
down(u);
if(ch[u][]) dfs(ch[u][]);
if(key[u]!=&&key[u]!=n+) printf("%d ",key[u]);
if(ch[u][]) dfs(ch[u][]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)val[i]=i;
Build(root,,n+,);
for(int i=,L,R;i<=m;i++)
{
scanf("%d%d",&L,&R);
L++; R++; rev(L,R);
}
dfs(root); puts("");
return ;
}
Splay(区间翻转) 模板的更多相关文章
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- splay区间翻转
原题P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: ...
- hdu-1890-Robotic Sort splay区间翻转
题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...
- 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区间翻转
思路: 区间翻转的裸题 终于tm理解splay了-- //By SiriusRen #include <cstdio> #include <cstring> #include ...
- Splay(区间翻转)&树套树(Splay+线段树,90分)
study from: https://tiger0132.blog.luogu.org/slay-notes P3369 [模板]普通平衡树 #include <cstdio> #inc ...
- [bzoj3223]文艺平衡树(splay区间反转模板)
解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- 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 ...
随机推荐
- EasyDarwin流媒体云平台:EasyCamera开源摄像机接入海康威视摄像机实时视频
本文转自EasyDarwin团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52755298 EasyCamera接收云平台实时 ...
- 用live555将内网摄像机视频推送到外网服务器,附源码
最近很多人问,如何将内网的摄像机流媒体数据发布到公网,如果用公网与局域网间的端口映射方式太过麻烦,一个摄像机要做一组映射,而且不是每一个局域网都是有固定ip地址,即使外网主机配置好了每一个摄像机的映射 ...
- leetcode题目解答报告(2)
Pascal's Triangle 题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- Hibernate ManyToOne Mappings 多对一关联映射
Hibernate ManyToOne Mappings 多对一关联映射 Hibernate框架的使用步骤: 1.创建Hibernate的配置文件(hibernate.cfg.xml)2.创建持久化类 ...
- Virtualbox虚拟机安装RouterOS
一,下载 vdi文件https://mikrotik.com/download Cloud Hosted Routerhttps://download.mikrotik.com/routeros/6. ...
- iOS开发过程中 xcode文件与Finder中文件保持一致 + 支付宝集成出错
目录 环境 前言 1.使用 Gem 安装 synx 2.直接在终端 Terminal 中开始使用 3.在使用的时候还可以加参数来实现不同的功能 4.解决项目中出现的一些 error 环境 OS X 1 ...
- Protocol_BGP
BGP协议 作者:Danbo 2015-7-8 BPG最重要的就是属性,下面我们针对路径属性分析一下.
- POJ1006 Biorhythms —— 中国剩余定理
题目链接:https://vjudge.net/problem/POJ-1006 Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total ...
- html5--5-6 绘制圆/弧
html5--5-6 绘制圆/弧 学习要点 掌握arc() 方法创建圆弧/曲线(用于创建圆或部分圆) 矩形的绘制方法 rect(x,y,w,h)创建一个矩形 strokeRect(x,y,w,hx,y ...
- java.lang.Exception: Socket bind failed: [730048] 端口被占用
错误提示如下: org.apache.coyote.http11.Http11AprProtocol init 严重: Error initializing endpoint java.lang.Ex ...