hdu 2665 Kth number
2013-08-03 00:55
148 查看
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3329 Accepted Submission(s): 1107
[align=left]Problem Description[/align]
Give you a sequence and ask you the kth big number of a inteval.
[align=left]Input[/align]
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]
[align=left]Output[/align]
For each test case, output m lines. Each line contains the kth big number.
[align=left]Sample Input[/align]
1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2
[align=left]Sample Output[/align]
2
分析:询问区间[s,t]内第k大的数
划分树:
#include<cstdio> #include<algorithm> using namespace std; const int N=100001; template<class T> inline bool scan_d(T &ret){ char c;bool sgn; if(c=getchar(),c==EOF)return 0; while(c!='-'&&(c<'0'||c>'9'))c=getchar(); sgn=(c=='-'); ret=sgn?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9')ret=ret*10+(c-'0'); if(sgn)ret=-ret; return 1; } int arr ,sorted ,val[18] ,toleft[18] ; void build(int l,int r,int d){ if(l==r)return; int m=(l+r)>>1; int same=m-l+1,i=l,j=m+1,k; for(k=l;k<=r;k++)if(val[d][k]<sorted[m])same--; for(k=l;k<=r;k++){ if(val[d][k]<sorted[m])val[d+1][i++]=val[d][k]; else if(val[d][k]==sorted[m]&&same>0){ val[d+1][i++]=val[d][k]; same--; }else val[d+1][j++]=val[d][k]; toleft[d][k]=toleft[d][l-1]+i-l; } build(l,m,d+1); build(m+1,r,d+1); } int query(int L,int R,int l,int r,int d,int k){ if(l==r)return val[d][l]; int m=(L+R)>>1; int cnt=toleft[d][r]-toleft[d][l-1]; if(cnt>=k){ int nl=L+toleft[d][l-1]-toleft[d][L-1]; int nr=nl+cnt-1; return query(L,m,nl,nr,d+1,k); }else{ int nr=r+toleft[d][R]-toleft[d][r]; int nl=nr-(r-l-cnt); return query(m+1,R,nl,nr,d+1,k-cnt); } } int main(){ int T,n,m,s,t,k,i; scanf("%d",&T); while(T--){ scan_d(n),scan_d(m); for(i=1;i<=n;i++){ scan_d(val[0][i]); sorted[i]=val[0][i]; } sort(sorted+1,sorted+n+1); build(1,n,0); while(m--){ scan_d(s),scan_d(t),scan_d(k); printf("%d\n",query(1,n,s,t,0,k)); } } return 0; }
归并树(但超时)
#include<cstdio> const int N=100002; struct MergeTree{ int l,r,m; }a[N<<2]; int arr ,val[18] ; void build(int l,int r,int d,int rt){ a[rt].l=l,a[rt].r=r,a[rt].m=(l+r)>>1; if(l==r){val[d][l]=arr[l];return ;} int m=a[rt].m; build(l,m,d+1,rt<<1); build(m+1,r,d+1,rt<<1|1); int i=l,j=m+1,k=l; while(i<=m&&j<=r){ if(val[d+1][i]<val[d+1][j])val[d][k++]=val[d+1][i++]; else val[d][k++]=val[d+1][j++]; } while(i<=m)val[d][k++]=val[d+1][i++]; while(j<=r)val[d][k++]=val[d+1][j++]; } int L,R; int lowb(int key,int d,int rt){ if(a[rt].l==a[rt].r){ return key<=val[d][a[rt].m]?a[rt].m:a[rt].m+1; } if(key<=val[d][a[rt].m])return lowb(key,d,rt<<1); else return lowb(key,d,rt<<1|1); } int find(int key,int d,int rt){ if(L<=a[rt].l&&R>=a[rt].r)return lowb(key,d,rt)-a[rt].l; int ans=0; if(L<=a[rt].m)ans+=find(key,d+1,rt<<1); if(R>a[rt].m)ans+=find(key,d+1,rt<<1|1); return ans; } int query(int l,int r,int k){ int m,tmp; while(l<r){ m=(l+r+1)>>1; tmp=find(val[0][m],0,1); if(tmp<k)l=m; else r=m-1; } return l; } int main(){ int T,n,m,i,k; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++)scanf("%d",arr+i); build(1,n,0,1); while(m--){ scanf("%d%d%d",&L,&R,&k); printf("%d\n",val[0][query(1,n,k)]); } } return 0; }
相关文章推荐
- hdu 2665 Kth number
- HDU 2665(Kth number-区间第k大[内存限制+重数])
- HDU 2665 kth number(主席树)
- hdu 2665 Kth number(主席树)
- Hdu-2665 Kth number(主席树)
- hdu 2665 Kth number(划分树模板题)
- hdu 2665 Kth number(线段树+归并树+二分)
- hdu 2665 Kth number || poj 2761 Feed the dogs || poj 2104 K-th Number
- HDU 2665 Kth number 可持久化线段树
- [HDU 2665] Kth number (主席树入门)
- HDU 2665 Kth number 主席树,函数式线段树
- hdu 2665 Kth number(主席树)
- hdu 2665 Kth number(划分树模板)
- HDU 2665 Kth number(划分树)
- HDU-2665 Kth number
- hdu 2665 Kth number (函数式线段树)+ SPOJ Count on a tree
- HDU 2665 Kth number(划分树)
- hdu 2665 Kth number 主席树
- HDU - 2665[Kth number] 主席树裸题
- HDU 2665 Kth number(划分树入门题,纯套模板)