1. 程式人生 > >hdu 2665 Kth number

hdu 2665 Kth number

title truct big blank rom acc col ott scrip

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12119 Accepted Submission(s): 3697


Problem Description Give you a sequence and ask you the kth big number of a inteval.

Input The first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]

Output For each test case, output m lines. Each line contains the kth big number.

Sample Input 1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2

Sample Output 2

Source HDU男生專場公開賽——趕在女生之前先過節(From WHU) 主席樹模板 心疼我是個智障,retrun 0 寫在for(;T--;)裏面了 技術分享 這次本來打算寫指針,各種蜜汁錯誤,樣例都跑不動。。向數組低頭。。 屠龍寶刀點擊就送
#include <algorithm>
#include <cstdio>
#define
N 105000 using std::sort; using std::unique; using std::lower_bound; struct cmt { int l,r,Size; }tr[N*20]; int a[N],b[N],t[N],tot,Size,n,m,T; int build(int l,int r) { int root=++tot; tr[root].Size=0; if(l==r) return root; int mid=(l+r)>>1; tr[root].l=build(l,mid); tr[root].r=build(mid+1,r); return root; } int update(int rt,int x) { int now=++tot; int tmp=now; tr[now].Size=tr[rt].Size+1; for(int mid,l=1,r=Size;l<=r;) { mid=(l+r)>>1; if(x<=mid) { tr[now].l=++tot; tr[now].r=tr[rt].r; rt=tr[rt].l; now=tot; r=mid-1; } else { tr[now].l=tr[rt].l; tr[now].r=++tot; rt=tr[rt].r; now=tot; l=mid+1; } tr[now].Size=tr[rt].Size+1; } return tmp; } int ask(int lx,int rx,int k) { int l=1,r=Size; for(int mid;l<=r;) { mid=(l+r)>>1; if(tr[tr[rx].l].Size-tr[tr[lx].l].Size>=k) { rx=tr[rx].l; lx=tr[lx].l; r=mid-1; } else { k-=tr[tr[rx].l].Size-tr[tr[lx].l].Size; rx=tr[rx].r; lx=tr[lx].r; l=mid+1; } } return l; } int main() { scanf("%d",&T); for(;T--;) { tot=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+1+n); Size=unique(b+1,b+1+n)-b-1; t[0]=build(1,Size); for(int i=1;i<=n;++i) a[i]=lower_bound(b+1,b+1+Size,a[i])-b; for(int i=1;i<=n;++i) t[i]=update(t[i-1],a[i]); for(int x,y,k;m--;) { scanf("%d%d%d",&x,&y,&k); printf("%d\n",b[ask(t[x-1],t[y],k)]); } } return 0; }

hdu 2665 Kth number