题目大意:给定一个序列,每次询问区间第k小
BZOJ又维护。。。尼玛,题写完交不上去,各种虐心。。。。。只能去学学其它的数据结构了0.0
静态序列求区间第k小,典型的划分树,具体写法直接见百度,这里不加累述
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define M 100100 using namespace std; int n,m,a[M],b[M],c[M]; int s[20][M]; void Build_Tree(int l,int r,int dpt) { int i,mid=l+r>>1; if(l==r)return ; int l1=l,l2=mid+1; for(i=l;i<=r;i++) { if(a[i]>c[mid]||a[i]==c[mid]&&l1>mid) b[l2++]=a[i],s[dpt][i]=(i==l?0:s[dpt][i-1] ); else b[l1++]=a[i],s[dpt][i]=(i==l?1:s[dpt][i-1]+1); } memcpy( a+l , b+l , sizeof(a[0])*(r-l+1) ); Build_Tree(l,mid,dpt+1); Build_Tree(mid+1,r,dpt+1); } int getans(int l,int r,int dpt,int x,int y,int k) { int mid=l+r>>1; if(l==r) return c[mid]; int l1=(x==l?0:s[dpt][x-1]),l2=s[dpt][y]; if(k<=l2-l1) return getans(l,mid,dpt+1,l+l1,l+l2-1,k); else return getans(mid+1,r,dpt+1,(mid+1)+(x-l-l1),(mid+1)+(y-l+1-l2)-1,k-l2+l1); } int main() { int i,x,y,k; cin>>n>>m; for(i=1;i<=n;i++) scanf("%d",&a[i]),c[i]=a[i]; sort(c+1,c+n+1); Build_Tree(1,n,0); for(i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&k); printf("%d\n", getans(1,n,0,x,y,k) ); } }