hdu2665 划分树
来源:互联网 发布:驾校坡道数据 编辑:程序博客网 时间:2024/06/04 19:31
模拟了快排的过程 得到的一棵树就是划分树了
静态查询区间第k大很方便
#include<stdio.h>#include<cstdio>#include<iostream>#include<algorithm>using namespace std;#define maxn 111111int tol[30][maxn],s[30][maxn];int sorted[maxn];void build(int dep,int l,int r){ if(l==r)return ; int mid=((l+r)>>1); int lpos=l,rpos=mid+1; int same=(mid-l+1); for(int i=l;i<=r;++i) if(s[dep][i]<sorted[mid]){ same--; } for(int i=l;i<=r;++i){ if(s[dep][i] < sorted[mid]) s[dep+1][lpos++] = s[dep][i]; else if(s[dep][i]==sorted[mid]&&same>0){ s[dep+1][lpos++] = s[dep][i] ; same--; } else s[dep+1][rpos++] = s[dep][i]; tol[dep][i]=tol[dep][l-1]+lpos-l; } build(dep+1,l,mid); build(dep+1,mid+1,r);}int query(int dep,int l,int r,int L,int R,int k){ if(l == r)return s[dep][l]; int cnt=tol[dep][r]-tol[dep][l-1]; int mid=((L+R)>>1); if(cnt>=k){ int newl=L+tol[dep][l-1]-tol[dep][L-1]; int newr=newl+cnt-1; return query(dep+1,newl,newr,L,mid,k); } else{ int newr=r+(tol[dep][R]-tol[dep][r]); int newl=newr-(r-l+1-cnt)+1; return query(dep+1,newl,newr,mid+1,R,k-cnt); }}int main(){ int t;scanf("%d",&t); while(t--){ int n,m,a,b,k; memset(tol,0,sizeof tol); memset(s,0,sizeof s); scanf("%d%d",&n,&m); for(int i=1;i<=n;++i){ scanf("%d",&s[0][i]); sorted[i]=s[0][i]; } sort(sorted+1,sorted+1+n); build(0,1,n); while(m--){ scanf("%d%d%d",&a,&b,&k); printf("%d\n",query(0,a,b,1,n,k)); } } return 0;}
0 0
- HDU2665之划分树
- hdu2665(划分树)
- poj2104 hdu2665 划分树
- hdu2665 划分树
- poj2104||hdu2665 归并树|划分树
- HDU2665--Kth Number(划分树)
- 划分树 hdu2665 第k小
- poj 2104 or poj2761 or hdu2665 划分树
- HDU2665 Kth number(划分树模板题)
- 划分树 图文讲解让你一次就懂 hdu2665为例
- hdu2665 主席树
- 主席树。。板子HDU2665
- HDU2665 主席树入门
- 主席树(hdu2665)
- hdu2665
- hdu2665
- hdu2665
- hdu2665 可持续化线段树
- HDU 2066 最短路
- ASP.NET MVC Model验证(四)
- PBS输出文件无法获取的问题
- 使用美橙主机建站(jsp+mysql+tomcat建站)
- python多线程threading.Condition示例及解析
- hdu2665 划分树
- 将<a>标签的href属性值设置为"#"所表示的意思
- hdu 1879 继续畅通工程
- 对比iOS网络组件:AFNetworking 和 ASIHTTPRequest
- 机器学习的一些资源
- HDU 1272 小希的迷宫 并查集
- 斐波那契数列
- shell trick
- Android开发学习笔记(四):两种方法实现图片播放器