【ZJOI2013】k大数查询 BZOJ 3110

时间:2023-03-08 16:23:04
【ZJOI2013】k大数查询 BZOJ 3110

Description

有N个位置,M个操作。操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c
如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少。

Input

第一行N,M
接下来M行,每行形如1 a b c或2 a b c

Output

输出每个询问的结果

Sample Input

2 5
1 1 2 1
1 1 2 2
2 1 1 2
2 1 1 1
2 1 2 3

Sample Output

1
2
1

HINT

【样例说明】

第一个操作 后位置 1 的数只有 1 , 位置 2 的数也只有 1 。 第二个操作 后位置 1

的数有 1 、 2 ,位置 2 的数也有 1 、 2 。 第三次询问 位置 1 到位置 1 第 2 大的数 是

1 。 第四次询问 位置 1 到位置 1 第 1 大的数是 2 。 第五次询问 位置 1 到位置 2 第 3

大的数是 1 。‍

N,M<=50000,N,M<=50000

a<=b<=N

1操作中abs(c)<=N

2操作中abs(c)<=Maxlongint

思路:

都是从浙江挂下来的考数据结构的歪风邪气←_←
一看就知道是数据结构题啦=。=
树套树。。我是写的树状数组套线段树。
外层的树状数组记录有关数字的信息,内层的线段树记录有关位置的信息。
也就是说我在[5,8]中加入了3这个数,就在外层的树状数组add_bit(3),然后在树状数组对应的节点上把[5,8]这个节点打上+1的标记。
每次对于一个询问[L,R]中第k大的数,我们先转化为求第k小,然后二分答案,寻找在[l,r]中有多少比Mid小的数,收缩上下界就行了。
p.s 网上说开树套树空间会爆,所以内层线段树要动态开点,我不知道要不要。。反正我是动态的。。不过跟静态的应该区别还是挺大的。
 #include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
#define Clear(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long LL;
typedef unsigned int Uint;
const int INF=0x3fffffff;
//==============struct declaration==============
struct Node{
Node *lc,*rc;
long long sum,add;
Node(){lc=rc=NULL;sum=add=;}
};
//==============var declaration=================
const int MAXN=;
int n,m,L,R;
long long sum[MAXN*],addv[MAXN*];
Node *BitTree[MAXN];
//==============function declaration============
int lowbit(int x){return x&-x;}
void add_bit(int x);
void add_seg(Node *&o,int l,int r);
long long query_bit(int x);
long long query_seg(Node *&i,int l,int r,long long add);
void update(Node *&o,int l,int r);
void add_num(int o,int l,int r);
long long query_num(int o,int l,int r,long long add);
//==============main code=======================
int main()
{
#define FILE__
#ifdef FILE__
freopen("input","r",stdin);
freopen("output","w",stdout);
#endif
scanf("%d%d",&n,&m);n++;
for(int i=;i<=n;i++) BitTree[i]=NULL;
while (m--){
int cmd,a,b;long long c;scanf("%d%d%d%lld",&cmd,&a,&b,&c);
if (cmd==){
L=a;R=b;
add_bit(c);add_num(,,n-);
}
else if (cmd==){
int low=,high=n-,mid;
L=a;R=b;
long long Num_Exist=query_num(,,n-,);
c=Num_Exist-c+;
while (low<high){
mid=(low+high)>>;
long long tmp=query_bit(mid);
if (tmp<c) low=mid+;
if (tmp>=c) high=mid;
}
printf("%d\n",low);
}
}
return ;
}
//================fuction code====================
void add_bit(int x){
while (x<=n){
add_seg(BitTree[x],,n-);
x+=lowbit(x);
}
}
void add_seg(Node *&o,int l,int r){
int m=(l+r)>>;
if (o==NULL) o=new(Node);
if (L<=l&&r<=R){
o->add++;
update(o,l,r);
return;
}
if (m>=L) add_seg(o->lc,l,m);
if (m<R) add_seg(o->rc,m+,r);
update(o,l,r);
}
void update(Node *&o,int l,int r){
o->sum=;
if (o->lc!=NULL) o->sum+=o->lc->sum;
if (o->rc!=NULL) o->sum+=o->rc->sum;
o->sum+=(r-l+)*o->add;
}
long long query_bit(int x){
long long res=;
while (x>){
res+=query_seg(BitTree[x],,n-,);
x-=lowbit(x);
}
return res;
}
long long query_seg(Node *&o,int l,int r,long long add){
if (o==NULL) return add*(min(r,R)-max(l,L)+);
int m=(l+r)>>;
if (L<=l&&r<=R)
return o->sum+(r-l+)*add;
long long Left=,Right=;
if (m>=L) Left=query_seg(o->lc,l,m,add+o->add);
if (m<R) Right=query_seg(o->rc,m+,r,add+o->add);
return Left+Right;
}
void add_num(int o,int l,int r){
if (L<=l&&r<=R){
addv[o]++;
sum[o]+=r-l+;
}
else{
int lc=o*,rc=o*+,m=(l+r)>>;
if (m>=L) add_num(lc,l,m);
if (m<R) add_num(rc,m+,r);
sum[o]=sum[lc]+sum[rc]+addv[o]*(r-l+);
}
}
long long query_num(int o,int l,int r,long long add){
int lc=o*,rc=o*+,m=(l+r)>>;
if (L<=l&&r<=R)
return sum[o]+add*(r-l+);
long long Left=,Right=;
if (m>=L) Left=query_num(lc,l,m,add+addv[o]);
if (m<R) Right=query_num(rc,m+,r,add+addv[o]);
return Left+Right;
}

Prob_3110