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的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...
随机推荐
- D3.js学习(一)
从今天开始我将和大家一起学习D3.js(Data-Driven Documents),由于国内关于D3的学习资料少之又少,所以我觉得很有必要把自己学习过程记录下来,供同学们参考,如果文章有有哪些表达有 ...
- bzoj1492 斜率优化|cdq分治
#include <stdio.h> #include <bitset> #include <string.h> #include <stack> #i ...
- scanf_s
很多带“_s”后缀的函数是为了让原版函数更安全,传入一个和参数有关的大小值,避免引用到不存在的元素,有时hacker可以利用原版的不安全性黑掉系统 例如: ANSI C中没有scanf_s(),只有s ...
- MFC双缓存技术代码
屏蔽背景刷新,在View中添加对WM_ERASEBKGND的响应,直接返回TRUE: BOOL CTEMV1View::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息 ...
- RBAC权限模型
RBAC 现在大多数的管理系统都是基于RBAC开发的组织机构权限框架.所有的操作都是基于角色(Role)来完成的.我们先从需求的角度出发,来了解关于系统权限管理. 用户A和用户B都属于研发部,我们可以 ...
- Android6.0权限组申请
void checkPermission() { final List<String> permissionsList = new ArrayList<>(); if (Bui ...
- canvas简介
一.canvas简介 1.1 什么是canvas?(了解) 是HTML5提供的一种新标签 <canvas></canvas> 英 ['kænvəs] 美 ['kænvəs] 帆 ...
- [转] 有java基础的人如何转行做大数据?
数据有两个方向,一个是偏计算机的,另一个是偏经济的.你学过Java,所以你可以偏将计算机基础1. 读书<Introduction to Data Mining>,这本书很浅显易懂,没有复杂 ...
- C#对象先序列化然后反序列化时间丢失八小时的问题
把对象JSON序列化,然后反序列化后发现时间少了八小时.因为在东八区,所以序列的时候按照1970-01-01:08:00:00为基数取得差值,而反序列化的时候以1970-01-01:00:00:00作 ...
- MySQL 5.6 OOM 问题解决分享【转】
本文来自:杨德华的原创分享 | MySQL 5.6 OOM 问题解决分享 延伸阅读:Linux的内存回收和交换 当遇到应用程序OOM的时候,大多数时候只能用头疼来形容,应用程序还可以通过引流来临时重启 ...