splay 文艺平衡树 (数据结构)
题目大意:略
splay维护区间翻转裸题,为了减少不必要的麻烦,多插入两个点,分别是0和n+1
每次找区间的第K个值,就在splay上二分即可
顺便学了一下splay的完美建树,而且splay有一些小函数可以宏定义或者用inline,跑得飞快
最后跑一遍中序遍历即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100100
#define il inline
#define ll long long
#define root d[0].ch[1]
#define con(x,ff,p) d[x].fa=ff,d[ff].ch[p]=x
#define idf(x) d[d[x].fa].ch[0]==x?0:1
#define lb(x) (x&(-x))
using namespace std; int n,m,cnt;
struct SPLAY{
int fa,ch[],id,sum,mrk;
}d[N<<];
il void pushup(int x) {d[x].sum=d[d[x].ch[]].sum+d[d[x].ch[]].sum+;}
il void pushdown(int x)
{
if(!d[x].mrk) return;
swap(d[x].ch[],d[x].ch[]);
d[x].mrk=;
d[d[x].ch[]].mrk^=;
d[d[x].ch[]].mrk^=;
}
il void rot(int x)
{
int y=d[x].fa;int ff=d[y].fa;
int px=idf(x);int py=idf(y);
con(d[x].ch[px^],y,px);
con(y,x,px^);
con(x,ff,py);
pushup(y),pushup(x);
}
void splay(int x,int to)
{
to=d[to].fa;
int y,px,py;
while(d[x].fa!=to)
{
y=d[x].fa;
px=idf(y),py=idf(x);
if(d[y].fa==to) rot(x);
else if(py==px){
rot(y);
rot(x);
}else{
rot(x);
rot(x);
}
}
}
int Find(int w)
{
int x=root;
while(x)
{
pushdown(x);
if(d[d[x].ch[]].sum>=w)
{
x=d[x].ch[];
continue;
}
w-=d[d[x].ch[]].sum;
if(w==) return x;
w--,x=d[x].ch[];
}
return ;
}
int build(int ff,int l,int r)
{
if(l>r) return ;
int x=++cnt;
int mid=(l+r)>>;
d[x].id=mid-,d[x].fa=ff,d[x].sum=;
d[x].ch[]=build(x,l,mid-);
d[x].ch[]=build(x,mid+,r);
pushup(x);
return x;
}
void Print(int x)
{
pushdown(x);
if(d[x].ch[]) Print(d[x].ch[]);
if(d[x].id!=&&d[x].id!=n+) printf("%d ",d[x].id);
if(d[x].ch[]) Print(d[x].ch[]);
}
int main()
{
//freopen("testdata.in","r",stdin);
scanf("%d%d",&n,&m);
int x,y;
root=build(,,n+);
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
if(x==y) continue;
int xx=Find(x);
splay(xx,root);
int yy=Find(y+);
splay(yy,d[root].ch[]);
d[d[d[root].ch[]].ch[]].mrk^=;
}
Print(root);
return ;
}
splay 文艺平衡树 (数据结构)的更多相关文章
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- 【Splay】bzoj3223-Tyvj1729文艺平衡树
一.题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 ...
- bzoj3223 文艺平衡树 (treap or splay分裂+合并)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3313 Solved: 1883 [Submit][S ...
- Tyvj P1729 文艺平衡树 Splay
题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
- bzoj3223Tyvj 1729 文艺平衡树 splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5644 Solved: 3362[Submit][Sta ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
随机推荐
- WSDL详解(一)
WSDL文档使用web服务描述语言来定义服务. 文档包括逻辑(抽象)部分和具体部分. 抽象部分用于定义独立于实现的数据类型和消息,具体部分定义一个endpoint如何实现一个可以与外界进行交互的服务. ...
- php设置cookie和删除cookie
设置cookie Example : - set - <?php setcookie( "name", "value", "future_tim ...
- C# 常用字符串加密解密方法
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- 00072_System类
1.概念 (1)System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作: (2)System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象: (3)Syste ...
- poj 1611 简单并查集的应用
#include<stdio.h> #define N 31000 int pre[N]; int find(int x) { if(x!=pre[x]) pre[x]=find( ...
- Oracle 高水位(HWM: High Water Mark)
http://blog.itpub.net/31397003/viewspace-2137246/ http://blog.itpub.net/12778571/viewspace-582695/ h ...
- 设置编码格式为utf8
response.setCharacterEncoding("UTF-8"); 在Servlet2.3中是不行的,至少要2.4版本才可以,如果低于2.4版本,可以用如下办法: re ...
- JAVA实现将GeoHash转化为相应的经纬度坐标
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/50568428 http://www.llwjy.com/blogdetail/f ...
- tomcat为什么要禁用session?
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/47829755 我们先来做一个实验,用jmeter对tomcat下的一个jsp ...
- MFC exe使用C++ dll中的std::string 崩溃
VC6中 MFC exe中 new 纯C++ dll dll 崩溃 我把纯C++的 dll,用/MTd 换成/MDd.就能够了