HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3625 Accepted Submission(s): 1660
is world-famous plumber. His “burly” figure and amazing jumping ability
reminded in our memory. Now the poor princess is in trouble again and
Mario needs to save his lover. We regard the road to the boss’s castle
as a line (the length is n), on every integer point i there is a brick
on height hi. Now the question is how many bricks in [L, R] Mario can
hit if the maximal height he can jump is H.
For each test data:
The
first line contains two integers n, m (1 <= n <=10^5, 1 <= m
<= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
each case, output "Case X: " (X is the case number starting from 1)
followed by m lines, each line contains an integer. The ith integer is
the number of bricks Mario can hit for the ith query.
10 10
0 5 2 7 5 4 3 8 7 7
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
4
0
0
3
1
2
0
1
5
1
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN=;
int tree[][MAXN];//表示每层每个位置的值
int sorted[MAXN];//已经排序的数
int toleft[][MAXN];//toleft[p][i]表示第i层从1到i有多少个数分入左边 void build(int l,int r,int dep)
{
if(l==r)return;
int mid=(l+r)>>;
int same=mid-l+;//表示等于中间值而且被分入左边的个数
for(int i=l;i<=r;i++)
if(tree[dep][i]<sorted[mid])
same--;
int lpos=l;
int rpos=mid+;
for(int i=l;i<=r;i++)
{
if(tree[dep][i]<sorted[mid])//比中间的数小,分入左边
tree[dep+][lpos++]=tree[dep][i];
else if(tree[dep][i]==sorted[mid]&&same>)
{
tree[dep+][lpos++]=tree[dep][i];
same--;
}
else //比中间值大分入右边
tree[dep+][rpos++]=tree[dep][i];
toleft[dep][i]=toleft[dep][l-]+lpos-l;//从1到i放左边的个数 }
build(l,mid,dep+);
build(mid+,r,dep+); } //查询区间第k大的数,[L,R]是大区间,[l,r]是要查询的小区间
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)>>;
int cnt=toleft[dep][r]-toleft[dep][l-];//[l,r]中位于左边的个数
if(cnt>=k)
{
//L+要查询的区间前被放在左边的个数
int newl=L+toleft[dep][l-]-toleft[dep][L-];
//左端点加上查询区间会被放在左边的个数
int newr=newl+cnt-;
return query(L,mid,newl,newr,dep+,k);
}
else
{
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);
}
}
int n,m;
int ans;
void solve(int s,int t,int k){
int l=;
int r=t-s+;
ans=;
while(l<=r){
int mid=(l+r)>>;
int temp=query(,n,s,t,,mid);
if(temp<=k){
ans=mid;
l=mid+;
}
else
r=mid-;
}
printf("%d\n",ans);
} int main(){
int T;
int cas=;
int s,t,k;
scanf("%d",&T);
while(T--){
memset(tree,,sizeof(tree));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&tree[][i]);
sorted[i]=tree[][i];
}
sort(sorted+,sorted+n+);
build(,n,);
printf("Case %d:\n",cas++);
for(int i=;i<=m;i++){
scanf("%d%d%d",&s,&t,&k);
s++;
t++;
solve(s,t,k);
} }
return ;
}
HDU 4417 Super Mario(划分树问题求不大于k的数有多少)的更多相关文章
- HDU 4417 Super Mario(划分树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- HDU 4417 Super Mario(划分树+二分)
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- HDU 4417 Super Mario ( 离线树状数组 )
把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...
- HDU 4417 Super Mario 主席树
分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- HDU-4417 Super Mario,划分树+二分!
Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...
- HDU 4417 Super Mario
题解:函数式线段树求区间小于等于k的数有几个,离线做法,首先将所有询问和序列一起离散,然后用函数式线段树处理. #include <map> #include <cstdio> ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- I/O————字符流和流的关闭
Reader字符输入流 Writer字符输出流 用于字符的形式读取和写入数据 FileReader与FileWriter分别是Reader与Writer的子类 public class CharScr ...
- Nacos部署中的一些常见问题汇总
开个帖子,汇总一下读者经常提到的一些问题 问题一:Ubuntu下启动Nacos报错 问题描述 使用命令sh startup.sh -m standalone启动报错: ./startup.sh: 78 ...
- 安装drupal对服务器环境的要求
Drupal 8 Apache 2.x PHP 5.5.9 及以上版本 MySQL 5.5.3 及以上版本 Drupal 7 Apache 2.x PHP 5.2.5 及以上版本(推荐 PHP 5.4 ...
- plpgsql 数组、JSON相关
Function Return Type Description Example Result array_append(anyarray,anyelement) anyarray append an ...
- 用好js与nodejs中的try...catch
对异常的捕获和处理是提高程序鲁棒性的一个重要方式,即使在javascript/nodejs等看似“很难写出bug”的弱类型语言里,异常捕获处理仍至关重要,这主要是因为: 1.在一个代码块里,如果程序运 ...
- 初探ant design pro
1.增加路由子页面&配置菜单 因为ant design pro采取的是umi路由配置,所以只要在对应的文件夹下新建相关的文件夹以及文件,它会自动解析.按照如下的步骤做即可 PS.如果想要给菜单 ...
- jmeter中通过jdbc方式连接mysql数据库的配置参考
jmeter中通过jdbc方式连接mysql数据库的配置参考: Database URL=jdbc:mysql://ip:port/dbname?useUnicode=true&allowMu ...
- webpack前端构建工具学习总结(四)之自动化生成项目中的html页面
接续上文:webpack前端构建工具学习总结(三)之webpack.config.js配置文件 插件的介绍文档:https://www.npmjs.com/package/html-webpack-p ...
- 动手使用ABAP Channel开发一些小工具,提升日常工作效率
今天的故事要从ABAP小游戏说起. 中国的ABAP从业者们手头或多或少都搜集了一些ABAP小游戏,比如下面这些. 消灭星星: 扫雷: 来自我的朋友刘梦,公众号"SAP干货铺"里的俄 ...
- 开源项目: circular-progress-button
带进度条显示的按钮, 其效果如下所示: 其由三部分动画组成: 初始状态->圆环状态->完成状态. 0. 实现从初始到圆环的简单实现: 继承自button 类, 设置其背景 public c ...