平衡树蒟蒻,敲了半天。

其实思路很简单,就是把许多个人合并成一个区间。必要的时候再拆开。(是不是和这个题的动态开点线段树有异曲同工之妙?)

每次操作最多多出来6个点。

理论上时间复杂度是nlogn,空间复杂度实际远远小于上界,其实4*n即可。

出错点:

1.pushup把哨兵sz变成了1:pushup要特判x!=0(比较保险)

2.fa和son的边一定要成对一起添加!!t[t[y].ch[0]=t[x].ch[0]].fa=y 压行的话不容易漏。

3.对于一列的情况,可能删除之后整个树空了。但是如果直接找的话,因为哨兵0有奇奇怪怪的左右儿子和father,所以,一路上会把0的sz变成1,并且访问到奇奇怪怪的点23333

那就特判如果没有根的话,那就直接建立即可。

代码:

#include<bits/stdc++.h>
#define reg register int
#define il inline
using namespace std;
typedef long long ll;
const int N=+;
struct node{
int ch[];
int sz;
int L,R;
int fa;
ll val;
}t[N*];
int tot;
void rd(int &x){
char ch;x=;
while(!isdigit(ch=getchar()));
for(x=ch^'';isdigit(ch=getchar());x=x*+(ch^''));
}
int rt[N];
int now;
int n,m,q;
void pushup(int x){
if(x)
t[x].sz=t[t[x].ch[]].sz+t[t[x].ch[]].sz+t[x].R-t[x].L+;
}
void rotate(int x){
int y=t[x].fa,d=(t[y].ch[]==x);
t[t[y].ch[d]=t[x].ch[!d]].fa=y;
t[t[x].fa=t[y].fa].ch[t[t[y].fa].ch[]==y]=x;
t[t[x].ch[!d]=y].fa=x;
pushup(y);
}
void splay(int x,int f){
while(t[x].fa!=f){
int y=t[x].fa,z=t[y].fa;
if(z!=f){
rotate((t[y].ch[]==x)==(t[z].ch[]==y)?y:x);
}
rotate(x);
}
pushup(x);
if(f==) rt[now]=x;
}
ll get(int pos){
if(now==n+){
return (ll)pos*m;
}
return (ll)(now-)*m+pos;
}
void ins(int x,ll c){
if(x==){
++tot;
t[tot].fa=;
t[tot].val=c;
t[tot].L=t[tot].R=;
pushup(tot);
rt[now]=tot;
}
else{
while(t[x].ch[]) t[x].sz++,x=t[x].ch[]; ++tot;
t[x].ch[]=tot;
t[tot].fa=x;
t[tot].L=t[tot].R=;t[tot].val=c;
t[tot].sz=;
pushup(x);
splay(tot,);
int tmp=rt[now];
}
}
ll query(int x,int k){//and dele
int d=k-t[t[x].ch[]].sz;
if(d<=) return query(t[x].ch[],k);
else if(t[x].R-t[x].L+<d) return query(t[x].ch[],d-(t[x].R-t[x].L+));
else{//find position
ll ret=;
if(t[x].R>t[x].L){
if(d>){
++tot;
t[tot].L=t[x].L;t[tot].R=t[x].L+d-;
t[t[tot].ch[]=t[x].ch[]].fa=tot;
t[t[x].ch[]=tot].fa=x;
pushup(tot);
}
if(d<t[x].R-t[x].L+){
++tot;
t[tot].L=t[x].L+d;t[tot].R=t[x].R;
t[t[tot].ch[]=t[x].ch[]].fa=tot;
t[t[x].ch[]=tot].fa=x;
pushup(tot);
}
t[x].val=get(t[x].L+d-);
t[x].L=t[x].R=;
}else if(t[x].R){
t[x].val=get(t[x].R);
t[x].L=t[x].R=;
}
pushup(x); ret=t[x].val; splay(x,);
int son=t[x].ch[];
if(!son){
t[rt[now]=t[x].ch[]].fa=;
pushup(rt[now]);
}
else{
while(t[son].ch[]) son=t[son].ch[];
splay(son,rt[now]); t[t[son].ch[]=t[x].ch[]].fa=son;
t[son].fa=;
pushup(son);
rt[now]=son;
}
return ret;
}
}
int main(){
rd(n);rd(m);rd(q);
int x,y;
for(reg i=;i<=n;++i){
rt[i]=++tot;
t[tot].L=,t[tot].R=m-;
t[tot].sz=m-;
t[tot].fa=;
}
rt[n+]=++tot;
t[tot].L=,t[tot].R=n;
t[tot].sz=n;
while(q--){
rd(x);rd(y);
ll id=;
if(y==m) now=n+,id=query(rt[now],x);
else now=x,id=query(rt[now],y);
printf("%lld\n",id);
now=n+;
ins(rt[now],id);
if(y!=m){
now=n+;
id=query(rt[now],x);
now=x;
ins(rt[now],id);
}
}
return ;
}

NOIP2017 列队——平衡树的更多相关文章

  1. [UOJ#334][NOIP2017]列队 平衡树/线段树/树状数组

    题目链接 题意不说了,一辈子也忘不掉 解法1.平衡树 这题就是平衡树裸题,每一行开一棵维护前 \(m-1\) 个,最后一列单独维护,因为很多人没有用到,所以平衡树每个节点是一个区间(pair),分裂时 ...

  2. [NOIP2017]列队 离线+SBT

    [NOIP2017]列队 题目描述 Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵 ...

  3. 题解[NOIP2017] 列队

    题解[NOIP2017] 列队 题面 解析 看到这题时感觉这个编号很难维护啊? 后来看了lzf大佬的题解才会.. 首先,考虑一个稍微暴力的做法, 维护每一行的前\(m-1\)个人和最后一列的\(n\) ...

  4. [NOIP2017] 列队(平衡树)

    考虑转化题意: 设这次操作删掉点\((x, y)\) 对于每一次向左看齐:在第x行删除\((x, y)\),并将y以后的点全部前移一位 对于每一次向前看齐:x以后的点全部上移一位,并在最后一列插入\( ...

  5. [NOIP2017]列队 (Splay)

    题目链接 NOIP2017真的是不按常理出牌: 1.数学题不在Day2T1 2.一道水题一道细节极多的模拟题一道不知道怎么形容的题(小凯的疑惑)(因为我太菜了) 3.3道大火题 当时看到列队这题是毫无 ...

  6. NOIP2017列队(phalanx)解题报告

    列队作为NOIP2017最后一道题,其实并不难,只是相对于其它题目,有点小小的工业 首先,这道题我用splay维护的,如果你不会splay,又想学一下splay,可以来这里学一学,接下来步入正题 首先 ...

  7. NOIP2017 列队 题解报告【56行线段树】

    题目描述 Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n \times mn×m名学生,方阵的行数 ...

  8. NOIP2017 列队——动态开点线段树

    Description: Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为  ...

  9. Luogu 3960 [NOIP2017] 列队 - splay|线段树

    题解 是我从来没有做过的裂点splay... 看的时候还是很懵逼的QAQ. 把最后一列的$n$个数放在一个平衡树中, 有 $n$ 个点 剩下的$n$行数, 每行都开一个平衡树,开始时每棵树中仅有$1$ ...

随机推荐

  1. Linux命令应用大词典-第31章 设备管理

    31.1 udevadm info:查询udev数据库中的设备信息 31.2 mkdnod:创建块设备和字符设备文件 31.3 MAKEDEV:创建/dev中的设备 31.4 lsblk:列出块设备信 ...

  2. 【template、import、include】微信小程序:模板(template)、引用(import、include)说明

    模板(template): 定义模板 <template name="[String]"> <!-- 模板代码 --> <view> <t ...

  3. lintcode100 删除排序数组中的重复数字

    删除排序数组中的重复数字   给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 您在真实的 ...

  4. 从零开始的Python学习Episode 5——字典

    字典 字典是另一种可变容器模型,且可存储任意类型对象. 一.添加 (1)直接添加 dict={'name':'smilepup'} dict['age']=20 dict['name']='piggy ...

  5. SPOJ 8073 The area of the union of circles(计算几何の圆并)(CIRU)

    Description You are given N circles and expected to calculate the area of the union of the circles ! ...

  6. C# 正则表达式 最全的验证类

    ///<summary> ///验证输入的数据是不是正整数 ///</summary> ///<param name="str">传入字符串&l ...

  7. 【Spring.Net】- 环境搭建

    参考文章:http://www.cnblogs.com/GoodHelper/archive/2009/10/25/SpringNET_Config.html 一.环境下载及安装 到Spring的官方 ...

  8. 不同品牌交换机设置telnet方法

    H3C交换机:1.设置telnet system-view super password level 3 cipher ******telnet server enable user-interfac ...

  9. mysql学习之数据备份与恢复

    该文使用mysql5.5 centos6.5 64位(本人使用rpm安装mysql,数据库的安装目录默认) 一.数据备份注意事项 读锁问题:数据库(或者某个表)一旦进行读锁操作则影响数据库的写操作所以 ...

  10. [OS] 多线程--原子操作 Interlocked系列函数

    转自:http://blog.csdn.net/morewindows/article/details/7429155 上一篇<多线程--第一次亲密接触 CreateThread与_begint ...