Super Mario 
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 题意:给你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(主席树)的更多相关文章

  1. HDU 4417 Super Mario 主席树

    分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...

  2. HDU4417 - Super Mario(主席树)

    题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...

  3. HDU 4417 Super Mario 主席树查询区间小于某个值的个数

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  4. hdu_4417_Super Mario(主席树)

    题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...

  5. HDU 4417 Super Mario(划分树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. HDU-4417 Super Mario,划分树+二分!

    Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...

  7. HDU 4417 Super Mario(划分树问题求不大于k的数有多少)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU-4417-Super Mario(主席树解法)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  9. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  10. HDOJ 4417 - Super Mario 线段树or树状数组离线处理..

    题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...

随机推荐

  1. Vue的快速入门

    1 环境准备 1 下载安装Node 地址https://nodejs.org/en/download/ 完成后通过cmd打开控制台输入node -v 可以看到版本信息 2 通过该node自带的npm ...

  2. Laravel-admin 表单提交同时验证俩个以上的字段唯一值

    $name = isset(request()->all()['name']) ? request()->all()['name'] : ''; $id = isset(request() ...

  3. Momentum Contrast for Unsupervised Visual Representation Learning

    Momentum Contrast for Unsupervised Visual Representation Learning 一.Methods Previously Proposed 1. E ...

  4. 面试之什么是java虚拟机

    java虚拟机体系结构 方法区 堆 java虚拟机栈 本地方法栈 方法区 java虚拟机编译的class文件中二进制数据类型解析数据存在方法区中 是所有线程共享 和存在数据的线程安全问题 当二个线程使 ...

  5. Binding的Path(路径)

    Binding的源可以是控件(一个控件是另一个控件的Source.控件把自己的容器作为Source),把集合作为ItemsControls的Source,把xml作为Tree或者Menu的Source ...

  6. loj 2336「JOI 2017 Final」绳

    loj 首先,所有位置最多被染色一次,因为要染多次的话,还不如一开始就染成最终的颜色.并且你可以一开始就染好色 因为最终长度为2,那么如果染完后这个序列可以被折完,那么首先最多只有两种颜色,还有就是要 ...

  7. css设置全屏背景图,background-size 属性

    在写主题样式的时候经常会碰到用背景图铺满整个背景的需求,这里分享下使用方法 需要的效果 图片以背景的形式铺满整个屏幕,不留空白区域 保持图像的纵横比(图片不变形) 图片居中 不出现滚动条 多浏览器支持 ...

  8. eclipses配置tomcat

    1,项目右键属性,设置为1.8,与jdk相对应 2,自动发布,tomcat 3,使用自己的tomcat 4,

  9. Zabbix 3.2.6使用注意事项

    1.如果需要使用zabbix自带的SMTP发送邮件,需要在安装前升级系统的curl到7.20版本以上 2.zabbix对接PHP 7.1版本,因为PHP 7.1类型强化,会在安装完成zabbix,登录 ...

  10. radio赋值法

    一般都会使用attr来使选中: $("#DIV的ID input[name='radio的name'][value="'+动态传的radio的value值+'"]&quo ...