Super Mario hdu 4417
题目描述:给定一个整数序列,有n个数。有m个询问,询问区间[L,R]内小于k的整数有多少个。
输入:第一行是整数T,表示测试用例个数。对每个测试,第一行是整数n,m。下一行是n个整数a1, a2, …, an,后面有m行,每行有3个整数L、R、k。
输出:对每个测试用例,输出m行,每行是一个询问的答案。
数据范围:1 ≤ n, m ≤ 105, 0 ≤ ai ≤10^5, 1 ≤ L ≤ R ≤ n,1 ≤ ai, k ≤10^9
思路:
主席树+二分。对于给定的H,由于满足的数字具有单调性0~H个,所以可以二分枚举,以logn的复杂度枚举所有可能的数,对于这个枚举到的数,通过主席树判断是否满足<=H,若满足,则当前的个数为l个,不断更新即可。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef int LL;
LL root[maxn],a[maxn],b[maxn],cnt=0;
struct Tree{
LL l,r,sum,total;
}tree[maxn<<5];
LL update(LL pre,LL pl,LL pr,LL x)
{
LL tr=++cnt;
tree[tr].l=tree[pre].l;
tree[tr].r=tree[pre].r;
tree[tr].sum=tree[pre].sum+1;
LL mid=(pl+pr)>>1;
if(pl!=pr)
{
if(x<=mid)
{
tree[tr].l=update(tree[pre].l,pl,mid,x);
}
else tree[tr].r=update(tree[pre].r,mid+1,pr,x);
}
return tr;
}
LL query(LL u,LL v,LL pl,LL pr,LL k)
{
if(pl==pr) return pl;
LL mid=(pl+pr)>>1;
LL res=tree[tree[v].l].sum-tree[tree[u].l].sum;
if(res>=k){
return query(tree[u].l,tree[v].l,pl,mid,k);
}
else return query(tree[u].r,tree[v].r,mid+1,pr,k-res);
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL T;cin>>T;
for(LL t=1;t<=T;t++)
{
LL n,m;cin>>n>>m;
for(LL i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i];
}
sort(b+1,b+1+n);
LL siz=unique(b+1,b+n+1)-b-1;
for(LL i=1;i<=n;i++){
LL x=lower_bound(b+1,b+1+siz,a[i])-b;
root[i]=update(root[i-1],1,siz,x);
}
cout<<"Case "<<t<<":"<<endl;
while(m--)
{
LL u,v,k;cin>>u>>v>>k;
u++;v++;
LL ans=0;
LL L=0;LL R=v-u+1;
while(L<R)
{
LL mid=(L+R+1)>>1;
LL t=query(root[u-1],root[v],1,siz,mid);///区间内第k小的数
if(b[t]<=k){
L=mid;
}
else R=mid-1;
}
cout<<L<<endl;
}
}
return 0;
}