HDU - 4417 Super Mario(分块+二分)
将给定区间分块,将每个块从小到大排序,二分查询每个块,
#include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define sc(x) scanf("%d",&(x)) using namespace std; const int maxn=1e5+10; int block,mub,l[maxn],r[maxn],belong[maxn]; int arr[maxn],n,m,tt[maxn]; void built() { block=sqrt(n); mub=n/block;if(n%mub) mub++; for(int i=1;i<=mub;i++) l[i]=(i-1)*block+1,r[i]=i*block; r[mub]=n; for(int i=1;i<=n;i++) belong[i]=(i-1)/block+1; for(int i=1;i<=n;i++) sort(tt+l[belong[i]],tt+1+r[belong[i]]); } int query(int a,int b,int c) { int ans=0; if(belong[a]==belong[b]) { for(int i=a;i<=b;i++) if(arr[i]<=c) ans++; } else { for(int i=a;i<=r[belong[a]];i++) if(arr[i]<=c) ans++; for(int i=belong[a]+1;i<belong[b];i++) ans+=upper_bound(tt+l[i],tt+r[i]+1,c)-tt-l[i]; for(int i=l[belong[b]];i<=b;i++) if(arr[i]<=c) ans++; } return ans; } int main(){ int t; sc(t); for(int ii=1;ii<=t;ii++) { sc(n),sc(m); for(int i=1;i<=n;i++) { sc(arr[i]); tt[i]=arr[i]; } built(); printf("Case %d:\n",ii); while(m--) { int l,r,c; sc(l),sc(r),sc(c); l++,r++; printf("%d\n",query(l,r,c)); } } return 0; }