[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,线段树什么的貌似都行. 像我这种渣渣只会线段树了(高级数据结构学了也不会用). 首先离线所有操作,因为不会有两个点值重复,所以直接离散. 一颗线段树来维护所有 ...
随机推荐
- 携程Apollo(阿波罗)配置中心在.NET Core项目快速集成
.NET Core的支持文档大体上可以参考文档.Net客户端使用指南:https://github.com/ctripcorp/apollo/wiki/.Net%E5%AE%A2%E6%88%B7%E ...
- configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.
Apache在2.4版本以后,编译时: # ./configure \ --prefix=/usr/local/apache2 \ --with-included-apr \ --enable-so ...
- iOS 关于文件的操作
最近做东西,遇到了使用文件方面的问题,花了点时间把文件研究了一下! 一 关于文件路径的生成 我用的方法是: -(NSString*)dataFilePath { NSArray * paths = ...
- js到底new了点啥
在最开始学习js的时候,我看书上写着,创建一个数组,一个对象通常使用new,如下: var arr=new Array(),//arr=[] obj=new Object();//obj={} 到了后 ...
- Java经典编程题50道之四十二
809*??=800*??+9*??+1,其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数.求??代表的两位数,以及809*??后的结果. public class Example ...
- 深入理解ES6之—对象
Object新方法 Object.is()方法 在js中比较两个值时,你可能会用相等运算符==或者严格相等运算符 ===.为了避免在比较时发生强制类型转换,许多开发者更倾向于使用后者. Object. ...
- 转:【web前端开发】浏览器兼容性处理大全
解决思路: ①.写代码的时候遵循W3C标准,按照最新稳定版本的IE或WebKit内核浏览器进行编码 ②.遇到部分无法全面解决浏览器兼容的时候,采取CSS的hack手段进行针对性微调.简单的说,CSS ...
- uva1025 动态规划
这道题的边界是dp(T,N)=0,状态dp(i,j)表示在时间i.第j个车站最少等待时间,有三个决策:1.等1分钟 2.如果有向左的车,向左 3.若果有向右的车,向右 转移方程就是dp(i,j)=m ...
- python语言基础语法笔记<note2--面向对象编程>
Python面向对象编程(OOP) 一.面向对象过程的优点特征: 封装 模型的特征和能力打包在一起 模型的改变由模型自身完成 隐藏模型的细节,外界只能使用,不能改变 继承 符合自然界分类规律 快速实现 ...
- nodejs爬虫初试---superagent和cheerio
前言 早就听过爬虫,这几天开始学习nodejs,写了个爬虫 demo ,爬取 博客园首页的文章标题.用户名.阅读数.推荐数和用户头像,现做个小总结. 使用到这几个点: 1.node的核心模块-- 文件 ...