题目

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. EBS提交请求出现REP-3000错误

    在AIX上利用并发请求提交报表的時候,出现如下错误:REP-3000: Internal error starting Oracle Toolkit.这是因为Report Server需要X-Wind ...

  2. 安装 sublime2 (包括插件)

    1.下载地址:http://www.sublimetext.com/2,注意选择不同的平台: 2.安装后,打开sublime,在菜单栏  help -- enter license 打开一个窗口,复制 ...

  3. 移动端浏览器body的overflow:hidden并没有什么作用

    今天突然遇到一个问题,使用li模拟select,但是碰到一个很尴尬的问题,给body加了overflow:hidden,但是body并没有禁止滚动条,滚动条依旧顺滑. <!DOCTYPE htm ...

  4. Visual Studio 常用快捷键备忘

    在代码中插入书签 用途 操作   vs2013 快速在自定义的不同代码位置跳转 首先点击: 编辑=>书签=>启用书签 然后再在代码编辑窗口 ctrl+k, k (取消书签,再按一次 ctr ...

  5. Toast 工具

    public class Utils { private static Toast toast; public static void showtoast(Context context,String ...

  6. 使用visual studio 调试android 程序 ,真机调试

    1 使用visual studio 2015 新建 blank android APP , 2 安卓手机调整到开发者模式 3 通过USB链接到PC 4 自动检测 设备(这一步貌似没有立即检测到真机设备 ...

  7. 面向过程(POP)、面向对象(OOP)、面向接口(IOP)、面向切面(AOP)

    面向过程:典型的是C/C++的结构体,结构体里只有变量,没有处理变量的方法,需要专门编写处理变量的方法. 面向对象:ArrayList<Integer> list=new ArrayLis ...

  8. html JS 打开本地程序及文件

    在网页打开本地应用程序示例: 一.在本地注册表自定义协议:以自定义调用Viso为例 1.在HKEY_CLASSES_ROOT下添加项ZVISIO. 2.修改ZVISIO项下的"(默认)&qu ...

  9. Ionic 常用插件

    ionic扩展插件 1.ionic-timepicker 时间选择 https://github.com/rajeshwarpatlolla/ionic-timepicker   2.ionic-da ...

  10. linux的安装

    在CentOS 7中提供了两种桌面"GNOME DESKTOP" 和 "KDE Plasa Workspaces",我们以安装"GNOME DESKT ...