P3380 【模板】二逼平衡树(树套树)

题目描述

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:

  1. 查询k在区间内的排名

  2. 查询区间内排名为k的值

  3. 修改某一位值上的数值

  4. 查询k在区间内的前驱(前驱定义为严格小于x,且最大的数,若不存在输出-2147483647)

  5. 查询k在区间内的后继(后继定义为严格大于x,且最小的数,若不存在输出2147483647)

注意上面两条要求和tyvj或者bzoj不一样,请注意

输入输出格式

输入格式:

第一行两个数 n,m 表示长度为n的有序序列和m个操作

第二行有n个数,表示有序序列

下面有m行,opt表示操作标号

若opt=1 则为操作1,之后有三个数l,r,k 表示查询k在区间[l,r]的排名

若opt=2 则为操作2,之后有三个数l,r,k 表示查询区间[l,r]内排名为k的数

若opt=3 则为操作3,之后有两个数pos,k 表示将pos位置的数修改为k

若opt=4 则为操作4,之后有三个数l,r,k 表示查询区间[l,r]内k的前驱

若opt=5 则为操作5,之后有三个数l,r,k 表示查询区间[l,r]内k的后继

输出格式:

对于操作1,2,4,5各输出一行,表示查询结果

输入输出样例

输入样例#1: 复制

9 6
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5
输出样例#1: 复制

2
4
3
4
9

说明

时空限制:2s,128M

n,m \leq 5\cdot {10}^4n,m≤5⋅104 保证有序序列所有值在任何时刻满足 [0, {10} ^8][0,108]

题目来源:bzoj3196 / Tyvj1730 二逼平衡树,在此鸣谢

此数据为洛谷原创。(特别提醒:此数据不保证操作5、6一定存在,故请务必考虑不存在的情况)

 #include<cstdio>
#include<iostream>
#include<algorithm>
#define maxn 50010
#define maxm 50010*400
using namespace std;
int n,m,tot,root[maxn],a[maxn],hash[maxn*],A[],B[];
struct node{int op,opl,opr,opk;}e[maxn];
struct S_T{
int cnt,lc[maxm],rc[maxm],sum[maxm];
void insert(int &k,int l,int r,int pos,int w){
if(!k)k=++cnt;
sum[k]+=w;
if(l==r)return;
int mid=(l+r)>>;
if(pos<=mid)insert(lc[k],l,mid,pos,w);
else insert(rc[k],mid+,r,pos,w);
}
int getrank(int l,int r,int w,bool ty){
int tmp=;
if(l==r){
if(!ty)return ;
else{
for(int i=;i<=A[];i++)tmp+=sum[A[i]];
for(int i=;i<=B[];i++)tmp-=sum[B[i]];
return tmp;
}
}
for(int i=;i<=A[];i++)tmp+=sum[lc[A[i]]];
for(int i=;i<=B[];i++)tmp-=sum[lc[B[i]]];
int mid=(l+r)>>;
if(w<=mid){
for(int i=;i<=A[];i++)A[i]=lc[A[i]];
for(int i=;i<=B[];i++)B[i]=lc[B[i]];
return getrank(l,mid,w,ty);
}
else {
for(int i=;i<=A[];i++)A[i]=rc[A[i]];
for(int i=;i<=B[];i++)B[i]=rc[B[i]];
return tmp+getrank(mid+,r,w,ty);
}
}
int getnum(int l,int r,int w){
if(l==r)return hash[l];
int tmp=;
for(int i=;i<=A[];i++)tmp+=sum[lc[A[i]]];
for(int i=;i<=B[];i++)tmp-=sum[lc[B[i]]];
int mid=(l+r)>>;
if(w<=tmp){
for(int i=;i<=A[];i++)A[i]=lc[A[i]];
for(int i=;i<=B[];i++)B[i]=lc[B[i]];
return getnum(l,mid,w);
}
else{
for(int i=;i<=A[];i++)A[i]=rc[A[i]];
for(int i=;i<=B[];i++)B[i]=rc[B[i]];
return getnum(mid+,r,w-tmp);
}
}
}ST;
struct B_I_T{
void add(int pos,int key,int val){
while(pos<=n){
ST.insert(root[pos],,tot,key,val);
pos+=pos&(-pos);
}
}
int qrank(int l,int r,int k,bool ty){
A[]=;while(r)A[++A[]]=root[r],r-=r&(-r);
B[]=;while(l)B[++B[]]=root[l],l-=l&(-l);
return ST.getrank(,tot,k,ty);
}
int qnum(int l,int r,int k){
A[]=;while(r)A[++A[]]=root[r],r-=r&(-r);
B[]=;while(l)B[++B[]]=root[l],l-=l&(-l);
return ST.getnum(,tot,k);
}
void modify(int pos,int w){
A[]=;int tmp=pos;
while(pos<=n)A[++A[]]=pos,pos+=pos&(-pos);
for(int i=;i<=A[];i++)ST.insert(root[A[i]],,tot,a[tmp],-);
for(int i=;i<=A[];i++)ST.insert(root[A[i]],,tot,w,);
a[tmp]=w;
}
}BIT;
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]),hash[i]=a[i];
int opt,l,r,k,tmp=n;
for(int i=;i<=m;i++){
scanf("%d",&opt);
if(opt==)scanf("%d%d",&l,&k);
else scanf("%d%d%d",&l,&r,&k);
e[i].op=opt;e[i].opl=l;e[i].opk=k;
if(opt!=)e[i].opr=r;
if(opt!=)hash[++tmp]=k;
}
sort(hash+,hash+tmp+);
tot=unique(hash+,hash+tmp+)-(hash+);
for(int i=;i<=n;i++){
a[i]=lower_bound(hash+,hash+tot+,a[i])-hash;
BIT.add(i,a[i],);
}
for(int i=;i<=m;i++)
if(e[i].op!=)e[i].opk=lower_bound(hash+,hash+tot+,e[i].opk)-hash; for(int i=;i<=m;i++){
if(e[i].op==)printf("%d\n",BIT.qrank(e[i].opl-,e[i].opr,e[i].opk,)+);
else if(e[i].op==)printf("%d\n",BIT.qnum(e[i].opl-,e[i].opr,e[i].opk));
else if(e[i].op==)BIT.modify(e[i].opl,e[i].opk);
else if(e[i].op==){
int tmp=BIT.qrank(e[i].opl-,e[i].opr,e[i].opk,);
if(!tmp)puts("-2147483647");
else printf("%d\n",BIT.qnum(e[i].opl-,e[i].opr,tmp));
}
else{
int tmp=BIT.qrank(e[i].opl-,e[i].opr,e[i].opk,);
if(tmp==e[i].opr-e[i].opl+)puts("");
else printf("%d\n",BIT.qnum(e[i].opl-,e[i].opr,tmp+));
}
}
}

洛谷P3380 【模板】二逼平衡树(树套树)(线段树+树状数组)的更多相关文章

  1. BZOJ3196 & 洛谷3380:二逼平衡树——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3196 https://www.luogu.org/problemnew/show/P3380 (题 ...

  2. 【BZOJ】3196: Tyvj 1730 二逼平衡树(区间第k小+树套树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3196 Treap+树状数组 1WA1A,好伤心,本来是可以直接1A的,这次开始我并没有看题解,就写出 ...

  3. 洛谷P5284 [十二省联考2019]字符串问题 [后缀树]

    传送门 思路 设\(dp_i\)表示以\(i\)结尾的\(A\)串,能达到的最长长度. 然后发现这显然可以\(i\)往自己控制的\(k\)连边,\(k\)往能匹配的\(j\)连边,就是个最长路,只要建 ...

  4. 洛谷.3835.[模板]可持久化平衡树(fhq treap)

    题目链接 对每次Merge(),Split()时产生的节点都复制一份(其实和主席树一样).时间空间复杂度都为O(qlogq).(应该更大些 因为rand()?内存真的爆炸..) 对于无修改的操作实际上 ...

  5. 洛谷 P3380 【模板】二逼平衡树(树套树)-线段树套splay

    P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...

  6. 洛谷 P3380 bzoj3196 Tyvj1730 【模板】二逼平衡树(树套树)

    [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数值 查询k在 ...

  7. P3380 【模板】二逼平衡树(树套树)(线段树套平衡树)

    P3380 [模板]二逼平衡树(树套树) 前置芝士 P3369 [模板]普通平衡树 线段树套平衡树 这里写的是线段树+splay(不吸氧竟然卡过了) 对线段树的每个节点都维护一颗平衡树 每次把给定区间 ...

  8. 【BZOJ 3196】二逼平衡树 线段树套splay 模板题

    我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...

  9. 「luogu3380」【模板】二逼平衡树(树套树)

    「luogu3380」[模板]二逼平衡树(树套树) 传送门 我写的树套树--线段树套平衡树. 线段树上的每一个节点都是一棵 \(\text{FHQ Treap}\) ,然后我们就可以根据平衡树的基本操 ...

随机推荐

  1. Linux下开放防火墙端口

    方法一:1.vi /etc/sysconfig/iptables 2.-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEP ...

  2. Java之泛型深解

    泛型的内容确实很多,在上一篇Java之泛型浅解讲到了一些常用的泛型,但是还远远不够,上一篇的内容比较容易理解,这一篇我自己觉得更加难理解一些,因此,我还得想办法让它更加接地气更加容易理解,方便我和源宝 ...

  3. spring事务隔离级别以及脏读 不可重复读 幻影读

    隔离级别 声明式事务的第二个方面是隔离级别.隔离级别定义一个事务可能受其他并发事务活动活动影响的程度.另一种考虑一个事务的隔离级别的方式,是把它想象为那个事务对于事物处理数据的自私程度. 在一个典型的 ...

  4. js和jquery 两种写法 鼠标经过图片切换背景效果

    这个是javascript的写法 <img src="res/img/shop-c_32.jpg" alt="" onmouseover="th ...

  5. Python基础-读取excel

    import xlrdbook = xlrd.open_workbook('lanxia.xls')sheet = book.sheet_by_name('sheet1')rows = sheet.n ...

  6. python-管理MySQL之ConfigParser模块

    1.拷贝2.7版本的ConfigParser.py模块支持无值解析 cp /usr/local/src/Python-2.7.5/Lib/ConfigParser.py /usr/lib/python ...

  7. BEC listen and translation exercise 37

    You're supposed to do that before 10.30 in the morning, but obviously, if it's an emergency, you can ...

  8. URAL1517Freedom of Choice(后缀数组)

    Background Before Albanian people could bear with the freedom of speech (this story is fully describ ...

  9. [POI 2018] Plan Metra

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5100 [算法] 首先分两类考虑 : 1. 1 -> N的路径不经过其它节点 , ...

  10. 【LeetCode】066. Plus One

    题目: Given a non-negative integer represented as a non-empty array of digits, plus one to the integer ...