「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. Shell脚本命令图片

    查看相关文档:shell脚本1  shell脚本2

  2. 单个源文件下CmakeList.txt

    单个源文件下CmakeList.txt 1. main.c代码 & CmakeLists.txt 文件内容 在任意自己选定的目录下(t1/)编写main.c 与 CmakeLists.txt ...

  3. Sigma Function

    做完这道题,我明白了人生的一个巨大道理,那就是: 其他题研究两下,做出来几百行.数论码字前研究半天,做出来十几二十行.做完特别没有成就感... 首先说下这题题意:首先,定义一个函数f[n],即为他所有 ...

  4. Angular 基本指令

    <!DOCTYPE html><html ng-app><head lang="en"> <meta charset="UTF- ...

  5. java中级——集合框架【1】-ArrayList

    集合框架----ArrayList 引子:我们先来看看传统数组的用法 写一个Hero对象类 package cn.jse.t1; public class Hero { public String n ...

  6. Python自动化运维ansible从入门到精通

    1. 下载安装 在windows下安装ansible:

  7. 一、IntelliJ IDEA创建java项目

    一.IntelliJ IDEA创建java项目 二.IntelliJ IDEA下载并包含jdbc包 1.下载zip格式的驱动包:https://dev.mysql.com/downloads/conn ...

  8. Mybatis-java.lang.RuntimeException: org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in sqlmap/User.xml ### Cause: org.apache.ibatis.builder.B

    mappers(映射器) 使用相对于类路径的资源 如:<mapper resource="sqlmap/User.xml" /> 使用完全限定路径 如:<mapp ...

  9. Linux CentOS7 开启80,443端口外网访问权限

    一.查看系统防火墙状态(如果返回 running 代表防火墙启动正常) firewall-cmd --state 二.开启端口外网访问 1.添加端口 返回 success 代表成功(--permane ...

  10. Spring Boot 构建电商基础秒杀项目 (八) 商品创建

    SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...