[BZOJ3678]wangxz与OJ-[Splay一类的平衡树]
Description
Solution
直接splay搞定吧。。似乎非旋treap也ok?
我已经菜到模板题都写不出来了qaq
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,A[];
struct Splay
{
int rt,cnt=;
int ch[][],num[][],sz[],fa[];
void pushup(int k){ sz[k]=sz[ch[k][]]+sz[ch[k][]]+num[k][]-num[k][]+;}
void build(int &k,int l,int r)
{
k=++cnt;
int mid=(l+r)/;
num[k][]=num[k][]=A[mid];
if (l<mid) build(ch[k][],l,mid-);
if (mid<r) build(ch[k][],mid+,r);
fa[ch[k][]]=fa[ch[k][]]=k;
pushup(k);
}
void rotate(int x,int &rt)
{
int y=fa[x],k=ch[y][]==x;
ch[y][k]=ch[x][k^];fa[ch[y][k]]=y;
if (y==rt) rt=x;else ch[fa[y]][ch[fa[y]][]==y]=x;
fa[x]=fa[y];
fa[y]=x;ch[x][k^]=y;
pushup(y);pushup(x);
}
void splay(int x,int &rt)
{
int y;
while (x!=rt)
{
y=fa[x];
if (y!=rt&&(ch[fa[y]][]==y)==(ch[y][]==x)) rotate(y,rt);
rotate(x,rt);
}
}
int work(int x,int k)
{
if (sz[ch[x][]]>=k) return work(ch[x][],k);
if (sz[x]-sz[ch[x][]]<k) return work(ch[x][],k-sz[x]+sz[ch[x][]]);
k-=sz[ch[x][]];int son;
if (k!=)
{
son=++cnt;
num[son][]=num[x][];num[son][]=num[son][]+k-;
num[x][]=num[son][]+; fa[son]=x;
ch[son][]=ch[x][];
ch[x][]=son;
pushup(son);
k=;
}
if (k!=num[x][]-num[x][]+)
{
son=++cnt;
num[son][]=num[x][]+k;
num[son][]=num[x][];
num[x][]=num[son][]-; fa[son]=x;
ch[son][]=ch[x][];
ch[x][]=son;
pushup(son);
}
return x;
}
void insert(int p,int a,int b)
{
int x1=work(rt,p),x2=work(rt,p+);
splay(x1,rt);
splay(x2,ch[x1][]);
int son=++cnt;
num[son][]=a;num[son][]=b;fa[son]=x2;
ch[x2][]=son;
sz[son]=b-a+;
pushup(x2);
pushup(x1);
}
void remove(int a,int b)
{
int x1=work(rt,a-),x2=work(rt,b+);
splay(x1,rt);
splay(x2,ch[x1][]);
ch[x2][]=;
pushup(x2);
pushup(x1);
}
int query(int x,int q)
{
if (!x) return ;
if (sz[ch[x][]]>=q) return query(ch[x][],q);
if (sz[x]-sz[ch[x][]]>=q)
{q-=sz[ch[x][]];return num[x][]+q-;}
return query(ch[x][],q-sz[x]+sz[ch[x][]]); }
}splay;
int _type,p,a,b;
int main()
{
scanf("%d%d",&n,&m);
n++;
for (int i=;i<=n;i++) scanf("%d",&A[i]);
n++;
splay.build(splay.rt,,n);
for (int i=;i<=m;i++)
{
scanf("%d",&_type);
if (!_type)
{
scanf("%d%d%d",&p,&a,&b);p++;
splay.insert(p,a,b);
}
if (_type==)
{
scanf("%d%d",&a,&b);a++;b++;
splay.remove(a,b);
}
if (_type==)
{
scanf("%d",&p);p++;
printf("%d\n",splay.query(splay.rt,p));
}
}
}
[BZOJ3678]wangxz与OJ-[Splay一类的平衡树]的更多相关文章
- 【BZOJ3678】wangxz与OJ Splay
[BZOJ3678]wangxz与OJ Description 某天,wangxz神犇来到了一个信息学在线评测系统(Online Judge).由于他是一位哲♂学的神犇,所以他不打算做题.他发现这些题 ...
- BZOJ3678 wangxz与OJ (平衡树 无旋treap)
题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...
- BZOJ3678: wangxz与OJ
splay缩点. #include<bits/stdc++.h> #define L(t) (t)->c[0] #define R(t) (t)->c[1] #define F ...
- bzoj 3678 wangxz与OJ
3678: wangxz与OJ Time Limit: 10 Sec Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem.php ...
- 洛谷P3285 [SCOI2014]方伯伯的OJ 动态开点平衡树
洛谷P3285 [SCOI2014]方伯伯的OJ 动态开点平衡树 题目描述 方伯伯正在做他的 \(Oj\) .现在他在处理 \(Oj\) 上的用户排名问题. \(Oj\) 上注册了 \(n\) 个用户 ...
- 【bzoj3678】wangxz与OJ
Portal -- > bzoj 3678 Solution 这题==真实智力康复qwq 然而众多神犇都说是10min写完的题我..可能写了近1h吧==深深感受到自己的弱小qwq (丢上来是因为 ...
- 【BZOJ3678】Wangxz和OJ
题意: 不想讲 题解: Rope真香! 正解是Splay缩点,访问时再拆成一个序列 代码: //STL dafa good! #include<algorithm> #include< ...
- BZOJ 3595: [Scoi2014]方伯伯的Oj Splay + 动态裂点 + 卡常
Description 方伯伯正在做他的Oj.现在他在处理Oj上的用户排名问题. Oj上注册了n个用户,编号为1-”,一开始他们按照编号排名.方伯伯会按照心情对这些用户做以下四种操作,修改用户的排名和 ...
- luogu P3285 [SCOI2014]方伯伯的OJ splay 线段树
LINK:方伯伯的OJ 一道稍有质量的线段树题目.不写LCT splay这辈子是不会单独写的 真的! 喜闻乐见的是 题目迷惑选手 \(op==1\) 查改用户在序列中的位置 题目压根没说位置啊 只有排 ...
随机推荐
- js实现点击按钮显示某个区域 然后点击页面中任意其他位置,隐藏该区域
$(".licat-header-list").on("click",function(e){ $(this).addClass("active&qu ...
- 协议森林03 IP接力赛 (IP, ARP, RIP和BGP协议)
网络层(network layer)是实现互联网的最重要的一层.正是在网络层面上,各个局域网根据IP协议相互连接,最终构成覆盖全球的Internet.更高层的协议,无论是TCP还是UDP,必须通过网络 ...
- burpsuit常用功能
1.生成GET数据包:复制url -> 打开burp -> repeater -> 右键paste url as request 2.生成POST数据包:生成一个GET数据包 -&g ...
- ethereumjs/merkle-patricia-tree-2-API
SecureTrie src/secure.js:10-15 Extends Trie 扩展前缀树 You can create a secure Trie where the keys are au ...
- 6 spark 存储体系
6.1 block存储体系 存储体系架构图 6.2 block 信息信息管理器 6.2.2 bock锁的实现 6.3 磁盘block管理 /* * Licensed to the Apache Sof ...
- ThinkPHP5入门(三)----模型篇
一.操作数据库 1.数据库连接配置 数据库默认的相关配置在项目的application\database.php中已经定义好. 只需要在模块的数据库配置文件中配置好当前模块需要连接的数据库的配置参数即 ...
- Samba服务器的配置与使用
1.系统环境变量 Fedora: yum install libacl-devel libblkid-devel gnutls-devel \ readline-devel python-devel ...
- Linux 下让终端走代理的方法
转载: https://blog.fazero.me/2015/09/15/%E8%AE%A9%E7%BB%88%E7%AB%AF%E8%B5%B0%E4%BB%A3%E7%90%86%E7%9A%8 ...
- import 本质
一. 模块:用来从逻辑上来组织python代码(变量,函数,类,逻辑,实现一个功能),本质就是,py结尾的python文件 1.1 导入方法: import module import module1 ...
- Oracle 11g DataGuard搭建(一) - 单节点到单节点
(一)DataGuard概要 DataGuard中文称为”数据卫士“,提供了数据库高可用性.数据保护和灾难恢复的功能.DataGuard通过建立primary数据库和standby数据库来确立参照关系 ...