hdu--4417Super Mario+划分树
来源:互联网 发布:用手充电软件下载 编辑:程序博客网 时间:2024/05/20 18:44
题目链接:点击进入
这道题就不能简单的套模板了,因为他是要判断给定的区间里有多少个值小于H。思路看完题后就有了,其实我们只要二分名次k,然后将得到的值与H进行比较就行了,这样就可以求出一个高度恰好小于H的名次作为答案了。但要注意一下,使用划分树求出的第k大值,其实是第k小值,开始就被被这一点坑了。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100000+100;int tree[20][maxn];int sorted[maxn];int toleft[20][maxn];bool cmp(int a,int b){ return a>b;}void build(int l,int r,int dep){ if(l==r) return; int mid=(l+r)>>1; int same=mid-l+1; for(int i=l;i<=r;i++) if(tree[dep][i]<sorted[mid]) same--; int lpos=l; int rpos=mid+1; for(int i=l;i<=r;i++) { if(tree[dep][i]<sorted[mid]) tree[dep+1][lpos++]=tree[dep][i]; else if(tree[dep][i]==sorted[mid]&&same>0) tree[dep+1][lpos++]=tree[dep][i],same--; else tree[dep+1][rpos++]=tree[dep][i]; toleft[dep][i]=toleft[dep][l-1]+lpos-l; } build(l,mid,dep+1); build(mid+1,r,dep+1);}int query(int L,int R,int l,int r,int dep,int k){ if(l==r) return tree[dep][l]; int mid=(L+R)/2; int cnt=toleft[dep][r]-toleft[dep][l-1]; if(cnt>=k) { int newl=L+toleft[dep][l-1]-toleft[dep][L-1]; int newr=newl+cnt-1; return query(L,mid,newl,newr,dep+1,k); } else { int newr=r+toleft[dep][R]-toleft[dep][r]; int newl=newr-(r-l-cnt); return query(mid+1,R,newl,newr,dep+1,k-cnt); }}int main(){ freopen("in.txt","r",stdin); int t,Case=0; scanf("%d",&t); while(t--) { printf("Case %d:\n",++Case); int n,m; scanf("%d%d",&n,&m); memset(tree,0,sizeof(tree)); for(int i=1;i<=n;i++) { scanf("%d",&tree[0][i]); sorted[i]=tree[0][i]; } sort(sorted+1,sorted+n+1); build(1,n,0); while(m--) { int L,R,H; scanf("%d%d%d",&L,&R,&H); L++,R++; int l=1,r=R-L+1; int ans=-1; while(l<=r) { int mid=(l+r)>>1; int tmp=query(1,n,L,R,0,mid); if(tmp<=H) l=mid+1,ans=mid; else r=mid-1; } if(ans==-1) { printf("0\n"); continue; } printf("%d\n",ans); } } return 0;}
1 0
- hdu 4417 Super Mario--二分--划分树
- hdu 4417 Super Mario (划分树)
- HDU 4417 Super Mario(划分树+二分)
- HDU-4417-Super Mario(划分树+二分)
- 【划分树+二分】HDU 4417 Super Mario
- HDU 4417 Super Mario 划分树
- hdu--4417Super Mario+划分树
- hdu 4417 Super Mario(划分树)
- HDU 4417Super Mario 划分树 + 二分
- HDU 4417 Super Mario 划分树+二分
- HDU-4417 Super Mario,划分树+二分!
- hdu 4417 Super Mario 划分树(线段树)
- HDU 4417 Super Mario(划分树)#by zh
- hdu 4417 Super Mario(划分树或树状数组)
- HDU 4417 Super Mario 划分树/树状数组
- hdu 4417 Super Mario(离线树状数组|划分树)
- 【HDU】4417 Super Mario(划分树+二分)
- HDU 4417 Super Mario(划分树+二分)
- 屏蔽用户输入查询通配符‘%’或‘_’
- 去除重复字符并排序
- 用MHA实现mysql自动故障转移
- 强制停止monkey执行
- RabbitMQ 集群与高可用配置
- hdu--4417Super Mario+划分树
- JAVA String--16进制 转换
- 如何把maven项目转成web项目
- django_openstack_auth源码分析与集成
- 面试常见问题及回答 面试技巧及注意事项
- linux随笔-2
- iOS开发之判断手机号
- git的一些命令
- Android之Content Provider学习使用