「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

转载于:https://www.cnblogs.com/butterflydew/p/10421797.html

「SCOI2014」方伯伯的 OJ 解题报告相关推荐

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

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

  2. [SCOI2014]方伯伯的OJ

    看到这道题的第一想法就是要用FHQ treap 过了这道题...于是至今尚未成功(华丽的 T 掉了 (╯‵□′)╯︵┻━┻ ).于是附个地址. 然后水一波博客. 题意简介 emmmm...方伯伯脑抽做 ...

  3. 【bzoj 3595】: [Scoi2014]方伯伯的Oj

    传送门&& 原题解 蒟蒻终于做到一道方伯伯的题了-- 调了一个上午一直TLE(发现自己打了好久的splay板子竟然是错的这种丢人事情我就不说了) 很明显,要建两棵树,$T1$维护排名, ...

  4. [BZOJ3595][SCOI2014]方伯伯的OJ(裂点Splay)

    用一棵Splay按名次维护每个点,其中一个节点对应初始编号连续的一段区间,这样总节点数是$O(m)$的. 对每个编号记录这个点被Splay的那个节点维护,用std::map存储,只记录被修改的点. 每 ...

  5. 【SCOI2014】方伯伯的商场之旅【数位dp】【单峰函数】

    题意:给定 l,r,kl,r,kl,r,k ,对于一个 kkk 进制数,将数码看成这个位置的石子个数,每将一个石子移动 111 的距离需要 111 的代价.求 [l,r][l,r][l,r] 中的所有 ...

  6. 「分块系列」数列分块入门3 解题报告

    数列分块入门3 题意概括 区间加法,区间求前驱. 写在前面 这题的方法与分块2方法极其类似,建议自行解决. 正题 和上一题类似,但是二分不是用来计数的,而是用来求小于c的最大值的.然后对于不完整快,将 ...

  7. 【BZOJ3598】【SCOI2014】方伯伯的商场之旅(数位dp)

    传送门 似乎做出来之后很简单了 显然对于每一个数都是在带权中点最优 假设先把所有数都移到最后一位 再枚举有多少能往左移更优 考虑显然在找中点的时候能往左移的条件是当前位置前缀大于后缀 而前后缀之差的范 ...

  8. Heu OJ 解题报告索引

    Heu OJ 百题解: 1001:http://www.cnblogs.com/lzj-0218/p/3211971.html 3201:http://www.cnblogs.com/lzj-0218 ...

  9. 「重磅」2020下半年中国移动互联网大报告

    1. 后疫情时代,移动互联网流量回落 2020年年初,疫情这只突如其来"黑天鹅"给互联网流量带来了短暂的增长,但是移动互联网大势不变,依旧面临着增长见顶的瓶颈.2020下半年移动互 ...

最新文章

  1. SQL SERVER全面优化-------写出好语句是习惯
  2. js事件循环 microtask macrotask
  3. python coding_python开头的coding设置方法
  4. ArcIMS 开发学习笔记(一)
  5. dedecms原理及使用
  6. 《30天自制操作系统》前言、目录、样章欢迎阅读!
  7. 《是碰巧还是执着?python所阅读的每一场知识点,唯一的共同点就是——参赛选手中,有详解Python的装饰器!》
  8. lwip连续发数据卡死_LwIP用TCP连接方式在数据量比较大协议栈卡死
  9. Python进行特征提取
  10. 【数据结构 严蔚敏版】 链式栈基本操作
  11. Android Studio的适配器Adapter以及Adapterview的使用
  12. inssider序列号_AP注册问题-华三.ppt
  13. 如何通过微信小程序进行更加有效的电商营销?
  14. 深度学习-活体检测发展之数据篇(二)
  15. 【网络教程】设置远程开启,主板如何开启网络唤醒,华硕主板网络唤醒 WOL 的设置与实现!
  16. 最大进程线程数 连接数
  17. 大龄计算机考研 考研帮,以自己的亲身经历,献给那些大龄的考研朋友们,加油!...
  18. BZOJ3165: [Heoi2013]Segment
  19. 钕铁硼磁铁(NdFeB MAGNET)生产工艺流程
  20. [诈骗]“中国移动”发送诈骗短信,china mobile 是骗子吗?

热门文章

  1. html cols跨两列,html – CSS奇怪,甚至有2个cols
  2. JVM 学习三:类加载器
  3. 无法从“cstring”转换为“lpcstr”_U盘重装系统后,无法使用?
  4. java打开输入框,java – 在Android中打开输入对话框
  5. linux dev alloc name,深入理解Linux网络技术内幕-设备注册和初始化(二)
  6. java的三个环境变量是啥_JAVA中常用需要设置的三个环境变量(JAVA_HOME、CLASSPATH、PATH)...
  7. object htmldivelement什么意思_深入探究 Function amp; Object 鸡蛋问题
  8. 有趣的编程代码_iPad amp; Mac 编程游戏推荐
  9. 基于Java+SpringBoot+vue+element实现新冠疫情物资管理系统详细设计
  10. php设置表单为整数,PHP中如何判断FROM表单提交的数字是否为整数?