题目大意
给你一个序列,每次求一个区间的第k大
n,m<=100000;
我的做法
离散化+整体二分
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int cc[100010];
int n;
void add(int x,int v)
{
for(;x<=n;x+=x&-x)
cc[x]+=v;
}
int sum(int x)
{
int s=0;
for(;x;x-=x&-x)
s+=cc[x];
return s;
}
void clear(int x)
{
for(;x<=n;x+=x&-x)
cc[x]=0;
}
struct p
{
int a,b,c,v;
int id;
};
p a[300010],b[300010],c[300010];
int d[100010];
int ans[200010];
void solve(int l,int r,int sl,int sr)
{
if(l>r)
return;
if(sl==sr)
{
int i;
for(i=l;i<=r;i++)
if(a[i].v==2)
ans[a[i].id]=sl;
return;
}
int i;
int l1=0,r1=0;
int j;
int sm=(sl+sr)>>1;
for(i=l;i<=r;i++)
if(a[i].v==1)
if(a[i].b<=sm)
{
add(a[i].a,a[i].c);
b[++l1]=a[i];
}
else
c[++r1]=a[i];
else
{
int s=sum(a[i].b)-sum(a[i].a-1);
if(a[i].c<=s)
b[++l1]=a[i];
else
{
a[i].c-=s;
c[++r1]=a[i];
}
}
for(i=l;i<=r;i++)
if(a[i].v==1&&a[i].b<=sm)
clear(a[i].a);
for(i=l,j=1;j<=l1;i++,j++)
a[i]=b[j];
for(j=1;j<=r1;i++,j++)
a[i]=c[j];
solve(l,l+l1-1,sl,sm);
solve(l+l1,r,sm+1,sr);
}
int now[100010];
int e[100010];
int main()
{
freopen("hdu2665.in","r",stdin);
freopen("hdu2665.out","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
int m;
scanf("%d%d",&n,&m);
memset(cc,0,sizeof(cc));
memset(now,0,sizeof(now));
memset(d,0,sizeof(d));
memset(ans,0,sizeof(ans));
int i;
int tot=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i].b);
a[i].a=i;
a[i].c=1;
a[i].v=1;
a[i].id=0;
now[i]=a[i].b;
e[++tot]=a[i].b;
}
sort(e+1,e+tot+1);
e[0]=0x80000000;
tot=unique(e+1,e+tot+1)-e-1;
for(i=1;i<=n;i++)
a[i].b=lower_bound(e+1,e+tot+1,a[i].b)-e;
int q=n;
for(i=1;i<=m;i++)
{
a[++q].v=2;
scanf("%d%d%d",&a[q].a,&a[q].b,&a[q].c);
a[q].id=i;
}
solve(1,q,1,tot);
for(i=1;i<=m;i++)
printf("%d\n",e[ans[i]]);
}
return 0;
}