标签:query scan unique == poj2104 div algorithm ace span
1 // poj2104 2 // p3834 3 #include<iostream> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace std; 7 const int N=1e5+7; 8 int a[N],b[N],rt[N]; 9 int cnt=0; 10 struct node{ 11 int l,r,sum; 12 }tr[N*20]; 13 void build(int& o,int l,int r){ 14 o=++cnt; 15 if(l==r) return; 16 int m=(l+r)>>1; 17 build(tr[o].l,l,m); 18 build(tr[o].r,m+1,r); 19 } 20 void change(int& neo,int pro,int l,int r,int p){ 21 neo=++cnt;tr[neo]=tr[pro];++tr[neo].sum; 22 if(l==r) return; 23 int m=(l+r)>>1; 24 if(p<=m) change(tr[neo].l,tr[pro].l,l,m,p); 25 else change(tr[neo].r,tr[pro].r,m+1,r,p); 26 } 27 int query(int rtl,int rtr,int l,int r,int k){ 28 if(l==r) return b[l]; 29 int x=tr[tr[rtr].l].sum-tr[tr[rtl].l].sum; 30 int m=(l+r)>>1; 31 if(x>=k) return query(tr[rtl].l,tr[rtr].l,l,m,k); 32 else return query(tr[rtl].r,tr[rtr].r,m+1,r,k-x); 33 } 34 int main(){ 35 int n,m;scanf("%d%d",&n,&m); 36 for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i]; 37 sort(b+1,b+1+n); 38 int num=unique(b+1,b+1+n)-b-1; 39 build(rt[0],1,num); 40 for(int i=1;i<=n;++i){ 41 int p=lower_bound(b+1,b+1+num,a[i])-b; 42 change(rt[i],rt[i-1],1,num,p); 43 } 44 for(int i=1;i<=m;++i){ 45 int l,r,k;scanf("%d%d%d",&l,&r,&k); 46 printf("%d\n",query(rt[l-1],rt[r],1,num,k)); 47 } 48 return 0; 49 }
标签:query scan unique == poj2104 div algorithm ace span
原文地址:https://www.cnblogs.com/xiaobuxie/p/11391857.html