HDU4417 Super Mario(主席树)
题目
Source
http://acm.hdu.edu.cn/showproblem.php?pid=4417
Description
Mario 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.
Input
The first line follows an integer T, the number of test data.
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.)
Output
For 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.
Sample Input
1
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
Sample Output
Case 1:
4
0
0
3
1
2
0
1
5
1
分析
题目就是给一个序列,区间查询小于等于某个数的个数。
依次插入序列各个数,建立可持久化的权值线段树,即可。。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100010 int tn,root[MAXN],lch[MAXN*17],rch[MAXN*17],sum[MAXN*17];
int x,y;
void update(int i,int j,int a,int &b){
b=++tn;
if(i==j){
sum[b]=sum[a]+1;
return;
}
lch[b]=lch[a]; rch[b]=rch[a];
int mid=i+j>>1;
if(x<=mid) update(i,mid,lch[a],lch[b]);
else update(mid+1,j,rch[a],rch[b]);
sum[b]=sum[lch[b]]+sum[rch[b]];
}
int query(int i,int j,int a,int b){
if(x>y) return 0;
if(x<=i && j<=y){
return sum[b]-sum[a];
}
int mid=i+j>>1,ret=0;
if(x<=mid) ret+=query(i,mid,lch[a],lch[b]);
if(y>mid) ret+=query(mid+1,j,rch[a],rch[b]);
return ret;
} int num[MAXN],nn;
int a[MAXN];
int main(){
int t,n,m;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
printf("Case %d:\n",cse);
scanf("%d%d",&n,&m);
nn=0;
for(int i=1; i<=n; ++i){
scanf("%d",a+i);
num[nn++]=a[i];
}
sort(num,num+nn);
nn=unique(num,num+nn)-num;
tn=0;
for(int i=1; i<=n; ++i){
x=lower_bound(num,num+nn,a[i])-num;
update(0,nn-1,root[i-1],root[i]);
}
int a,b,c;
while(m--){
scanf("%d%d%d",&a,&b,&c);
x=0; y=upper_bound(num,num+nn,c)-num-1;
printf("%d\n",query(0,nn-1,root[a],root[b+1]));
}
}
return 0;
}
HDU4417 Super Mario(主席树)的更多相关文章
- HDU4417 - Super Mario(主席树)
题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...
- HDU-4417 Super Mario,划分树+二分!
Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...
- HDU 4417 Super Mario 主席树
分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- [HDU4417]Super Mario(主席树+离散化)
传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...
- hdu4417 Super Mario (树状数组/分块/主席树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...
- hdu4417 Super Mario 树阵离线/划分树
http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others) ...
- hdu-4417 Super Mario(树状数组 + 划分树)
题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu4417 Super Mario
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
- hdu_4417_Super Mario(主席树)
题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...
随机推荐
- Fiddler将笔记本设置代理,抓取手机网络请求包
第一步:下载fiddler,下载地址:http://www.telerik.com/download/fiddler 第二步:安装fiddler,略过... 第三步:启动fiddler,启动后界面如下 ...
- canvas九宫格跑马灯
canvas九宫格跑马灯抽奖 之前用dom写了一版,部分 安卓机会卡顿,换用canvas dom版本九宫格抽奖
- phpstudy 局域网访问
安装mantis缺陷管理系统,我使用的是phpstudy集成环境.之前使用wamp,同事说phpstudy好更新php版本,所有就用phpstudy了. 今天安装好phpstudy,下载mantis安 ...
- 简化MSI在WIN10的安装
这里给大家分享一个简化MSI安装的工具 InstallByDrag: 在win10系统中,通过双击方式打开 MSI 安装文件,可能被提示由于dll加载问题无法安装,这是由于没有使用管理员权限运行.而M ...
- monkeyrunner脚本录制
1.在窗口输入 monkeyrunner monkey_recorder.py 调用录制脚本工具 2.在窗口输入 monkeyrunner monkey_playback.py d:\game ...
- vim /vi中对字符串的查找并替换
vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂的功能,记录几种在此,方便以后查询. :s/vivian/sky/ 替换当前行第一个 vivian ...
- 【Django】--Models 和ORM以及admin配置
Models 数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库 <1>sqlite django默认使用sqlite的数据库 ...
- HTML5 的一些小的整理吧
主要的就是一些HTML 5 API 的使用 也是借鉴别人的博客 ,和MDN(中文部分的还是能看的懂) 上面的一些东西 具体的代码在 有道云笔记里面也有. 先把总得列出来 1.Canvas绘图 学完这个 ...
- 【转载】maven插件mybatis-generator自动生成 (1)
http://blog.csdn.net/itlqi/article/details/49534447 1.新建一个maven项目在pom.xml添加如下: <plugins> <p ...
- WinForm任务栏最小化
在C#编写的WinForm里,在FormBorderStyle设为None的时候,任务栏点击程序图标,不会自动最小化.在主窗口WinForm.cs里加入如下代码后,即可恢复该功能. protected ...