[AH/HNOI2017]单旋
这道题可以用LCT做,开set,LCT,二叉树
- 操作1:直接开set,找到它要插入的位置,一定是前驱,后缀中deep最大的(显然
手玩) - 操作2:set+LCT询问路径,直接手动提上去,因为树的形态不变
- 操作3:同2
- 操作4:LCT::Cut,手动删除
- 操作5:同4
- 没了
- 记得手动更新二叉树(这个一定要想清楚
会又WA又TLE又RE n遍)
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 10);
IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
}
int fa[_], ch[2][_], rev[_], S[_], sz[_], qc[_], qx[_], o[_], rt, spc[2][_], spf[_];
set <int> T;
set <int> :: iterator it;
IL bool Son(RG int x){ return ch[1][fa[x]] == x; }
IL bool Isroot(RG int x){ return ch[0][fa[x]] != x && ch[1][fa[x]] != x; }
IL void Update(RG int x){ sz[x] = sz[ch[0][x]] + sz[ch[1][x]] + 1; }
IL void Reverse(RG int x){ swap(ch[0][x], ch[1][x]); rev[x] ^= 1; }
IL void Pushdown(RG int x){ if(!rev[x]) return; Reverse(ch[0][x]); Reverse(ch[1][x]); rev[x] = 0; }
IL void Rotate(RG int x){
RG int y = fa[x], z = fa[y], c = Son(x);
if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y;
ch[!c][x] = y; fa[y] = x;
Update(y);
}
IL void Splay(RG int x){
S[++S[0]] = x;
for(RG int y = x; !Isroot(y); y = fa[y]) S[++S[0]] = fa[y];
while(S[0]) Pushdown(S[S[0]--]);
for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
Update(x);
}
IL void Access(RG int x){ for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), ch[1][x] = y, Update(x); }
IL int Findroot(RG int x){ Access(x); Splay(x); while(ch[0][x]) x = ch[0][x]; return x; }
IL void Makeroot(RG int x){ Access(x); Splay(x); Reverse(x); }
IL void Split(RG int x, RG int y){ Makeroot(x); Access(y); Splay(y); }
IL void Link(RG int x, RG int y){ if(!x || !y) return; Makeroot(x); fa[x] = y; }
IL void Cut(RG int x, RG int y){ if(!x || !y) return; Split(x, y); ch[0][y] = fa[x] = 0; }
IL int Query(RG int x){ Makeroot(rt); Access(x); Splay(x); return sz[x]; }
int main(RG int argc, RG char *argv[]){
RG int len = 0, m = Read();
T.insert(-2e9); T.insert(2e9);
for(RG int i = 1; i <= m; ++i){
qc[i] = Read();
if(qc[i] == 1) o[++len] = qx[i] = Read();
}
sort(o + 1, o + len + 1);
for(RG int i = 1; i <= m; ++i)
if(qc[i] == 1) qx[i] = lower_bound(o + 1, o + len + 1, qx[i]) - o;
for(RG int i = 1; i <= m; ++i){
if(qc[i] != 1 && T.size() == 3){
puts("1"); it = T.begin(); ++it;
if(qc[i] >= 3) T.erase(it), rt = 0;
continue;
}
if(qc[i] == 1){
if(T.size() == 2){ T.insert(qx[i]); rt = qx[i]; puts("1"); continue; }
it = T.lower_bound(qx[i]); RG int ans = 0, pos, g;
if(*it != 2e9){ g = Query(*it); if(g > ans) ans = g, pos = *it; }
if(*(--it) != -2e9){ g = Query(*it); if(g > ans) ans = g, pos = *it; }
T.insert(qx[i]); Link(qx[i], pos);
spc[qx[i] > pos][pos] = qx[i]; spf[qx[i]] = pos;
printf("%d\n", ans + 1);
}
else if(qc[i] == 2){
it = T.begin(); ++it;
RG int x = *it, y = spc[1][x], z = spf[x];
printf("%d\n", Query(x));
if(rt != x){
Cut(x, z); Cut(x, y); Link(x, rt); Link(y, z);
spf[rt] = x; spf[x] = 0; spf[y] = z; spc[1][x] = rt; rt = x; spc[0][z] = y;
}
}
else if(qc[i] == 3){
it = T.end(); --it; --it;
RG int x = *it, y = spc[0][x], z = spf[x];
printf("%d\n", Query(x));
if(rt != x){
Cut(x, z); Cut(x, y); Link(x, rt); Link(y, z);
spf[rt] = x; spf[x] = 0; spf[y] = z; spc[0][x] = rt; rt = x; spc[1][z] = y;
}
}
else if(qc[i] == 4){
it = T.begin(); ++it;
RG int x = *it, y = spc[1][x], z = spf[x];
T.erase(it);
printf("%d\n", Query(x));
Cut(x, z); Cut(x, y); Link(y, z);
spf[y] = z; spc[0][x] = spc[1][x] = spf[x] = 0; spc[0][z] = y;
if(rt == x) rt = y;
}
else if(qc[i] == 5){
it = T.end(); --it; --it;
RG int x = *it, y = spc[0][x], z = spf[x];
T.erase(it);
printf("%d\n", Query(x));
Cut(x, z); Cut(x, y); Link(y, z);
spf[y] = z; spc[0][x] = spc[1][x] = spf[x] = 0; spc[1][z] = y;
if(rt == x) rt = y;
}
}
return 0;
}
[AH/HNOI2017]单旋的更多相关文章
- bzoj 4825: [Hnoi2017]单旋 [lct]
4825: [Hnoi2017]单旋 题意:有趣的spaly hnoi2017刚出来我就去做,当时这题作死用了ett,调了5节课没做出来然后发现好像直接用lct就行了然后弃掉了... md用lct不知 ...
- 【LG3721】[HNOI2017]单旋
[LG3721][HNOI2017]单旋 题面 洛谷 题解 20pts 直接模拟\(spaly\)的过程即可. 100pts 可以发现单旋最大.最小值到根,手玩是有显然规律的,发现只需要几次\(lin ...
- 4825: [Hnoi2017]单旋
4825: [Hnoi2017]单旋 链接 分析: 以后采取更保险的方式写代码!!!81行本来以为不特判也可以,然后就总是比答案大1,甚至出现负数,调啊调啊调啊调~~~ 只会旋转最大值和最小值,以最小 ...
- [BZOJ4825][HNOI2017]单旋(线段树+Splay)
4825: [Hnoi2017]单旋 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 667 Solved: 342[Submit][Status][ ...
- 【BZOJ4825】[Hnoi2017]单旋 线段树+set
[BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...
- bzoj4825 [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...
- BZOJ:4825: [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...
- HNOI2017 单旋
题目描述 网址:https://www.luogu.org/problemnew/show/3721 大意: 有一颗单旋Splay(Spaly),以key值为优先度,总共有5个操作. [1] 插入一个 ...
- HNOI2017单旋
单旋 这道题做法贼多,LCT,splay,线段树什么的貌似都行. 像我这种渣渣只会线段树了(高级数据结构学了也不会用). 首先离线所有操作,因为不会有两个点值重复,所以直接离散. 一颗线段树来维护所有 ...
随机推荐
- linux集群架构
Linux集群架构 根据功能划分为两大类:高可用和负载均衡 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有:heart ...
- Scoping the Project for iOS 7
Scoping the Project On This Page Things Every App Must Do Things Every App Should Do If You Must Con ...
- 【应知应会】15个常用的JavaScript字符串操作方法
1 初始化 //常用初始化方法 var stringVal = "hello iFat3"; //构造函数创建方法 var stringObj = new String(" ...
- Spark DataFrame写入HBase的常用方式
Spark是目前最流行的分布式计算框架,而HBase则是在HDFS之上的列式分布式存储引擎,基于Spark做离线或者实时计算,数据结果保存在HBase中是目前很流行的做法.例如用户画像.单品画像.推荐 ...
- PHP 个人用到的琐碎代码记录
查找字符串出现次数的方法 substr_count(string,substring,[start],[length]) 函数延迟代码执行若干秒,若成功,返回 0,否则返回 false. sleep( ...
- hibernate学习(六) flush()和clean()区别和使用
session.flush()是强制和数据库同步 session.clean()是清除session中的缓存 对于批量数据插入的时候优化:减少cpu和内存(缓存)占用量 @Test public vo ...
- Sping Boot入门到实战之入门篇(二):第一个Spring Boot应用
该篇为Spring Boot入门到实战系列入门篇的第二篇.介绍创建Spring Boot应用的几种方法. Spring Boot应用可以通过如下三种方法创建: 通过 https://start.spr ...
- EL表达式判断不能为空
前几天在做一个网站的时候遇到一个问题,要判断一个在request或者session域里面的对象是否为空,利用EL表达式,正常思维就是用 <c:if test="${* != null} ...
- SIFT解析(二)特征点位置确定
最近微博上有人发起投票那篇论文是自己最受益匪浅的论文,不少人说是lowe的这篇介绍SIFT的论文.确实,在图像特征识别领域,SIFT的出现是具有重大意义的,SIFT特征以其稳定的存在,较高的区分度推进 ...
- mysql数据库导入导出 查询 修改表记录
mysql数据导入导出: 导入: 把系统的文件的内容,保存到数据库的表里 导入数据的基本格式:mysql> load data infile "文件名" into table ...