「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. JavaScript实现文字跑马灯

    其实实现文字的跑马灯和实现图片轮播的原理是一样的. 下面是我自己实现的,文字的位置可以随便更改,效果不会变,文字的内容可以通过ajax获取,同时,可以直接用Jquery改写一下,很方便. <!D ...

  2. python自动化常见问题汇总

           1.如何提高selenium脚本的执行速度? Selenium脚本的执行速度受多方面因素的影响,如网速,操作步骤的繁琐程度,页面加载的速度,以及我们在脚本中设置的等待时间,运行脚本的线程 ...

  3. springboot注解@SpringBootApplication分析

    @SpringBootApplication注解用在Spring Boot的入口类上面,是Spring Boot提供的应用启动相关的注解. 直接上注解的源码: @Target(ElementType. ...

  4. 5 Expressing future time

    1 英语中表达将来的时间有四种主要方式:be going to, will, 现在进行时,一般现在时. 2 Make a prediction. 若要预测将来, 可以使用 be going to 或者 ...

  5. 第四周作业&&结对编程

    1. 结对编程. 本周开始,和我结对编程的小伙伴是齐嘉亮(博客:http://www.cnblogs.com/zhengrui0452/). 因为这周需要发布四人团队项目的alpha版本,刚好我和亮哥 ...

  6. Codeforces 1154C Gourmet Cat

    题目链接:http://codeforces.com/problemset/problem/1154/C 题目大意: 主人有一只猫.周一&周四&周日:吃鱼周二&周六:吃兔子周三 ...

  7. bootstrap.css.map 404

    删除bootstrap.css的最后一行即可: /*# sourceMappingURL=bootstrap.css.map */ English: from bootstrap-theme.css  ...

  8. 如何使用 Yum Repository 安装指定版本的 MySQL

    自从从使用 debian 系的 apt-get 转到使用 yum 工具之后一直不是很习惯,也没有去看过很多工具包安装的时候到底影响到了哪些文件等.这次借这次社区版 MySQL 安装来一并梳理一下. 首 ...

  9. 在阿里云上部署 Postfix

    Postfix 可以很方便的在一台机器上部署 smtp 服务,在 centos 上来说的话可以使用: sudo yum install postfix sudo systemctl enable po ...

  10. 根据request获取请求客户端的外网ip

    //根据request获取外网ip private static String getRemoteIp(HttpServletRequest request) { //x-forwarded-for: ...