HDU 4417 Super Mario 划分树
来源:互联网 发布:2017淘宝账号怎么贷款 编辑:程序博客网 时间:2024/06/06 16:44
题意:给出长度为N的序列。有M个询问。询问有如下形式:L,R,H. 要求出[L,R]小于等于H的数字的个数。
思路:和划分树相关。对于给定的L,R,H,小于等于H的个数,等价于,就是求H是区间的第几小。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100005;const int M = 20;int aleft[M][N];int tree[M][N];int n,m;int a[N];void bulidtree(int l,int r,int dep){ if(l==r) { tree[dep+1][l]=tree[dep][l]; return ; } int mid=(l+r)/2; int x=a[mid]; int lsame=l-1; for(int i=l; i <=r; ++i) if(tree[dep][i]<x) lsame++; int newl=l,newr=mid+1; for(int i=l; i <=r; ++i) { if(tree[dep][i]<x) tree[dep+1][newl++]=tree[dep][i]; else if(tree[dep][i]==x && lsame<mid) tree[dep+1][newl++]=tree[dep][i],lsame++; else tree[dep+1][newr++]=tree[dep][i]; aleft[dep][i]=aleft[dep][l-1]+newl-l; } bulidtree(l,mid,dep+1); bulidtree(mid+1,r,dep+1);}int query(int L,int R,int l,int r,int dep,int h){ if(l==r) { if(tree[dep][l]<=h) return 1; else return 0; } if(l>r) return 0; int mid=(L+R)/2; int s=aleft[dep][r]-aleft[dep][l-1]; int ss=aleft[dep][l-1]-aleft[dep][L-1]; int e=r-l+1-s; int ee=l-L-ss; int x=a[mid]; if(x<=h) { int newl=mid+ee+1; int newr=mid+ee+e; return s+query(mid+1,R,newl,newr,dep+1,h); } else { int newl=L+ss; int newr=newl+aleft[dep][r]-aleft[dep][l-1]-1; return query(L,mid,newl,newr,dep+1,h); }}int main(){ //freopen("main.in","r",stdin); int l,r,h; int n,m; int T,cas = 1; scanf("%d", &T); while(T--){ scanf("%d %d", &n,&m); for(int i = 1; i <= n; ++i){ scanf("%d", &tree[0][i]); a[i] = tree[0][i]; } sort(a+1,a + n + 1); bulidtree(1,n,0); printf("Case %d:\n",cas++); for(int i = 0 ; i < m; ++i){ scanf("%d %d %d", &l, &r, &h); printf("%d\n",query(1,n,l+1,r+1,0,h)); } } return 0;}
0 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(划分树+二分)
- Mysql 中insert语句的性能优化
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项
- 啊开始打卡机合适的空间安徽省的开奖号
- java.sql.SQLException: No database selected
- python模拟登陆
- HDU 4417 Super Mario 划分树
- JAVA 反射机制
- 低功耗STM32L-Discovery开发板评测(二)
- 将DVWA中SQL注入漏洞修改为POST提交方式
- 性能优化(七) - 后台程序
- c语言之可变参数函数
- 杨辉三角
- C#验证码
- adt lauching project