4825: [Hnoi2017]单旋

题意:有趣的spaly


hnoi2017刚出来我就去做,当时这题作死用了ett,调了5节课没做出来然后发现好像直接用lct就行了然后弃掉了...

md用lct不知道好写到哪里去了1h就写完了

原树的父亲孩子可以直接维护

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
#define fir first
#define sec second
const int N = 4e5+5, INF=1e9+5;
inline int read() {
char c=getchar(); int x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
} namespace lct {
struct meow{int ch[2], fa, rev, size;} t[N];
inline int wh(int x) {return t[pa].ch[1] == x;}
inline int isr(int x) {return t[pa].ch[0] != x && t[pa].ch[1] != x;}
inline void update(int x) {t[x].size = t[lc].size + t[rc].size + 1;}
inline void rever(int x) {swap(lc, rc); t[x].rev ^= 1;}
inline void pushdn(int x) {
if(t[x].rev) {
if(lc) rever(lc);
if(rc) rever(rc);
t[x].rev = 0;
}
}
void pd(int x) {if(!isr(x)) pd(pa); pushdn(x);}
inline void rotate(int x) {
int f = t[x].fa, g = t[f].fa, c = wh(x);
if(!isr(f)) t[g].ch[wh(f)] = x; t[x].fa = g;
t[f].ch[c] = t[x].ch[c^1]; t[t[f].ch[c]].fa = f;
t[x].ch[c^1] = f; t[f].fa = x;
update(f); update(x);
}
void splay(int x) {
pd(x);
for(; !isr(x); rotate(x))
if(!isr(pa)) rotate(wh(x) == wh(pa) ? pa : x);
}
void access(int x) {
for(int y=0; x; y=x, x=pa) splay(x), rc=y, update(x);
}
void maker(int x) {
access(x); splay(x); rever(x);
}
void link(int x, int y) { if(!x || !y) return;
maker(x); t[x].fa = y;
}
void cut(int x, int y) { if(!x || !y) return;
maker(x); access(y); splay(y);
t[x].fa = t[y].ch[0] = 0; update(y);
}
int que(int x, int y) {
maker(x); access(y); splay(y);
return t[y].size;
}
} using lct::link; using lct::cut; using lct::que; int root;
struct info {int fa, l, r;} t[N]; set< pair<int, int> > S;
set< pair<int, int> >::iterator i1, i2;
int tot;
void insert(int k) {
++tot;
i1 = i2 = S.insert(make_pair(k, tot)).fir;
int a = 0, b = 0, x = tot;
if(i1 != S.begin()) a = (--i1)->sec;
if(++i2 != S.end()) b = i2->sec;
if(!a && !b) root = x;
else if(a && t[a].r == 0) t[a].r = x, t[x].fa = a, link(a, x);
else if(b && t[b].l == 0) t[b].l = x, t[x].fa = b, link(b, x);
printf("%d\n", que(root, x));
} void spa_min() {
int x = S.begin()->sec, r = t[x].r, p = t[x].fa;
printf("%d\n", que(root, x));
if(x == root) return; cut(x, p); cut(x, r); link(p, r); link(x, root);
t[x].r = root; t[root].fa = x; t[r].fa = p; t[p].l = r;
root = x;
} void spa_max() {
i1 = S.end();
int x = (--i1)->sec, l = t[x].l, p = t[x].fa;
printf("%d\n", que(root, x));
if(x == root) return; cut(x, p); cut(x, l); link(p, l); link(x, root);
t[x].l = root; t[root].fa = x; t[l].fa = p; t[p].r = l;
root = x;
} void del_min() {
i1 = S.begin();
int x = S.begin()->sec, r = t[x].r, p = t[x].fa; //printf("\ndel_min %d %d %d\n", x, r, p);
printf("%d\n", que(root, x));
if(x == root) {
cut(x, r); t[r].fa = 0; root = r;
} else {
cut(x, p); cut(x, r); link(p, r);
t[r].fa = p; t[p].l = r;
}
S.erase(i1);
} void del_max() {
i1 = S.end();
int x = (--i1)->sec, l = t[x].l, p = t[x].fa;
printf("%d\n", que(root, x));
if(x == root) {
cut(x, l); t[l].fa = 0; root = l;
} else {
cut(x, p); cut(x, l); link(p, l);
t[l].fa = p; t[p].r = l;
}
S.erase(i1);
} int m, type, k;
int main() {
freopen("in", "r", stdin);
m = read();
for(int i=1; i<=m; i++) {
type=read(); //printf("hi %d %d\n", i, type);
if(type == 1) k = read(), insert(k);
if(type == 2) spa_min();
if(type == 3) spa_max();
if(type == 4) del_min();
if(type == 5) del_max();
}
}

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

  1. BZOJ:4825: [Hnoi2017]单旋

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

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

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

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

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

  4. 4825: [Hnoi2017]单旋

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

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

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

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

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

  7. 【LG3721】[HNOI2017]单旋

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

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

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

  9. 洛谷3721 HNOI2017单旋(LCT+set+思维)

    这题难道不是spaly裸题吗? 言归正传QWQ 一看到这个题目,其实第一反应是很懵X的 从来没有见过类似的题目啊,什么\(spaly\),单旋.QWQ很懵逼啊 不过,我们可以注意到这么一件事情,就是我 ...

随机推荐

  1. Sublime Text 3.0版本的傻瓜式汉化步骤

    Sublime text 3是非常好的文本编辑器,在试用过N款文本编辑器(Notepad.Notepad++.Notepad2.Programmer's notepad.EditPlus,Vim, T ...

  2. ReentrantLock与Condition构造有界缓存队列与数据栈

    通过ReentrantLock与Condition的设计,以数组为基础,可以实现简单的队列和栈的数据结构,临界阻塞的效果. ReentrantLock相对于synchronized比较大的一个区别是有 ...

  3. hbase伪分布式安装(单节点安装)

    hbase伪分布式安装(单节点安装) http://hbase.apache.org/book.html#quickstart   1.    前提配置好java,环境java变量     上传jdk ...

  4. The most interesting feature of iPhone X - FaceID

    No doubt everybody knows that iPhone 8 & iPhone X appear on the market. A feature called FaceID ...

  5. 初识RabbitMQ,附RabbitMQ+PHP演示实例

    RabbitMQ是一个在AMQP基础上实现的企业级消息系统.何谓消息系统,就是消息队列系统,消息队列是""消费-生产者模型""的一个典型的代表,一端往消息队列中 ...

  6. DEDE在图集列表中调出图集的所有图片[首页也适用]

    在include/common.func.php 中添加以下函数代码 代码如下:   // 在图集列表中调出图集的所有图片 function Getimgs($aid, $imgwith = 220, ...

  7. 网口up不起来问题排查

    最近处理一个问题,发现有的网口up不起来.       ethtool eth6   Settings for eth6:   Supported ports: [ FIBRE ]   Support ...

  8. ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(一)—修改数据库连接

    开发环境:vs2017  版本:15.3.5 项目环境:.net framework 4.6.1    模板asp.net core 2.0  Web应用程序(模型视图控制器) 身份验证:个人用户账号 ...

  9. _0_web_基础

    创:18_3_2017修:20_3_2017 什么是前端? --在浏览器中展示内容以及处理请求 什么是浏览器? --一款能将网页内容展现给用户查看,并且让用户与网页交互的软件 什么是内核? --渲染引 ...

  10. linkin大话面向对象--java关键字

    java中的关键字有以下几个,他们不能作任何其它的用途. 发现没,java中的关键字全是小写,java是严格区分大小写的. abstract  default  null  synchronized ...