题目

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(主席树)的更多相关文章

  1. HDU4417 - Super Mario(主席树)

    题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...

  2. HDU-4417 Super Mario,划分树+二分!

    Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...

  3. HDU 4417 Super Mario 主席树

    分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...

  4. HDU 4417 Super Mario 主席树查询区间小于某个值的个数

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  5. [HDU4417]Super Mario(主席树+离散化)

    传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...

  6. hdu4417 Super Mario (树状数组/分块/主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...

  7. hdu4417 Super Mario 树阵离线/划分树

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    ...

  8. hdu-4417 Super Mario(树状数组 + 划分树)

    题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Other ...

  9. hdu4417 Super Mario

    Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...

  10. hdu_4417_Super Mario(主席树)

    题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...

随机推荐

  1. 在js中实现邮箱格式的验证

    在js中实现邮箱格式的验证 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><htm ...

  2. solr 安装

    1:solr简介 solr是一个开源的搜索引擎,是对lucene做了封装,对外提供类似于webservice接口, 可以使用http请求的方式对solr进行操作. lucene.solr.elasti ...

  3. MySQL自增ID 起始值 修改方法

    在mysql中很多朋友都认为字段为AUTO_INCREMENT类型自增ID值是无法修改,其实这样理解是错误的,下面介绍mysql自增ID的起始值修改与设置方法. 通常的设置自增字段的方法: 创建表格时 ...

  4. maven+ssm+cxf3配置例子

    以下只是简单记录 ssm结合cxf3的配置 提供方配置::: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...

  5. jquery_DOM笔记2

    属性操作; addClass() 添加指定的类名.用于切换效果 after() 在元素后面插入 before() 在元素之前插入 append()在元素后面添加 appendTo() 一直在元素尾部添 ...

  6. ThinkInside

    #sideBar,#blog_post_info_block { display: none;}#under_post_news { display: none;} /*评论框大小*/ #tbComm ...

  7. 使用Dir,遍历文件夹下所有子文件夹及文件

    '------------------------------------------- '获取某文件夹下所有文件和子目录下的文件 '--------------------------------- ...

  8. git: fatal: Not a git repository (or any of the parent directories): .git

    在看书 FlaskWeb开发:基于Python的Web应用开发实战 时,下载完源码后 git clone https://github.com/miguelgrinberg/flasky.git 试着 ...

  9. .NetChajian

    Code generation(代码自动生成) NVelocity CodeSmith X-Code .NET XGoF - NMatrix / DEVerest Compilation(编译工具) ...

  10. Selenium FF WebDriver 加载firebug 和设置代理

    首先这次使用的webDriver for Firefox的 由于项目的原因,需要在测试的时候加载Firebug和使用vpn,加载代理 Firefox 加载代理,可以从FF菜单上看,代理分为好几种 我这 ...