这道题可以用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]单旋的更多相关文章

  1. bzoj 4825: [Hnoi2017]单旋 [lct]

    4825: [Hnoi2017]单旋 题意:有趣的spaly hnoi2017刚出来我就去做,当时这题作死用了ett,调了5节课没做出来然后发现好像直接用lct就行了然后弃掉了... md用lct不知 ...

  2. 【LG3721】[HNOI2017]单旋

    [LG3721][HNOI2017]单旋 题面 洛谷 题解 20pts 直接模拟\(spaly\)的过程即可. 100pts 可以发现单旋最大.最小值到根,手玩是有显然规律的,发现只需要几次\(lin ...

  3. 4825: [Hnoi2017]单旋

    4825: [Hnoi2017]单旋 链接 分析: 以后采取更保险的方式写代码!!!81行本来以为不特判也可以,然后就总是比答案大1,甚至出现负数,调啊调啊调啊调~~~ 只会旋转最大值和最小值,以最小 ...

  4. [BZOJ4825][HNOI2017]单旋(线段树+Splay)

    4825: [Hnoi2017]单旋 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 667  Solved: 342[Submit][Status][ ...

  5. 【BZOJ4825】[Hnoi2017]单旋 线段树+set

    [BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...

  6. bzoj4825 [Hnoi2017]单旋

    Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...

  7. BZOJ:4825: [Hnoi2017]单旋

    Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...

  8. HNOI2017 单旋

    题目描述 网址:https://www.luogu.org/problemnew/show/3721 大意: 有一颗单旋Splay(Spaly),以key值为优先度,总共有5个操作. [1] 插入一个 ...

  9. HNOI2017单旋

    单旋 这道题做法贼多,LCT,splay,线段树什么的貌似都行. 像我这种渣渣只会线段树了(高级数据结构学了也不会用). 首先离线所有操作,因为不会有两个点值重复,所以直接离散. 一颗线段树来维护所有 ...

随机推荐

  1. Python print 输出到控制台 丢数据

    import xlrd import sys,time data = xlrd.open_workbook("C:\Users\Administrator\Desktop\\new1.xls ...

  2. 如何学习 MFC ?

    //std::string => CString std::string srcString = "Hello World!"; CString dstString = CS ...

  3. 《android开发艺术探索》读书笔记(十)--Android的消息机制

    接上篇<android开发艺术探索>读书笔记(九)--四大组件 No1: 消息队列MessageQueue的内部存储结构并不是真正的队列,而是采用单链表的数据结构来存储消息列表,因为单链表 ...

  4. UVA - 12186 Another Crisis (树形DP)

    思路:dp[i]表示让上司i签字至少需要多少工人签字.       转移方程:将i的所有节点根据所需工人数量升序排序,设i需要k个下属签字,dp[i] = sum{dp[v]| 0 <= v & ...

  5. SpringBoot+gradle+idea实现热部署和热加载

    前言 因为之前使用myeclipes的同学就知道,在使用myeclipes的时候,java文件或者jsp文件写完之后会被直接热加载到部署的容器中,从而在开发的时候,不同经常去重启项目,从而达到了增加开 ...

  6. 科普 TLS 1.3—新特性与开启方式

    TLS 1.3 协议针对安全强化及效率提升等方面进行了大量修改,相继推出 20 多个草案版本,即将完成最终的标准化.标准完成后,OpenSSL 组织将推出 OpenSSL 1.1.1 版本,对 TLS ...

  7. win10如何快速扫描-上海IT外包

     第一步,点击Windows图标  第二步点击所有应用  第三步点击Windows附件  最后点击Windows传真和扫描就可以了 上海IT33_专业的it外包一站式服务商,为多家企业提供it ...

  8. webpack打包速度和性能再次优化

    一. 改单dll为双dll 因为上图原因,使用CommonsChunkPlugin时,导致其打包出来的vendors.js内的模块ID会因为其他文件引用模块数量的变化而变化. 所以现利用DllPlug ...

  9. webpack入门宝典

    前提摘要 本文是经过二天自己学习总结出来的一些心得,本文是在最新版的webpack4x的基础上进行配置的(听说webpack4x比以往改变都很大有些插件可能有问题).如果你以前没怎么接触过Webpac ...

  10. API接口签名校验

    在开发app中,我们经常要为app提供接口.但是为了保证数据的安全,我们通常会对接口的参数进行加密. 1.不验证的接口api api接口请求,"http://www.xx.com/getUs ...