【bzoj3678】wangxz与OJ
Solution
这题==真实智力康复qwq
然而众多神犇都说是10min写完的题我。。可能写了近1h吧==深深感受到自己的弱小qwq
(丢上来是因为。。我的splay实在是太垃圾了==)
其实就是splay的裸题啊qwq(然后因为过于不熟练导致写了巨久==)然后的话不同的地方就是插入的时候你不能一个一个往里面插。。因为插入的是连续的一段数,我们考虑直接将这段数存在一个点里面,在需要用其中的某个位置的时候再把这个位置分离出来
具体实现的话就是每个节点维护\(vall\)和\(valr\)就好了,提取某一个位置(假如说是第\(x\)个位置)的时候我们先找到这个位置再哪个点里面,然后把这个点(如果有的话)分成三个点,\(x\)之前的数一个点,\(x\)一个点,\(x\)之后的数一个点,然后返回一下就好了,注意一下splay中的父子关系什么的
删除\([l,r]\)位置中的数的话直接把\(l-1\)这个位置和\(r+1\)这个位置提取出来,然后大力splay:先把\(l-1\)转到根,再把\(r+1\)转到\(l-1\)的左儿子,这样\(r+1\)的整个左子树就是\([l,r]\)了,那我们直接把\(r+1\)的左儿子断掉就好了
代码大概长这个样子
#include<iostream>
#include<cstdio>
#include<cstring>
#define mp make_pair
#define Pr pair<int,int>
using namespace std;
const int N=1e5+10,S=N*10;
int n,m,ans;
namespace Splay{/*{{{*/
int ch[S][2],vall[S],valr[S],fa[S],sz[S];
int n,tot,rt;
int which(int x){return ch[fa[x]][1]==x;}
bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
int newnode(int l,int r){
ch[++tot][0]=0; ch[tot][1]=0; vall[tot]=l; valr[tot]=r;
sz[tot]=r-l+1;
return tot;
}
void pushup(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+(valr[x]-vall[x]+1);}
void rotate(int x){
int dir=which(x),f=fa[x];
if (!isroot(f)) ch[fa[f]][which(f)]=x;
fa[x]=fa[f]; fa[f]=x;
if (ch[x][dir^1]) fa[ch[x][dir^1]]=f;
ch[f][dir]=ch[x][dir^1];
ch[x][dir^1]=f;
pushup(f);
pushup(x);
}
void splay(int x,int top){
for (int f=fa[x]; fa[x]!=top; f=fa[x]){
if (fa[f]!=top)
rotate(which(f)==which(x)?f:x);
rotate(x);
}
if (!top) rt=x;
}
int _build(int l,int r){
if (l>r) return 0;
int mid=l+r>>1;
if (l==r){sz[mid]=1; return mid;}
ch[mid][0]=_build(l,mid-1);
ch[mid][1]=_build(mid+1,r);
if (ch[mid][0]) fa[ch[mid][0]]=mid;
if (ch[mid][1]) fa[ch[mid][1]]=mid;
pushup(mid);
return mid;
}
void build(int l,int r){rt=_build(l,r); tot=r;}
int get_pos(int x,int k){
if (k<=sz[ch[x][0]]) return get_pos(ch[x][0],k);
if (k>sz[ch[x][0]]+(valr[x]-vall[x]+1)) return get_pos(ch[x][1],k-(sz[ch[x][0]]+valr[x]-vall[x]+1));
int nw;
k-=sz[ch[x][0]];
if (k!=1){
nw=newnode(vall[x],vall[x]+k-2);
vall[x]=valr[nw]+1;
ch[nw][0]=ch[x][0]; fa[ch[x][0]]=nw;
ch[x][0]=nw; fa[nw]=x;
pushup(nw);
k=1;
}
if (k!=valr[x]-vall[x]+1){
nw=newnode(vall[x]+k,valr[x]);
valr[x]=vall[nw]-1;
ch[nw][1]=ch[x][1]; fa[ch[x][1]]=nw;
ch[x][1]=nw; fa[nw]=x;
pushup(nw);
}
return x;
}
Pr split(int l,int r){
int le=get_pos(rt,l),ri=get_pos(rt,r);
splay(le,0);
splay(ri,le);
return mp(le,ri);
}
void insert(int p,int l,int r){
Pr rec=split(p,p+1);
int nw=newnode(l,r);
ch[rec.second][0]=nw; fa[nw]=rec.second;
pushup(rec.second);
pushup(rec.first);
}
void del(int l,int r){
Pr rec=split(l-1,r+1);
ch[rec.second][0]=0;
pushup(rec.second);
pushup(rec.first);
}
int query(int x){
int pos=get_pos(rt,x);
return vall[pos];
}
}/*}}}*/
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
int op,x,y,p;
scanf("%d%d",&n,&m);
for (int i=2;i<=n+1;++i){
scanf("%d",&x);
Splay::vall[i]=Splay::valr[i]=x;
}
Splay::build(1,n+2);//0~n+1
for (int i=1;i<=m;++i){
scanf("%d",&op);
if (op==0){
scanf("%d%d%d",&p,&x,&y);
Splay::insert(p+1,x,y);
}
else if (op==1){
scanf("%d%d",&x,&y);
Splay::del(x+1,y+1);
}
else{
scanf("%d",&p);
printf("%d\n",Splay::query(p+1));
}
}
}
【bzoj3678】wangxz与OJ的更多相关文章
- BZOJ3678: wangxz与OJ
splay缩点. #include<bits/stdc++.h> #define L(t) (t)->c[0] #define R(t) (t)->c[1] #define F ...
- BZOJ3678 wangxz与OJ (平衡树 无旋treap)
题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...
- 【BZOJ3678】wangxz与OJ Splay
[BZOJ3678]wangxz与OJ Description 某天,wangxz神犇来到了一个信息学在线评测系统(Online Judge).由于他是一位哲♂学的神犇,所以他不打算做题.他发现这些题 ...
- bzoj 3678 wangxz与OJ
3678: wangxz与OJ Time Limit: 10 Sec Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem.php ...
- 【BZOJ3678】Wangxz和OJ
题意: 不想讲 题解: Rope真香! 正解是Splay缩点,访问时再拆成一个序列 代码: //STL dafa good! #include<algorithm> #include< ...
- [BZOJ3678]wangxz与OJ-[Splay一类的平衡树]
Description 传送门 Solution 直接splay搞定吧..似乎非旋treap也ok? 我已经菜到模板题都写不出来了qaq Code #include<iostream> # ...
- NOIp2018模拟赛三十六
好久没打模拟赛了...今天一样是两道国集,一道bzoj题 成绩:13+0+95=108 A题开始看错题了...导致样例都没看懂,结果xfz提醒我后我理解了一个我自认为正确的题意(事实证明我和xfz都错 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Online Judge(OJ)搭建(第一版)
搭建 OJ 需要的知识(重要性排序): Java SE(Basic Knowledge, String, FileWriter, JavaCompiler, URLClassLoader, Secur ...
随机推荐
- Java EE JSP编程基础
一.JSP编程介绍 JSP是实现普通静态HTML和动态HTML混合编码的技术,可以说是Servlet的一种变形,相比Servlet它更像普通的Web页面.JSP在第一次运行时会花费很长时间,原因在与其 ...
- Python基础灬dict&set
字典dict 字典使用键-值(key-value)存储,具有极快的查找速度. dict基本操作 取值 a_dict = {'name': 'jack', 'age': 18} print(a_dict ...
- android开发问题 Failed to pull selection 菜鸟记录
在eclipse中开发创建了一个sqlite数据库文件,为了查看数据库文件的内容,决定复制到PC上一看究竟,位置在data……里 当我点击ddms文件浏览里的pull a file from the ...
- c# HttpListener拒绝访问
直接记录解决步骤: 程序代码: HttpListener httpListener = new HttpListener(); httpListener.Prefixes.Add("http ...
- 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem F. Format
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:1s 空间限制:512MB 题目大意: 给定一个字符串,使用%[...] ...
- 又要开始新的征程了hhh(这次内容比较感兴趣)
因为做英雄部分,既是我比较感兴趣,又很符合这次c++学习的目的,所以我很开心. 其实从小玩的RPG,即时战略和回合制游戏不算少,对于属性方法其实都算不上陌生.但是还是在网上找了一些学习资源. http ...
- rsa加密算法,前后端实现。
前端js: 下载地址:http://travistidwell.com/jsencrypt/ js第一版本: // 对数据加密 function encrypt(enStr){ //没有加载jsenc ...
- 关于String和StringBuffer的原理
public class Foo {2. public static void main (String [] args) {3. StringBuffer a = new Strin ...
- Windows Forms编程实战学习:第三章 菜单
第三章 菜单 1,控件和容器 所有的Windows Forms控件都是从System.Windows.Forms.Control类继承的,相关类的层次结构如下图所示: MarshalByRefObje ...
- 使用windows live writer发表的博客
试插入代码 #include <iostream.h> using namespace std; int main() { cout<<"hello world&qu ...