Splay 暴力维护节点信息即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; int delta,t1,t2,t3,k[1000005],ch[1000005][2],fa[1000005],a[1000005],t[1000005],n,ind,root,opera[1000005][2],seq[1000005],cnt=0; void rotate(int p){
int q=fa[p], y=fa[q], x=ch[fa[p]][1]==p;
ch[q][x]=ch[p][x^1]; fa[ch[q][x]]=q;
ch[p][x^1]=q; fa[q]=p; fa[p]=y;
if(y)if(ch[y][0]==q)ch[y][0]=p;
else if(ch[y][1]==q)ch[y][1]=p;} void splay(int p){
int x=p,y;
for(;y=fa[x];rotate(x))if(fa[y])rotate((ch[fa[y]][0]==y)==(ch[y][0]==x)?y:x);
root=x;} int prefix(int p){
if(p==0) return 0;
splay(p);
int x=ch[p][0];
while(x&&ch[x][1]) x=ch[x][1];
return x;} int suffix(int p){
if(p==0) return 0;
splay(p);
int x=ch[p][1];
while(x&&ch[x][0]) x=ch[x][0];
return x;} int minpt(){
int x=root;
while(x&&ch[x][0]) x=ch[x][0];
return x;} int maxpt(){
int x=root;
while(x&&ch[x][1]) x=ch[x][1];
return x;} void insert(int v){
if(root==0) root=++ind, k[ind]=v;
else{
int x=root,tx=root;
while(tx) x=tx, tx=ch[x][k[x]<v];
ch[x][k[x]<v]=++ind;
k[ind]=v; fa[ind]=x;
splay(ind);
}} int search(int v){
int p=root;
while(k[p]-v&&ch[p][k[p]<v]) p=ch[p][k[p]<v];
return p;} void remove(int p){
if(p==0)return;
splay(p);
int lr=ch[p][0],rr=ch[p][1];
if(lr==0) {root=rr, fa[rr]=0; return;}
int tm=lr;
root=lr;
while(tm&&ch[tm][1]) tm=ch[tm][1];
if(tm) splay(tm);
ch[root][1]=rr; fa[rr]=root;
} void print(int p){
if(p==0) return;
print(ch[p][0]);
printf("%d ",k[p]);
print(ch[p][1]);} void modify(int p,int l,int r,int ml,int mr,int k){
if(l>mr||r<ml) return;
if(l>=ml&&r<=mr){ t[p]+=k; a[p]+=k; return;}
t[p*2]+=t[p], t[p*2+1]+=t[p];
a[p*2]+=t[p], a[p*2+1]+=t[p]; t[p]=0;
modify(p*2,l,(l+r)/2,ml,mr,k);
modify(p*2+1,(l+r)/2+1,r,ml,mr,k);
a[p]=min(a[p*2],a[p*2+1]);} int query(int p,int l,int r,int ql,int qr){
if(l>qr||r<ql) return 0x7fffffff;
if(l>=ql&&r<=qr) return a[p];
t[p*2]+=t[p], t[p*2+1]+=t[p];
a[p*2]+=t[p], a[p*2+1]+=t[p]; t[p]=0;
return min(query(p*2,l,(l+r)/2,ql,qr),query(p*2+1,(l+r)/2+1,r,ql,qr));} int getvalue(int pos){
int t=query(1,1,cnt,pos,pos);
if(t>=0x3f3f3f3) return 0;
else return t;} void putvalue(int pos,int k){
int ori=query(1,1,cnt,pos,pos);
modify(1,1,cnt,pos,pos,-ori);
modify(1,1,cnt,pos,pos,k);} int main(){
memset(a,0x3f,sizeof a);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&t1);
if(t1==1) scanf("%d",&t2);
opera[i][0]=t1, opera[i][1]=t2;
if(t1==1) seq[cnt++]=t2;
}
sort(seq,seq+cnt);
for(int i=1;i<=n;i++)
opera[i][1]=upper_bound(seq,seq+cnt,opera[i][1])-seq;
for(int i=1;i<=n;i++){
t1=opera[i][0], t2=opera[i][1];
if(t1==1){
insert(t2);
putvalue(t2,(t3=max(getvalue(k[prefix(search(t2))]),getvalue(k[suffix(search(t2))]))+1));
printf("%d\n",t3);
}
if(t1==2){
int p=k[minpt()];
int lb=p,rb=p,sd=getvalue(p);
for(int i=17;i>=0;i--) if(rb+(1<<i)<=cnt&&query(1,1,cnt,rb+1,rb+(1<<i))>sd) rb=rb+(1<<i);
for(int i=17;i>=0;i--) if(lb-(1<<i)>0&&query(1,1,cnt,lb-(1<<i),lb-1)>sd) lb=lb-(1<<i);
modify(1,1,cnt,1,cnt,+1);
modify(1,1,cnt,lb,rb,-1);
putvalue(p,1);
printf("%d\n",sd);
}
if(t1==3){
int p=k[maxpt()];
int lb=p,rb=p,sd=getvalue(p);
for(int i=17;i>=0;i--) if(rb+(1<<i)<=cnt&&query(1,1,cnt,rb+1,rb+(1<<i))>sd) rb=rb+(1<<i);
for(int i=17;i>=0;i--) if(lb-(1<<i)>0&&query(1,1,cnt,lb-(1<<i),lb-1)>sd) lb=lb-(1<<i);
modify(1,1,cnt,1,cnt,+1);
modify(1,1,cnt,lb,rb,-1);
putvalue(p,1);
printf("%d\n",sd);
}
if(t1==4){
int p=k[minpt()];
int lb=p,rb=p,sd=getvalue(p);
for(int i=17;i>=0;i--) if(rb+(1<<i)<=cnt&&query(1,1,cnt,rb+1,rb+(1<<i))>sd) rb=rb+(1<<i);
for(int i=17;i>=0;i--) if(lb-(1<<i)>0&&query(1,1,cnt,lb-(1<<i),lb-1)>sd) lb=lb-(1<<i);
modify(1,1,cnt,lb,rb,-1);
putvalue(p,0x3f3f3f3f);
remove(minpt());
printf("%d\n",sd);
}
if(t1==5){
int p=k[maxpt()];
int lb=p,rb=p,sd=getvalue(p);
for(int i=17;i>=0;i--) if(rb+(1<<i)<=cnt&&query(1,1,cnt,rb+1,rb+(1<<i))>sd) rb=rb+(1<<i);
for(int i=17;i>=0;i--) if(lb-(1<<i)>0&&query(1,1,cnt,lb-(1<<i),lb-1)>sd) lb=lb-(1<<i);
modify(1,1,cnt,lb,rb,-1);
putvalue(p,0x3f3f3f3f);
remove(maxpt());
printf("%d\n",sd);
}
}
}

[AH2017/HNOI2017] 单旋 - Splay的更多相关文章

  1. P3721 [AH2017/HNOI2017]单旋

    题目:https://www.luogu.org/problemnew/show/P3721 手玩一下即可AC此题. 结论:插入x后,x要么会成为x的前驱的右儿子,要么成为x的后继的左儿子,这取决于它 ...

  2. 洛谷 P3721 - [AH2017/HNOI2017]单旋(LCT)

    洛谷题面传送门 终于调出来这道题了,写篇题解( 首先碰到这样的题我们肯定要考虑每种操作会对树的形态产生怎样的影响: 插入操作:对于 BST 有一个性质是,当你插入一个节点时,其在 BST 上的父亲肯定 ...

  3. luogu P3721 [AH2017/HNOI2017]单旋

    传送门 \(Spaly:\)??? 考虑在暴力模拟的基础上优化 如果要插入一个数,那么根据二叉查找树的性质,这个点一定插在他的前驱的右子树或者是后继的左子树,可以利用set维护当前树里面的数,方便查找 ...

  4. [AH2017/HNOI2017]单旋

    题目 \(\rm splay\)水平太差,于是得手玩一下才能发现规律 首先插入一个数,其肯定会成为其前驱的右儿子或者是后继的左儿子,进一步手玩发现前驱的右儿子或者是后继的左儿子一定只有一个是空的,我们 ...

  5. 洛谷P3721 [AH2017/HNOI2017]单旋(线段树 set spaly)

    题意 题目链接 Sol 这题好毒瘤啊.. 首先要观察到几个性质: 将最小值旋转到根相当于把右子树变为祖先的左子树,然后将原来的根变为当前最小值 上述操作对深度的影响相当于右子树不变,其他的位置-1 然 ...

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

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

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

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

  8. 【LG3721】[HNOI2017]单旋

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

  9. 4825: [Hnoi2017]单旋

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

随机推荐

  1. React Native运行出现Could not find "iPhone X" simulator

    打开项目文件夹下 node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js 查找 if (!version.startsW ...

  2. alpine安装telnet等工具

    alpine确实是很精简,但是对于熟悉了centos和ununtu的个人来说,实在是不习惯. 因此,记录关于alpine的一些包安装,以及操作细节(逐渐补充). 1. telnet >>& ...

  3. Android中通过Fragment进行简单的页面切换

    首先是activity中的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constr ...

  4. 一次线上nohup.out日志丢失的问题

    今天有小伙伴求助,线上一个应用的nohup.out日志不更新了,但进程还是正常的.此时需要查看这个日志排查一些问题,这可怎么办呢? nohup.out文件的更新时间停留在了昨天9点36,日志也刚好打到 ...

  5. 实验一Git代码版本管理

    GIT代码版本管理 实验目的: 1)了解分布式分布式版本控制系统的核心机理: 2) 熟练掌握git的基本指令和分支管理指令: 实验内容: 1)安装git 2)初始配置git ,git init git ...

  6. vue history模式 ios微信分享 踩过的坑

    背景:教育项目,整体依赖于微信环境,涉及到微信分享.微信二次分享 问题:vue使用history模式在iso微信下分享设置出错(签名认证错误.分享设置失败) 问题发现路径 1.按照微信公众号官方文档设 ...

  7. JavaScript自学笔记(2)---function a(){} 和 var a = function(){}的区别(javascript)

    function a(){} 和 var a = function(){}的区别: 学习做浮窗,看到别人的代码里有: window.onresize = function(){ chroX = doc ...

  8. 基于Docker的Consul集群实现服务发现

    服务发现 其实简单说,服务发现就是解耦服务与IP地址之间的硬绑定关系,以典型的集群为例,对于集群来说,是有多个节点的,这些节点对应多个IP(或者同一个IP的不同端口号),集群中不同节点责任是不一样的. ...

  9. 纪中10日T1 2313. 动态仙人掌

    纪中10日 2313. 动态仙人掌 (File IO): input:dinosaur.in output:dinosaur.out 时间限制: 1500 ms  空间限制: 524288 KB  具 ...

  10. ftp 拉去远程文件脚本

    ftp 拉去远程文件脚本 cat ftp.sh #!/bin/bash ftp -i -n 192.168.1.1 << EOF user ftpadmin gaofeng binary ...