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名了..有 ...
随机推荐
- SVN服务器搭建与配置管理
1.下载和搭建SVN服务器 现在Subversion已经迁移到Apache网站上了,下载地址:http://subversion.apache.org/packages.html,这是二进制文件包的下 ...
- RPC 框架 介绍 (转)
出处: 谁能用通俗的语言解释一下什么是 RPC 框架? 知乎: 问题:了解到最近 Java 的 Netty 很火,只知道它是这样类型的一种框架.想了解一下它主要用于解决了什么问题?适用于什么样的场景? ...
- Codeforces 1203F2. Complete the Projects (hard version)
传送门 首先对于 $b>0$ 的工作显然有个贪心,把 $b>0$ 的按 $a$ 从小到大排序 把能做的都做了,然后得到一个最大等级 剩下就是考虑 $b<0$ 的工作了,看到数据显然可 ...
- 比特(bit)和字节(Byte)
比特(bit)和字节(Byte) 基础的内容就不说了,这里是一个小的学习笔记 比特和字节的写法差异与应用场景 标准的写法中,正如标题中写的那样,是通过大小写来区分比特和字节的:比特的b应该是小写,而字 ...
- Appium Android 获取包名appPackage和appActivity的几种方法
情况1: 安装包未安装到手机 准备前提条件: 1 Android SDK管理工具目录 2 PC端有apk包 使用方法: 1 打开终端,当前路径移动到sdk管理工具目录tools或build-tools ...
- vue-resource 全局拦截器
项目中可能会添加超时登录的功能,因此根据tokenid 判断是否超时.如果token已过期,需要跳转至登录页面. 因此需要用到全局拦截器拦截返回的状态 //下边代码添加在main.js中 Vue.ht ...
- elasticsearch设置执行脚本并添加开机启动 (转)
elasticsearch设置执行脚本并添加开机启动 在/etc/init.d目录下新建文件elasticsearch #!/bin/sh #chkconfig: 2345 80 05 #descri ...
- symfony3 使用命令行工具生成Entity实体所踩的坑
1.把配置文件汇总连接邮箱的配置信息注释掉了,在创建Entity时php bin/console doctrine:generate:entity报错 2. 错误原因是实体文件映射到数据库中的字段时候 ...
- 使用SpringBoot做Javaweb时,数据交互遇到的问题
有段时间没做过javaweb了,有点生疏了,js也忘记得差不多,所以今天下午做前后端交互的时候,传到后台的参数总是为空,前端控制台了报一个String parameter “xxx” is not p ...
- Spring NamespaceHandlerResolver xml的标签加载的扩展 和 ApplicationContext
NamespaceHandlerResolver public NamespaceHandler resolver(String namespaceUri); DefaultNamespaceHand ...