//整体二分
#include <cstring>
#include <cstdio>
#include <iostream>
#define low(x) ((x)&(-x))
#define m(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
template<class T>void rd(T &x)
{
x=0;int f=0;char ch=getchar();
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return;
}
const int N=2e5+5,INF=0x3f3f3f3f;
struct node{int l,r,k,op,id;}q[N<<1],a[N<<1],b[N<<1];
int ans[N],c[N],n;
void add(int x,int val){
while(x<=n) c[x]+=val,x+=low(x);
}
int getsum(int x){
int res=0;
while(x) res+=c[x],x-=low(x);
return res;
}
void _clear(int x){
while(x<=n&&c[x]) c[x]=0,x+=low(x);
}
void whole(int l,int r,int L,int R)
{
if(L>R) return;
if(l==r){
for(int i=L;i<=R;++i)
if(q[i].op==2) ans[q[i].id]=l;
return;
}
int mid=(l+r)>>1,na=0,nb=0;
for(int i=L;i<=R;++i)
{
if(q[i].op==1)
{
if(q[i].l<=mid) add(q[i].id,q[i].r),a[++na]=q[i];
else b[++nb]=q[i];
}
else
{
int res=getsum(q[i].r)-getsum(q[i].l-1);
if(res>=q[i].k) a[++na]=q[i];
else b[++nb]=q[i],b[nb].k-=res;
}
}
for(int i=1;i<=na;++i)
{
if(a[i].op==1) _clear(a[i].id);
q[L+i-1]=a[i];
}
for(int i=1;i<=nb;++i) q[L+na-1+i]=b[i];
whole(l,mid,L,L+na-1);
whole(mid+1,r,L+na,R);
}
int main()
{
int m;rd(n),rd(m);
int l,r,k;
for(int i=1;i<=n;++i) rd(l),q[i]=(node){l,1,0,1,i};
for(int i=1;i<=m;++i) rd(l),rd(r),rd(k),q[n+i]=(node){l,r,k,2,i};
whole(-INF,INF,1,n+m);
for(int i=1;i<=m;++i) printf("%d\n",ans[i]);
}
P3834 【模板】(主席树/整体二分)
最新推荐文章于 2021-06-08 11:22:40 发布