[POJ2104]K-th Number
来源:互联网 发布:魔兽数据库 编辑:程序博客网 时间:2024/05/16 00:26
原题地址
区间k大裸题,我用的可持久化线段树,树套树过不了OLZ…
代码较挫…
AC code:
#include <cstdio>#include <algorithm>using namespace std;const int N=100001;int n,m,tot=-1;int a[N],hash[N];struct temp{ int val,rank;}y[N];struct segtree{ int l,r,sum; segtree *Lc,*Rc;}*root[N],t[32*N];void discrete(int *);void build(segtree **,int,int);void insert(int,int);int get_Kth(int,int,int);int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); discrete(a); build(&root[0],1,n); for(int i=1;i<=n;i++) insert(i,a[i]); for(int i=1;i<=m;i++){ int l,r,k; scanf("%d%d%d",&l,&r,&k); printf("%d\n",hash[get_Kth(l-1,r,k)]); } return 0;}bool cmp(temp x,temp y){ return x.val<y.val;}void discrete(int x[]){ int v=1; for(int i=1;i<=n;i++){ y[i].rank=i; y[i].val=x[i]; } sort(y+1,y+n+1,cmp); for(int i=1;i<=n;i++){ hash[v]=x[y[i].rank]; x[y[i].rank]=v; if(y[i+1].val!=y[i].val) v++; }}void build(segtree **p,int L,int R){ *p=&t[++tot]; (*p)->l=L; (*p)->r=R; (*p)->sum=0; if(L==R){ (*p)->Lc=(*p)->Rc=NULL; return ; } int mid=(L+R)>>1; build(&(*p)->Lc,L,mid); build(&(*p)->Rc,mid+1,R);}void insert(int num,int val){ segtree **last_p=&root[num-1],**new_p=&root[num]; while(1){ *new_p=&t[++tot]; (*new_p)->l=(*last_p)->l; (*new_p)->r=(*last_p)->r; (*new_p)->sum=(*last_p)->sum+1; if((*new_p)->l==(*new_p)->r) return ; int mid=((*new_p)->l+(*new_p)->r)>>1; if(val<=mid){ (*new_p)->Rc=(*last_p)->Rc; last_p=&(*last_p)->Lc; new_p=&(*new_p)->Lc; } else{ (*new_p)->Lc=(*last_p)->Lc; last_p=&(*last_p)->Rc; new_p=&(*new_p)->Rc; } }}int get_Kth(int Lnum,int Rnum,int K){ segtree *Lp=root[Lnum],*Rp=root[Rnum]; while(1){ if(Rp->l==Rp->r) return Rp->l; if(Rp->Lc->sum-Lp->Lc->sum>=K){ Lp=Lp->Lc; Rp=Rp->Lc; } else{ K-=Rp->Lc->sum-Lp->Lc->sum; Lp=Lp->Rc; Rp=Rp->Rc; } }}
0 0
- [poj2104]K-th Number
- [Poj2104]K-th Number
- [POJ2104]K-th Number
- poj2104 K-th Number
- poj2104 K-th Number
- POJ2104-K-th Number
- poj2104 K-th Number
- POJ2104:K-th Number
- poj2104 K-th Number
- POJ2104 k-th number
- 【poj2104】K-th Number
- poj2104 K-th Number
- Poj2104 K-th Number
- poj2104 K-th Number
- POJ2104-K-th Number
- POJ2104-K-th Number
- poj2104 K-th Number
- POJ2104 K-th Number
- 启动安卓模拟器时出现 The connection to adb is down, and a severe error has occured
- J2SE之基本数据类型及转换
- SVN历史版本删除 瘦身svn库
- MySQL-index
- 编写一个C程序,用于打印九九乘法表
- [POJ2104]K-th Number
- 大型网站图片服务器架构的演进
- [转]PHP ob_start() and ob_start('ob_gzhandler')
- isSubstring子串判断笔试题
- 一个社交App需实现的功能
- 随机字符串
- eclipse安装svn插件报eclipse subversion native library not available
- 关于classpath
- [POJ3264]Balanced Lineup