【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 ...
随机推荐
- C#匿名参数(转载too)
匿名方法是在初始化委托时内联声明的方法. 例如下面这两个例子: 不使用匿名方法的委托: using System; using System.Collections.Generic; using Sy ...
- [ Continuously Update ] This is an *Index Page*.
The links below present papers in certain fields. Despite overlaps exist, their emphasis is markedly ...
- 4.airflow测试
1.测试sqoop任务1.1 测试全量抽取1.1.1.直接执行命令1.1.2.以shell文件方式执行sqoop或hive任务1.2 测试增量抽取2.测试hive任务3.总结 当前生产上的任务主要分为 ...
- BVT、EVT、DVT、PVT产品开发几个阶段
EVT EVT(Engineering Verification Test) 工程验证 产品开发初期的设计验证.设计者实现样品时做初期的测试验证,包括 功能和安规测试,一般由 RD(Researc ...
- “Hello World!”团队第七周召开的第一次会议
今天是我们团队“Hello World!”团队第七周召开的第一次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 ...
- 软件工程-东北师大站-第七次作业(PSP)
1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图
- centos快速安装lamp
搭建MySQL数据库 使用 yum 安装 MySQL: yum install mysql-server -y 安装完成后,启动 MySQL 服务: service mysqld restart 设置 ...
- 将java开发的wordcount程序提交到spark集群上运行
今天来分享下将java开发的wordcount程序提交到spark集群上运行的步骤. 第一个步骤之前,先上传文本文件,spark.txt,然用命令hadoop fs -put spark.txt /s ...
- 内存测试——Android Studio中对应进程的Heap
通过Android Studio的Heap查看该程序的目前占用内存大小,多次进出界面,观察内存内存大小的变化.用Heap监测应用进程使用内存情况的步骤如下: 1. 启动Android Studio—& ...
- 第195天:js---函数对象详解(call、apply)
一.call 1.call供爷法则 // 对象1 var myclass={ getAllStudentsNumbers:function(num1,num2){ return num1+num2; ...