「SCOI2014」方伯伯的 OJ

和列队有点像,平衡树点分裂维护即可

但是需要额外用个set之类的对编号查找点的位置

插入完了后记得splay,删除时注意特判好多东西


Code:

#include <cstdio>
#include <cctype>
#include <set>
const int N=2e5+10;
template <class T>
void inline read(T &x)
{
x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
}
#define ls ch[now][0]
#define rs ch[now][1]
#define fa par[now]
int siz[N],ch[N][2],L[N],R[N],par[N],tot,root;
void connect(int f,int now,int typ){ch[fa=f][typ]=now;}
int identity(int now){return ch[fa][1]==now;}
void updata(int now){siz[now]=siz[ls]+siz[rs]+R[now]+1-L[now];}
void Rotate(int now)
{
int p=fa,typ=identity(now);
connect(p,ch[now][typ^1],typ);
connect(par[p],now,identity(p));
connect(now,p,typ^1);
updata(p),updata(now);
}
void splay(int now,int to)
{
to=par[to];
if(!to) root=now;
for(;fa!=to;Rotate(now))
if(par[fa]!=to)
Rotate(identity(now)^identity(fa)?now:fa);
}
struct yuucute
{
int x,id;
yuucute(){}
yuucute(int X,int Id){x=X,id=Id;}
bool friend operator <(yuucute a,yuucute b){return a.x<b.x;}
bool friend operator ==(yuucute a,yuucute b){return a.x==b.x;}
};
std::set <yuucute> s;
std::set <yuucute>::iterator it;
#define yuulovely 1
int New(int l,int r)
{
siz[++tot]=r+1-l,L[tot]=l,R[tot]=r;
return tot;
}
int getnum(int x)
{
it=--s.upper_bound(yuucute(x,yuulovely));
return it->id;
}
void split(int now,int x)
{
if(L[now]==R[now]) return;
s.erase(yuucute(L[now],yuulovely));
s.insert(yuucute(x,now));
int lson=ls,rson=rs;
if(L[now]<x)
{
int lp=New(L[now],x-1);
s.insert(yuucute(L[now],lp));
connect(now,lp,0);
connect(lp,lson,0);
updata(lp);
}
if(x<R[now])
{
int rp=New(x+1,R[now]);
s.insert(yuucute(x+1,rp));
connect(now,rp,1);
connect(rp,rson,1);
updata(rp);
}
L[now]=R[now]=x;
}
void insl(int now,int ins)
{
++siz[now];
if(ls) insl(ls,ins);
else connect(now,ins,0);
}
void insr(int now,int ins)
{
++siz[now];
if(rs) insr(rs,ins);
else connect(now,ins,1);
}
int getlef(int now)
{
if(ls) return getlef(ls);
return now;
}
void erase(int now)
{
if(!rs) {par[root=ls]=0,ls=0,updata(now);return;}
splay(root=getlef(rs),rs);
connect(root,ls,0);
updata(root),par[root]=0;
ls=rs=0,updata(now);
}
int getrank(int now,int &x)
{
if(siz[ls]>=x) return getrank(ls,x);
x-=siz[ls];
if(x<=R[now]-L[now]+1) return now;
x-=R[now]-L[now]+1;
return getrank(rs,x);
}
int main()
{
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
int n,m;
read(n),read(m);
root=New(1,n);
s.insert(yuucute(1,root));
int op,x,y,las=0;
for(int i=1;i<=m;i++)
{
read(op),read(x);
x-=las;
if(op==1)
{
read(y),y-=las;
int now=getnum(x);
splay(now,root);
printf("%d\n",las=siz[ls]+x+1-L[now]);
split(now,x);
L[now]=R[now]=y;
s.erase(yuucute(x,yuulovely));
s.insert(yuucute(y,now));
}
else if(op==2)
{
int now=getnum(x);
splay(now,root);
printf("%d\n",las=siz[ls]+x+1-L[now]);
split(now,x);
erase(now);
insl(root,now);
splay(now,root);
}
else if(op==3)
{
int now=getnum(x);
splay(now,root);
printf("%d\n",las=siz[ls]+x+1-L[now]);
split(now,x);
erase(now);
insr(root,now);
splay(now,root);
}
else
{
int now=getrank(root,x);
printf("%d\n",las=x+L[now]-1);
splay(now,root);
}
}
return 0;
}

2019.2.23

「SCOI2014」方伯伯的 OJ 解题报告的更多相关文章

  1. 「SCOI2014」方伯伯运椰子 解题报告

    「SCOI2014」方伯伯运椰子 可以看出是分数规划 然后我们可以看出其实只需要改变1的流量就可以了,因为每次改变要保证流量守恒,必须流成一个环,在正负性确定的情况下,变几次是无所谓的. 然后按照套路 ...

  2. 「SCOI2014」方伯伯的商场之旅 解题报告

    「SCOI2014」方伯伯的商场之旅 我一开始的想法会被两个相同的集合位置去重给搞死,不过应该还是可以写的,讨论起来老麻烦. 可以先钦定在\(1\)号点集合,然后往后调整一部分. 具体一点,通过前缀和 ...

  3. 「SCOI2014」方伯伯的玉米田 解题报告

    #2211. 「SCOI2014」方伯伯的玉米田 发现是取一个最长不下降子序列 我们一定可以把一个区间加的右端点放在取出的子序列的最右边,然后就可以dp了 \(dp_{i,j}\)代表前\(i\)个玉 ...

  4. 「SCOI2014」方伯伯的商场之旅

    「SCOI2014」方伯伯的商场之旅 题目描述 方伯伯有一天去参加一个商场举办的游戏.商场派了一些工作人员排成一行.每个人面前有几堆石子.说来也巧,位置在 \(i\) 的人面前的第 \(j\) 堆的石 ...

  5. 「NOI2013」小 Q 的修炼 解题报告

    「NOI2013」小 Q 的修炼 第一次完整的做出一个提答,花了半个晚上+一个上午+半个下午 总体来说太慢了 对于此题,我认为的难点是观察数据并猜测性质和读入操作 我隔一会就思考这个sb字符串读起来怎 ...

  6. 「SDOI2017」树点涂色 解题报告

    「SDOI2017」树点涂色 我sb的不行了 其实一开始有一个类似动态dp的想法 每个点维护到lct树上到最浅点的颜色段数,然后维护一个\(mx_{0,1}\)也就是是否用虚儿子的最大颜色 用个set ...

  7. 「SCOI2015」小凸想跑步 解题报告

    「SCOI2015」小凸想跑步 最开始以为和多边形的重心有关,后来发现多边形的重心没啥好玩的性质 实际上你把面积小于的不等式列出来,发现是一次的,那么就可以半平面交了 Code: #include & ...

  8. 「SCOI2015」小凸解密码 解题报告

    「SCOI2015」小凸解密码 题意:给一个环,定义一段连续的极长\(0\)串为\(0\)区间,定义一个位置的离一个\(0\)区间的距离为这个位置离这个区间中\(0\)的距离的最小值,每次询问一个位置 ...

  9. 「SCOI2015」小凸玩矩阵 解题报告

    「SCOI2015」小凸玩矩阵 我好沙茶啊 把点当边连接行和列,在外面二分答案跑图的匹配就行了 我最开始二分方向搞反了,样例没过. 脑袋一抽,这绝壁要费用流,连忙打了个KM 然后wa了,一想这个不是完 ...

随机推荐

  1. 使用Dockerfile来构建镜像

    Dockerfile原理 创建Dockerfile Dockerfile实例 Dockerfile指令 注释 FROM MAINTAINER RUN ADD WORKDIR ENV USER COPY ...

  2. docker 修改port 端口

    修改docker容器端口映射的方法 - wesleyflagon的专栏 - CSDN博客https://blog.csdn.net/wesleyflagon/article/details/78961 ...

  3. IdentityServer4【Topic】之StartUp中的配置

    Startup 身份服务器是中间件和服务的组合.所有的配置都是在启动类中完成的. Configuring services 通过调用如下代码在DI(dependency inject,依赖注入)中添加 ...

  4. C# Note29: Close()和Dispose()的区别

    待更! 深入解析Close()和Dispose()的区别

  5. vue.js实战——vue 实时时间

    created:实例创建完成后调用,此阶段完成了数据的观测等,但尚未挂载,$el还不可用,需要初始化处理一些数据时会比较有用. mounted:el挂载到实例上后调用,一般我们的第一个业务逻辑会在这里 ...

  6. python3 自动识图

    一.安装依赖库 pip install pytesseract pip install pillow 二.安装识图引擎tesseract-ocr https://pan.baidu.com/s/1Qa ...

  7. java.util.concurrent.TimeoutException: Idle timeout expired: 300000/300000 ms

    Request idle timed out at 123000 ms. That means there was no activity (read or write) for 123000 ms ...

  8. sqlmap-学习1 配置环境

    sqlmap是一款非常强大的开源sql自动化注入工具,可以用来检测和利用sql注入漏洞.它由python语言开发而成,因此运行需要安装python环境 1 安装 python (https://www ...

  9. Delphi MDI 子窗体的创建和销毁 [zhuan]

    1.如果要创建一个mdi child,先看是否有这个child 存在,如果有,则用它,如果没有再创建 //该函数判断MDI 子窗体是否存在,再进行创建和显示function isInclude(for ...

  10. 线程同步Volatile与Synchronized(一)

    volatile 一.volatile修饰的变量具有内存可见性 volatile是变量修饰符,其修饰的变量具有内存可见性. 可见性也就是说一旦某个线程修改了该被volatile修饰的变量,它会保证修改 ...