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开源流媒体音视频云平台遇到的奇葩问题:内网运行正常,公网流媒体不通
最近在帮助EasyDarwin的用户部署EasyNVR+EasyDarwin云平台+EasyClient方案的过程中,遇到一个问题,EasyNVR分布在用户各地区现场的内网中,EasyDarwin云平 ...
- VLC RTP Over TCP
在RTSP协议请求数据时,让VLC以TCP的方式获取服务器发来的RTP数据 不为别的,下次回复直接用博客链接就能回复大家了! 操作:工具 -> 首选项 然后: 搞定! ------------- ...
- JQery 动态填充数据到table 中
说明: 1.把数据库中的数据查询出来,填充到前台的table中,注意 从数据查询出来的 属性IsNew="0"(table 行tr的属性) 2.单击“添加”按钮 新添加行追加到ta ...
- SQL Server 2005中top关键字的用法
1.返回N条记录数 select top n * from <表名> [查询条件] 2.返回总结果集中指定百分比记录数 select top n percent * from <表名 ...
- Linux就该这么学--命令集合4(文件目录管理命令)
1.touch命令用于创建空白文件与修改文件时间:(touch [选项] [文件]) 对于在Linux中的文件有三种时间: 更改时间(mtime):内容修改时间(不包括权限的) 更改权限(ctime) ...
- mtk6589显示子系统笔记(一)
拿到MT6589的版本不久,发现显示系统代码结构改变很大.做些备忘,后续不忙的时候可以继续看. MT6589之前的MTK的Android系统显示系统同featurePhone基本一致. 先来回顾下MT ...
- codeforces B. Multitasking 解题报告
题目链接:http://codeforces.com/problemset/problem/384/B 题目意思:给出n个数组,每个数组包括m个数字,当k = 0 时,需要把n个数组都按照从小到大的顺 ...
- WebRTC学习
1. WebRTC学习 1.1 WebRTC现状 本人最早接触WebRTC是在2011年底,那时Google已经在Android源码中加入了webrtc源码,放在/external/web ...
- UVA-11892(组合游戏)
题意: 给n堆石子,每堆有ai个,每次可以取每堆中任意数目的石子;但是上一次操作的人没有将一堆全部取走,那么下一个人还要在那一堆取; 思路: 每次取到这堆就剩一个的策略; AC代码: #include ...
- hdu-4989 Summary(水题)
题目链接: Summary Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) P ...