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名了..有 ...
随机推荐
- C++类型转换(类型转换函数+类型构造函数)
C++类型转换(类型转换函数+类型构造函数) 类型转换函数 类型转换运算符是类的一种特殊成员函数,它负责将一个类类型的值转换成其他类型. graph LR 类类型--> 类型转换函数 --> ...
- 从入门到自闭之Python软件命名规范
软件命名规范:分文件存储 当代码存放在一个py文件中时会存在一下缺点: 不便于管理 可读性差 加载速度慢 是Django的雏形 程序员预定俗称的一些东西 启动文件:也叫启动接口,通常文件夹名字使用bi ...
- 纯CSS3绘制神奇宝贝伊布动画特效
在线演示 本地下载
- Codeforces 1194C. From S To T
传送门 首先贪心, $S$ 能和 $T$ 匹配就要尽量匹配,剩下的才让 $P$ 来补 在 $S$ 全部匹配上的情况下,看看 $P$ 是否有足够的字符即可 #include<iostream> ...
- Binding的简单使用
Binding可以看作是数据的桥梁,两端分别为Source和Target,一般情况,Source是逻辑层的对象,Target是UI层的控件对象,可以将数据从逻辑层送往UI层展现 简单的例子: clas ...
- web-CSS居中大全
居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...
- python多线程、多进程、协程笔记
import threading import time import multiprocessing import asyncio movie_list = ['斗破.avi', '复仇者联盟.mp ...
- Jquery.serializeArray()可看表单提交内容
- 问题:tomcat启动后,可以访问主页面,但是无法访问dubbo-admin
原因分析: 直接查看logs中的日志文件,发现一行 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.undeploy Undep ...
- linux中查看文件夹结构的小工具
tree命令是Linux/UNIX系统中常用的命令,可以非常方便地查看文件夹的结构,并且以树形目录的形式展示 在Ubuntu中安装 sudo apt-get install tree 在CentOS中 ...