Super Mario(主席树)
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 题意:给你N个数,M次询问,每次输入一个区间和一个高度,求这个区间内不超过这个高度的数的个数。
算法:主席树 代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm> using namespace std; const int maxn = 1e5+; struct node {
int l, r, s;
}tree[maxn * ]; vector<int> v; int a[maxn];
int cnt;
int root[maxn]; void build(int &cur, int l, int r) {
cur = ++cnt;
tree[cur].s = ;
if(l == r) {
return;
}
int mid = (l + r) >> ;
build(tree[cur].l, l, mid);
build(tree[cur].r, mid + , r);
} int getId(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + ;
} void update(int l, int r, int x, int &y, int pos) {
y = ++cnt;
tree[y] = tree[x];
tree[y].s++;
if(l == r) {
return;
}
int mid = (l + r) >> ;
if(pos <= mid) {
update(l, mid, tree[x].l, tree[y].l, pos);
} else {
update(mid + , r, tree[x].r, tree[y].r, pos);
}
} int query(int l, int r, int x, int y, int h) {
if(l == r) {
return tree[y].s - tree[x].s;
}
int mid = (l + r) >> ;
int ans = ; //存储小于该高度的数的数量
if(h <= mid) {
ans += query(l, mid, tree[x].l, tree[y].l, h);
} else {
ans += tree[tree[y].l].s - tree[tree[x].l].s; //如果该数在右子树上,那么就需要把左子树上的数都加进去
ans += query(mid + , r, tree[x].r, tree[y].r, h);
}
return ans;
} int main() {
int T;
int Case = ;
scanf("%d", &T);
while(T--) {
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
//build(root[0], 1, n);
int size = v.size();
for(int i = ; i <= n; i++) {
update(, size, root[i - ], root[i], getId(a[i]));
}
printf("Case %d:\n", Case++);
while(m--) {
int left, right, k;
scanf("%d %d %d", &left, &right, &k);
left++; //此处记住需要加1,因为你的主席树是从1开始建立的
right++;
int h = upper_bound(v.begin(), v.end(), k) - v.begin(); //找到第一个比k大的数的位置
int ans = ;
if(h > ) {
ans = query(, size, root[left - ], root[right], h);
}
cout << ans << endl;
}
}
return ;
}
Super Mario(主席树)的更多相关文章
- HDU 4417 Super Mario 主席树
分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...
- HDU4417 - Super Mario(主席树)
题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- hdu_4417_Super Mario(主席树)
题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...
- HDU 4417 Super Mario(划分树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU-4417 Super Mario,划分树+二分!
Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...
- HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU-4417-Super Mario(主席树解法)
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- HDOJ 4417 - Super Mario 线段树or树状数组离线处理..
题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...
随机推荐
- 第k小团(Bitset+bfs)牛客第二场 -- Kth Minimum Clique
题意: 给你n个点的权值和连边的信息,问你第k小团的值是多少. 思路: 用bitset存信息,暴力跑一下就行了,因为满足树形结构,所以bfs+优先队列就ok了,其中记录下最后进入的点(以免重复跑). ...
- Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量
Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量 2019年02月18日 23:52:37 xiongxianze 阅读数 398更多 分类专栏: ...
- 如何同步多个 git 远程仓库
请看 -> 如何同步多个 git 远程仓库
- 分布式的几件小事(六)dubbo如何做服务治理、服务降级以及重试
1.服务治理 服务治理主要作用是改变运行时服务的行为和选址逻辑,达到限流,权重配置等目的. ①调用链路自动生成 一个大型的分布式系统,会由大量的服务组成,那么这些服务之间的依赖关系和调用链路会很复杂, ...
- call,apply,bind的理解
call,apply,bind均是用于改变this指向. 三者相似之处: 1:都是用于改变函数的this指向. 2:第一个参数都是this要指向的对象. 3:都可以通过后面的参数进行对方法的传参. l ...
- ES6基本常见语法
特色:写法更加优雅,更加像面像对象的编程,其思想和 ES5 是一致的. 箭头函数.this ES6中可以使用 => 作为函数表达形式,极简风格,参数+ => +函数体. var foo = ...
- 关于mail mailx 以及sendmail 的理解
最近在弄邮件告警相关的东西,接触到了mail这一块,但是发送邮件的时间看到网上的用法 yum install mailx sednmail -y 这一块很迷糊 所以决定自己研究下 首先套用官话解释: ...
- orcle_day02
第三章:单值函数 函数分为: 1.单值函数 1.字符函数 2.日期函数 3.转换函数 4.数字函数 2.分组函数(后面的章节再做学习) 哑表dual dual是一个虚拟表,用来构成select的语法规 ...
- python、mysql三-2:数据类型
一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/mysql/mysql-data ...
- 使用QEMU模拟树莓派
QEMU上的树莓派 我们开始设置一个Lab VM.我们将使用Ubuntu并在其中模拟我们所需的ARM版本. 首先,获取最新的Ubuntu版本并在VM中运行它: https://www.ubuntu.c ...