「SCOI2014」方伯伯的 OJ 解题报告
「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 解题报告的更多相关文章
- 「SCOI2014」方伯伯运椰子 解题报告
「SCOI2014」方伯伯运椰子 可以看出是分数规划 然后我们可以看出其实只需要改变1的流量就可以了,因为每次改变要保证流量守恒,必须流成一个环,在正负性确定的情况下,变几次是无所谓的. 然后按照套路 ...
- 「SCOI2014」方伯伯的商场之旅 解题报告
「SCOI2014」方伯伯的商场之旅 我一开始的想法会被两个相同的集合位置去重给搞死,不过应该还是可以写的,讨论起来老麻烦. 可以先钦定在\(1\)号点集合,然后往后调整一部分. 具体一点,通过前缀和 ...
- 「SCOI2014」方伯伯的玉米田 解题报告
#2211. 「SCOI2014」方伯伯的玉米田 发现是取一个最长不下降子序列 我们一定可以把一个区间加的右端点放在取出的子序列的最右边,然后就可以dp了 \(dp_{i,j}\)代表前\(i\)个玉 ...
- 「SCOI2014」方伯伯的商场之旅
「SCOI2014」方伯伯的商场之旅 题目描述 方伯伯有一天去参加一个商场举办的游戏.商场派了一些工作人员排成一行.每个人面前有几堆石子.说来也巧,位置在 \(i\) 的人面前的第 \(j\) 堆的石 ...
- 「NOI2013」小 Q 的修炼 解题报告
「NOI2013」小 Q 的修炼 第一次完整的做出一个提答,花了半个晚上+一个上午+半个下午 总体来说太慢了 对于此题,我认为的难点是观察数据并猜测性质和读入操作 我隔一会就思考这个sb字符串读起来怎 ...
- 「SDOI2017」树点涂色 解题报告
「SDOI2017」树点涂色 我sb的不行了 其实一开始有一个类似动态dp的想法 每个点维护到lct树上到最浅点的颜色段数,然后维护一个\(mx_{0,1}\)也就是是否用虚儿子的最大颜色 用个set ...
- 「SCOI2015」小凸想跑步 解题报告
「SCOI2015」小凸想跑步 最开始以为和多边形的重心有关,后来发现多边形的重心没啥好玩的性质 实际上你把面积小于的不等式列出来,发现是一次的,那么就可以半平面交了 Code: #include & ...
- 「SCOI2015」小凸解密码 解题报告
「SCOI2015」小凸解密码 题意:给一个环,定义一段连续的极长\(0\)串为\(0\)区间,定义一个位置的离一个\(0\)区间的距离为这个位置离这个区间中\(0\)的距离的最小值,每次询问一个位置 ...
- 「SCOI2015」小凸玩矩阵 解题报告
「SCOI2015」小凸玩矩阵 我好沙茶啊 把点当边连接行和列,在外面二分答案跑图的匹配就行了 我最开始二分方向搞反了,样例没过. 脑袋一抽,这绝壁要费用流,连忙打了个KM 然后wa了,一想这个不是完 ...
随机推荐
- Linux reboot与init 6区别
Reboot与init 6的区别 - flyingcloud_2008的专栏 - CSDN博客https://blog.csdn.net/flyingcloud_2008/article/detail ...
- [GS]uuid-ossp
uuid-ossp 原贴地址:http://postgres.cn/docs/9.6/uuid-ossp.html 关于 OSSP的含义 uuid-ossp模块提供函数使用几种标准算法之一产生通用唯一 ...
- C# Note25: .Net Core
.NET Core全面扫盲贴 .NET Core与.NET Framework.Mono之间的关系 https://www.postgresql.org/
- 剑指offer(10)
题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 思路: 如果忽略题目中 ...
- 版本控制--git+idea
- Session和Cookie介绍及常见httpcode
Cookie和Session,及常见httpcode 1.cookie和session简介: cookie是放在客户端的键值对,用来识别用户信息的,主要包括:名字,值,过期时间,路径和域.路径与域一起 ...
- python数据结构与算法第十三天【归并排序】
1.代码实现 def merge_sort(alist): if len(alist) <= 1: return alist # 二分分解 num = len(alist)/2 left = m ...
- faster rcnn训练详解
http://blog.csdn.net/zy1034092330/article/details/62044941 py-faster-rcnn训练自己的数据:流程很详细并附代码 https://h ...
- faster rcnn训练自己的数据集
采用Pascal VOC数据集的组织结构,来构建自己的数据集,这种方法是faster rcnn最便捷的训练方式
- Node.js 安装与管理
一.node安装 Windows下,官网下载 Node.js 安装包,运行安装即可, 安装成功后,可查看版本号 node -v 二.npm npm 是 node 包管理工具,随同node一起安装,安装 ...