4825: [Hnoi2017]单旋

链接

分析:

  以后采取更保险的方式写代码!!!81行本来以为不特判也可以,然后就总是比答案大1,甚至出现负数,调啊调啊调啊调~~~

  只会旋转最大值和最小值,以最小值为例,画一下图可以看出,旋转后,深度分成三部分讨论,最小值的深度(变为1),最小值右子树的深度(不变),其他的点的深度(整体加1)。所以线段树维护一下。

  现在考虑如何插入一个点,可以知道一个点加入后一定是在前驱的右边,或者后继的左边。一个性质:前驱后继一定在splay上是一个是另一个的祖先的关系(反证:若中间存在一个点为它们两个的祖先,那么这个点前驱后继一定有一个是中间的点,没有重复的数字)。那么这个点加入的过程中,一定是加入在前驱后继中比较深的一个点。所以可以直接找到前驱后继,深度大的就是它的父节点。

  找到根据splay的性质找到相应的区间,进行区间加减,单点求值。前驱后继可以直接在线段树上二分,或者直接用set。(或者直接splay)

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define Root 1, tot, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define Clear(x) ch[x][0] = ch[x][1] = fa[x] = 0
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int T[N << ], tag[N << ], ch[N][], fa[N], a[N], disc[N], opt[N], Rt;
set<int> s;
set<int> :: iterator it; void pushdown(int rt) {
tag[rt << ] += tag[rt]; tag[rt << | ] += tag[rt];
T[rt << ] += tag[rt], T[rt << | ] += tag[rt];
tag[rt] = ;
}
void update(int l,int r,int rt,int L,int R,int v) {
if (L <= l && r <= R) {
tag[rt] += v; T[rt] += v; return;
}
if (tag[rt]) pushdown(rt);
int mid = (l + r) >> ;
if (L <= mid) update(lson, L, R, v);
if (R > mid) update(rson, L, R, v);
}
void Change(int l,int r,int rt,int p,int v) {
if (l == r) { T[rt] = v, tag[rt] = ; return ; }
if (tag[rt]) pushdown(rt);
int mid = (l + r) >> ;
if (p <= mid) Change(lson, p, v);
if (p > mid) Change(rson, p, v);
}
int query(int l,int r,int rt,int p) {
if (l == r) return T[rt];
if (tag[rt]) pushdown(rt);
int mid = (l + r) >> ;
if (p <= mid) return query(lson, p);
if (p > mid) return query(rson, p);
}
int main() {
int n = read(), tot = ;
for (int i = ; i <= n; ++i) {
opt[i] = read();
if (opt[i] == ) a[i] = read(), disc[++tot] = a[i];
}
s.insert(-1e9), s.insert(1e9);
sort(disc + ,disc + tot + );
for (int i = ; i <= n; ++i) if (opt[i] == ) a[i] = lower_bound(disc + , disc + tot + , a[i]) - disc; for (int pre, suc, now, mx, mn, i = ; i <= n; ++i) {
if (opt[i] == ) {
it = s.lower_bound(a[i]); suc = *it; pre = *(--it); now = ;
int d1 = -, d2 = -;
if (pre != -1e9) d1 = query(Root, pre);
if (suc != 1e9) d2 = query(Root, suc);
if (d1 > d2) now = d1 + , fa[a[i]] = pre, ch[pre][] = a[i];
if (d1 < d2) now = d2 + , fa[a[i]] = suc, ch[suc][] = a[i];
if (now == ) Rt = a[i], Clear(Rt);
Change(Root, a[i], now);
s.insert(a[i]);
}
else if (opt[i] & ) {
it = s.end(); it --; it --; mx = *it; now = query(Root, mx);
if (mx != Rt) { // 注意!!!此处需特判!!!!!!!!!!!!!
update(Root, , mx - , );
if (fa[mx] + <= mx - ) {
update(Root, fa[mx] + , mx - , -);
}
fa[ch[mx][]] = fa[mx]; ch[fa[mx]][] = ch[mx][];
ch[mx][] = Rt; fa[Rt] = mx; Rt = mx; fa[mx] = ;
Change(Root, mx, );
}
if (opt[i] == ) {
Rt = ch[mx][], fa[Rt] = ; Clear(mx);
s.erase(s.find(mx));
update(Root, , tot, -);
}
}
else {
it = s.begin(); it ++; mn = *it; now = query(Root, mn);
if (mn != Rt) {
update(Root, mn + , tot, );
if (mn + <= fa[mn] - ) {
update(Root, mn + , fa[mn] - , -);
}
fa[ch[mn][]] = fa[mn]; ch[fa[mn]][] = ch[mn][];
ch[mn][] = Rt; fa[Rt] = mn; Rt = mn; fa[mn] = ;
Change(Root, mn, );
}
if (opt[i] == ) {
Rt = ch[mn][]; fa[Rt] = ; Clear(mn);
s.erase(s.find(mn));
update(Root, , tot, -);
}
}
printf("%d\n",now);
}
return ;
}

4825: [Hnoi2017]单旋的更多相关文章

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

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

  2. BZOJ:4825: [Hnoi2017]单旋

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

  3. 【刷题】BZOJ 4825 [Hnoi2017]单旋

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

  4. bzoj 4825: [Hnoi2017]单旋【dfs序+线段树+hash】

    这个代码已经不是写丑那么简单了--脑子浆糊感觉np++分分钟想暴起打死我--就这还一遍A过了-- 先都读进来hash一下,因为是平衡树所以dfs序直接按照点值来就好 对于每个操作: 1:set维护已插 ...

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

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

  6. 【LG3721】[HNOI2017]单旋

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

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

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

  8. bzoj P4825 [Hnoi2017]单旋——solution

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

  9. bzoj4825 [Hnoi2017]单旋

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

随机推荐

  1. struts2(2.0.x到2.1.2版本)的核心和工作原理(转)

    在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样的好处? 设计目标 Struts设计的第一目标就是使MVC模式应用于web程序设计.在这儿MVC模式的好处就 ...

  2. PHP imagechar() 图形验证码 字体太小问题

    bool imagechar ( resource $image , int $font , int $x , int $y , string $c , int$color ) imagechar() ...

  3. gluoncv,faster rcnn 处理难样本

    难样本,是我们在标注的时候,连肉眼都看不清的小像素物体,也可以说是既不是正样本,也不是负样本. 利用gluoncv时,这些标注框也实在存在,gluoncv会实在将他当做一个GT,但我们知道这是不好的. ...

  4. Mysql 用户权限管理--从 xxx command denied to user xxx

    今天遇到一个mysql 权限的问题,即标题所述  xxx command denied to user xxx,一般mysql 这种报错,基本都属于当前用户没有进行该操作的权限,需要 root 用户授 ...

  5. Mybatis Plus简介

    集成 MP Mybatis-Plus 的集成非常简单,对于 Spring,我们仅仅需要把 Mybatis 自带的MybatisSqlSessionFactoryBean替换为 MP 自带的即可. &l ...

  6. Knowledge Point 20180506 深究Java的跨平台特性

    本章主题:从骨子里看Java的跨平台;本文内容部分摘自https://www.cnblogs.com/roger-yu/p/5827452.html 有过基础Java知识的开发人员都知道Java是跨平 ...

  7. centos 7 配置nginx 的yum源

    在/etc/yum.repos.d里创建nginx.repo文件: touch nginx.repo vim nginx.repo 填写如下内容后保存 [nginx] name=nginx repo ...

  8. IIS配置导入导出

    使用管理员身份运行cmd 应用程序池: # 导出所有应用程序池 %windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\a ...

  9. 修复支付宝后台报错session IP change to many

    在项目的web.xml 中添加: <init-param> <param-name>sessionStatEnable</param-name> <param ...

  10. Redis之Redis持久化

    Redis(Remote Dictionary Server)是一个可持久化的内存.Key-Value数据库. 作为内存数据库,为了防止因服务器断电或系统宕机而引起的数据丢失问题,Redis自带了持久 ...