HDU 2665 Kth number 主席树
来源:互联网 发布:优酷播放量 淘宝 编辑:程序博客网 时间:2024/06/05 11:10
简单主席树第k小经典应用。
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int maxn=110005;int a[maxn],b[maxn];int dep[30][maxn];int tl[30][maxn];void build(int tot,int l,int r){ if(l==r) return ; int i,mid,l1,r1,s; l1=l; mid=(l+r)/2; r1=mid+1; s=mid-l+1; for(i=l;i<=r;i++){ if(dep[tot][i]<a[mid]){ s--; } } for(i=l;i<=r;i++){ if(dep[tot][i]<a[mid]){ dep[tot+1][l1++]=dep[tot][i]; } else if(dep[tot][i]==a[mid]&&s>0){ dep[tot+1][l1++]=dep[tot][i]; s--; } else{ dep[tot+1][r1++]=dep[tot][i]; } tl[tot][i]=l1-l+tl[tot][l-1]; } build(tot+1,l,mid); build(tot+1,mid+1,r);}int query(int tot,int le,int ri,int l,int r,int k){ if(l==r) return dep[tot][l]; int x,y; x=tl[tot][r]-tl[tot][l-1]; y=tl[tot][l-1]-tl[tot][le-1]; if(x>=k){ return query(tot+1,le,(le+ri)/2,le+y,le+y+x-1,k); } else{ int mid=(le+ri)/2; y=r+tl[tot][ri]-tl[tot][r]; return query(tot+1,mid+1,ri,y-(r-l+1-x)+1,y,k-x); }}int main(){ int i,n,m,t; cin>>t; while(t--){ cin>>n>>m; for(i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; } sort(a+1,a+1+n); memset(dep,0,sizeof(dep)); memset(tl,0,sizeof(tl)); for(i=1;i<=n;i++) dep[0][i]=b[i]; build(0,1,n); for(i=0;i<m;i++){ int x,y,c; scanf("%d%d%d",&x,&y,&c); // c=y-x+1-c+1; printf("%d\n",query(0,1,n,x,y,c)); } }}
0 0
- HDU 2665 Kth number 主席树
- HDU 2665 kth number(主席树)
- Hdu-2665 Kth number(主席树)
- HDU 2665 Kth number (主席树)
- HDU-2665 Kth number(主席树)
- hdu 2665 Kth number(主席树)
- 主席树(Kth number,HDU 2665)
- 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 主席树模板题
- [Hdu] 2665 Kth number(主席树模板题)
- HDU 2665 Kth number 主席树裸题
- HDOJ-2665 Kth number(主席树)
- 【PROFILE】使用Oracle的PROFILE对用户资源限制和密码限制的研究
- nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
- Spring 入门示例---绝对经典!
- Oracle分析函数
- Oracle 用户密码过期
- HDU 2665 Kth number 主席树
- java对象循环依赖问题
- 一个特殊需求的环形Buffer设计
- Markdown使用入门
- CCEditBox简介
- 第九节 常用组件 之 Source Qualifier Distinct
- oracle一条语句递归查询父子关系
- 磁盘中的文件被之后拷贝的文件挤掉了怎么办?
- 如何解决继承之间、行为中局部的需求差异?