hdu 4417 Super Mario (主席树+二分)
2015-07-28 21:51
507 查看
/* 求l,r中比k小的数的个数 主席树+二分第几小的值与k相比 */ #include <stdio.h> #include <algorithm> # include <string.h> using namespace std; # define lson l,m # define rson m+1,r # define N 100005 int a ,Hash ; int T ;///树祖宗节点的编号 int sum[N<<5];//数目 int L[N<<5];//左儿子的编号 int R[N<<5];//右儿子的编号 int tot; int build(int l,int r) { int rt=(++tot); sum[rt]=0; if(l<r) { int m=(l+r)>>1; L[rt]=build(lson); R[rt]=build(rson); } return rt; } int update(int pre,int l,int r,int x) { int rt=(++tot); L[rt]=L[pre]; R[rt]=R[pre]; sum[rt]=sum[pre]+1; if(l<r) { int m=(l+r)>>1; if(x<=m) L[rt]=update(L[pre],lson,x); else R[rt]=update(R[pre],rson,x); } return rt; } int query(int u,int v,int l,int r,int k) { if(l>=r) return l; int m=(l+r)>>1; int num=sum[L[v]]-sum[L[u]]; if(num>=k) return query(L[u],L[v],lson,k); else return query(R[u],R[v],rson,k-num); } int main() { int n,m,i,t; while(~scanf("%d",&t)) { int cas=0; while(t--) { scanf("%d%d",&n,&m); tot=0; for(i=1; i<=n; i++) { scanf("%d",&a[i]); Hash[i]=a[i]; } sort(Hash+1,Hash+n+1);//离散 int size=unique(Hash+1,Hash+n+1)-Hash-1; T[0]=build(1,size);//裸树 for(i=1; i<=n; i++) //在T[0]的基础上建n棵树 { int x=lower_bound(Hash+1,Hash+size+1,a[i])-Hash; T[i]=update(T[i-1],1,size,x); } printf("Case %d:\n",++cas); while(m--) { int l,r,k; scanf("%d%d%d",&l,&r,&k); l++; r++; int left=1; int right=r-l+1; while(left<=right) { int mid=(left+right)/2; int x=query(T[l-1],T[r],1,size,mid); if(Hash[x]>k) right=mid-1; else left=mid+1; } printf("%d\n",left-1); } } } return 0; }
相关文章推荐
- PDB文件:每个开发人员都必须知道的
- hdu5319Painter 模拟
- 提高sql查询性能-使用instr函数替换like
- 也谈在线教育产品如何激发用户持续的学习动力
- 使用Notepad++编码编译时报错(已解决?)
- Yii提供的Htmler助手checkboxList可自定义Checkbox输出格式
- CAS在登陆页停留过长时登陆后重回登陆页的问题
- C语言之函数调用14—递归法打印勒让德多项式前N项
- CxImage
- Find missing number in a sorted array
- 疑难杂症
- JAVA里的单引号和双引号及String和char的区别
- JAVA-GUI练习实例,QQ登录界面
- PAT (Advanced Level) 1069. The Black Hole of Numbers (20) 简单数字操作
- 径向基概率神经网络的实例(实现预测,分类)
- 单链表反转
- 53. Maximum Subarray
- Keepalived
- JAVA-GUI练习实例,QQ登录界面
- 【leetcode】Pow(x,n)