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 ...
随机推荐
- ElasticSearch(十一)批量CURD bulk
1.bulk语法 POST /_bulk { "delete": { "_index": "test_index", "_type ...
- java之冒泡排序
//冒泡排序(Bubble Sorting)的基本思想是:通过对待排序序列从后向前(从下标较大的元素开始),依次比较相邻元素的排序码,若发现逆序则交换,使排序码较小的元素逐渐从后部移向前部(从下标较大 ...
- ubuntu下安装android模拟器genymotion【转】
本文转载自:http://www.jianshu.com/p/e6062ebb8fc9 去genymotion下载对应的安装包genymotion-2.4.0_x64.bin sudo ./genym ...
- 重学DSP:对于卷积的理解
最近,我发现自己对于一个事情,如果不给自己一个说服自己的理由,就会出现不能理解,不能记住,以至于不会使用或者“盲目”应用的情况. 但是,我学的这个学科就是应当建立在对信号作用过程的理解上面的. 下面, ...
- 从数据的角度带你深入了解IPFS
IPFS 和区块链有着非常紧密的联系, 随着区块链的不断发展,对数据的存储需求也越来越高.本文从IPFS 的底层设计出发, 结合源代码, 分析了IPFS 的一些技术细节. 一.概述 IPFS 和区块链 ...
- hdu 1361.Parencodings 解题报告
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence. P-se ...
- hibernate入门(-)
1.struts2的支持 在web.xml中配置struts2的支持 <?xml version="1.0" encoding="UTF-8"?> ...
- 多线程设计模式(一) Single Threaded Execution
这里有一座独木桥.因为桥身非常的细,一次只能允许一个人通过.当这个人没有下桥,另一个人就不能过桥.如果桥上同时又两个人,桥就会因为无法承重而破碎而掉落河里. 这就是Single Threaded Ex ...
- 1336 : Matrix Sum (hihocoder)
题目链接: 点击打开链接 二维树状数组,百度一大堆,我只是存代码的 #include<stdio.h> #include<iostream> #include<algor ...
- Java笔记(六)
IO流: 字符流和字节流: 字符流两个基类: InputStream OutputStream 字节流两个基类: Reader Writer FileWriter: import java.io.Fi ...