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的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...
随机推荐
- java从基础知识(十)java多线程(下)
首先介绍可见性.原子性.有序性.重排序这几个概念 原子性:即一个操作或多个操作要么全部执行并且执行的过程不会被任何因素打断,要么都不执行. 可见性:一个线程对共享变量值的修改,能够及时地被其它线程看到 ...
- Redis常用操作及客户端工具
修改redis密码 打开redis.windows.conf,找到requirepass 设置密码重启服务即可 将redis安装为windows服务,批处理如下: redis-server.exe ...
- 安装和卸载windows服务 bat
1. 安装 windows服务 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil [服务路径](例:C:\\test\myt ...
- web页面跳转的几种方式
可用客户端触发或服务端触发的方式来实现页面跳转. 客户端触发 方式一:使用Javascript 利用window.location对象的href属性.assign()方法或replace()方法来实现 ...
- SDK,API,DLL名词解释
SDK (software devalopment kit) 软件开发工具包 : 一般都是一些软件工程师Wie特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的开发工具的集合. API (A ...
- 【openStack】Libcloud 如何支持 keystone V3?
Examples This section includes some examples which show how to use the newly available functionality ...
- Ternary Expression Parser
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
- Qt 二维码
1.生成二维码 利用第三方库qrencode ,将qrencode源码添加到自己的程序中,直接调用使用. 参考http://blog.csdn.net/zhangxufei/article/detai ...
- sql 数据库结构导出到文件
SELECT 表名 = Case When A.colorder= Then D.name Else '' End, 表说明 = Case When A.colorder= Then isnull(F ...
- context:component-scan标签的use-default-filters属性的作用以及原理分析
一.背景 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,那么本文就来分析下出现这种问题可能的原因以及解决方式. 二.分析及原理窥探 1.项目结构 ...