bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 17715 Solved: 7769
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
Input
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)
Output
对于操作3,4,5,6每行输出一个数,表示对应答案
Sample Input
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
84185
492737
HINT
Source
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio> #define inf 1000000007
#define N 100007
#define ls c[p][0]
#define rs c[p][1]
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if (ch=='-') f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,rt;
int fa[N],val[N],rev[N],mx[N],sz[N],flag[N],c[N][]; inline void update(int p)
{
sz[p]=sz[ls]+sz[rs]+;
}
inline void pushdown(int p)
{
if (rev[p])
{
rev[p]^=;
rev[ls]^=,rev[rs]^=;
swap(c[p][],c[p][]);
}
}
void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if (c[y][]==x) l=;else l=;r=l^;
if (y==k) k=x;//交换后x就等于y
else if (c[z][]==y) c[z][]=x;
else c[z][]=x;
fa[x]=z,fa[y]=x,fa[c[x][r]]=y;
c[y][l]=c[x][r],c[x][r]=y;
update(y),update(x);
}
void splay(int x,int &k)
{
while(x!=k)
{
int y=fa[x],z=fa[y];
if (y!=k)
{
if (c[y][]==x^c[z][]==y) rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int find(int p,int num)
{
pushdown(p);
if (sz[ls]>=num) return find(ls,num);
else if (sz[ls]+==num) return p;
else return find(rs,num-sz[ls]-);
}
void spin(int l,int r)
{
int x=find(rt,l),y=find(rt,r+);
splay(x,rt),splay(y,c[x][]);
int now=c[c[x][]][];
rev[now]^=;
}
void build(int l,int r,int p)
{
if (l>r) return;
if (l==r)
{
fa[l]=p,sz[l]=,val[l]=l;
if (l<p) c[p][]=l;
else c[p][]=l;
return;
}
int mid=(l+r)>>;
build(l,mid-,mid),build(mid+,r,mid);
fa[mid]=p,val[mid]=mid;
if (mid<p) c[p][]=mid;
else c[p][]=mid;
update(mid);
}
int main()
{
n=read(),m=read();
build(,n+,),rt=(n+)>>;
while(m--)
{
int x=read(),y=read();
spin(x,y);
}
for (int i=;i<=n;i++)
{
int x=find(rt,i+);
if (i==) cout<<val[x]-;
else cout<<" "<<val[x]-;
}
}
bzoj 3223 文艺平衡树 splay 区间翻转的更多相关文章
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- bzoj 3223 文艺平衡树 Splay 打标志
是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...
- [题解]bzoj 3223 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- BZOJ 3223 文艺平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- BZOJ 3223 文艺平衡树 [codevs3303翻转区间]
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...
- 文艺平衡树(区间翻转)(Splay模板)
这篇blog写的吼啊 #include<cstdio> #include<iostream> #include<cstring> using namespace s ...
- 洛谷 3391 【模板】文艺平衡树 Treap区间翻转
[题解] 用Treap维护这个序列. 加入的时候直接插入到末尾,这样Treap就变成一棵以插入时间先后为排序关键字的二叉搜索树. 对于翻转操作,我们分裂出需要翻转的区间,给这个区间的root打一个翻转 ...
- [bzoj3223]文艺平衡树(splay区间反转模板)
解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
随机推荐
- 使用json传输数组实例
client.php <?php //遍历数组元素,并将元素转码 function array_iconv($data, $in_charset='GBK', $out_charset='UTF ...
- CF962D Merge Equals
思路: 不必每次都找最小的值进行合并,从前往后扫一遍的过程中能合并就一直合并. 实现: #include <bits/stdc++.h> using namespace std; type ...
- jQuery中面向对象思想实现盒子内容切换
这里主要是模拟小米官网中的首页的内容模块实现的主要动态效果 布局:采用了bootstrap框架进行布局,及其其中的字体图标 html: <!-- 内容 --> <div class= ...
- CPLD
复杂可编程逻辑器件(Complex Programmable Logic Device, CPLD),CPLD适合用来实现各种运算和组合逻辑(combinational logic).一颗CPLD内等 ...
- COGS 495. 窗口
★☆ 输入文件:window.in 输出文件:window.out 简单对比时间限制:2 s 内存限制:256 MB [问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左 ...
- 聊聊JavaScript和Scala的表达式 Expression
我们先看下面这段简单的JavaScript代码. 我在第10行调用了函数f,其中传入的第二个和第三个参数都是一个逗号表达式. 函数f的实现,会检查这两个参数的类型,如果是函数,则执行函数调用,再打印其 ...
- Linux OpenGL 实践篇-9 模型
之前一直渲染箱子,显得有点单调.这一次我们绘制一个用艺术家事先用建模工具创建的模型. 本次实践参考:https://learnopengl-cn.github.io/03%20Model%20Load ...
- DB9串口引脚定义
在单片机串口通信中,使用3根信号线就能够实现通信:RXD,TXD,GND. 经常使用的RS232串口线使用DB9端子. DB9分为公头和母头两种: 一般使用时,引脚定义如下: 连接方式: 注:RXD- ...
- AspNetCore容器化(Docker)部署(一) —— 入门
一.docker注册安装 Windows Docker Desktop https://www.docker.com/products/docker-desktop Linux Docker CE h ...
- 连接MySQL错误“plugin caching_sha2_password could not be loaded”的解决办法
MySQL新版默认使用caching_sha2_password作为身份验证插件,而旧版是使用mysql_native_password.当连接MySQL时报错“plugin caching_sha2 ...