Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7265    Accepted Submission(s): 3127

Problem 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
 

Source

 
题意:问[l,r]区间内小于等于h的数有多少,即询问区间内h为第几大。
思路:可持久化线段树。
 //2017-09-07
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mid ((l+r)>>1) using namespace std; const int N = ;
const int M = N * ;
struct node{
int lson, rson, sum;//sum维护l到r的数有几个
}tree[M];
//第i棵线段树为插入前i个数字所构成的权值线段树。
int root[N], arr[N], arr2[N], tot;
int n, m, q; void init(){//将原数列排序并去重
tot = ;
for(int i = ; i <= n; i++)
arr2[i] = arr[i];
sort(arr2+, arr2++n);
m = unique(arr2+, arr2++n)-arr2-;
} //离散化
int getID(int x){
return lower_bound(arr2+, arr2++m, x) - arr2;
} int build(int l, int r){
int id = ++tot;
tree[id].sum = ;
if(l == r)return id;
if(l <= mid)
tree[id].lson = build(l, mid);
if(r > mid)
tree[id].rson = build(mid+, r);
return id;
} int update(int id, int pos, int value){
int newroot = ++tot, tmp = newroot;
tree[newroot].sum = tree[id].sum + value;
int l = , r = m;
while(l < r){
if(pos <= mid){
tree[newroot].lson = ++tot;
tree[newroot].rson = tree[id].rson;
newroot = tree[newroot].lson;
id = tree[id].lson;
r = mid;
}else{
tree[newroot].rson = ++tot;
tree[newroot].lson = tree[id].lson;
newroot = tree[newroot].rson;
id = tree[id].rson;
l = mid+;
}
tree[newroot].sum = tree[id].sum + value;
}
return tmp;
} int query(int ltree, int rtree, int k){
int l = , r = m, ans = ;
while(l < r){
if(k <= mid){
ltree = tree[ltree].lson;
rtree = tree[rtree].lson;
r = mid;
}else{
ans += tree[tree[rtree].lson].sum - tree[tree[ltree].lson].sum;
ltree = tree[ltree].rson;
rtree = tree[rtree].rson;
l = mid+;
}
}
if(l == r){
if(k < l)return ;
else return ans + tree[rtree].sum - tree[ltree].sum;
}
} int main()
{
//freopen("dataN.txt", "r", stdin);
int T, kase = ;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++)
scanf("%d", &arr[i]);
init();
root[] = build(, m);
for(int i = ; i <= n; i++){
int pos = getID(arr[i]);
root[i] = update(root[i-], pos, );
}
printf("Case %d:\n", ++kase);
while(q--){
int l, r, h;
scanf("%d%d%d", &l, &r, &h);
l++; r++;
int pos = getID(h);//找到第一个小于概数的位置
if(arr2[pos] > h)pos--;
printf("%d\n", query(root[l-], root[r], pos));
}
} return ;
}

HDU4417(SummerTrainingDay08-N 主席树)的更多相关文章

  1. HDU4417 - Super Mario(主席树)

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

  2. [HDU4417]Super Mario(主席树+离散化)

    传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...

  3. hdu4417 Super Mario (树状数组/分块/主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...

  4. hdu4417 主席树求区间小于等于K

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417   Problem Description Mario is world-famous plum ...

  5. 【主席树】【bzoj2161】[hdu4348]

    #include<cstdio> #include<algorithm> #include<cstring> #define N 400000 using name ...

  6. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

  7. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

  8. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

  9. BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3522  Solved: 1041[Submi ...

  10. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

随机推荐

  1. django -- 美多订单分表

    订单分表: 随着公司业务增长,如果每天1000多万笔订单的话,3个月将有约10亿的订单量,之前数据库采用单表的形式已经不满足于业务需求,数据库改造迫在眉睫. 解决思路: 按月分表,将原订单表拆分为 o ...

  2. 01-01java概述 doc命令、jdk\jre下载安装、path、classpath配置、开发中常见小问题

    1:计算机概述(了解) (1)计算机 (2)计算机硬件 (3)计算机软件 系统软件:window,linux,mac 应用软件:qq,yy,飞秋 (4)软件开发(理解) 软件:是由数据和指令组成的.( ...

  3. odoo按钮图标 icon

    https://www.slideshare.net/TaiebKristou/odoo-icon-smart-buttons http://www.iconfont.cn/collections/d ...

  4. Spring Boot启动流程

    基础准备 1,BeanPostProcessor:这个接口的作用在于对于新构造的实例可以做一些自定义的修改.比如如何构造.属性值的修改.构造器的选择等等 2,BeanFactoryPostProces ...

  5. gdb调试正在运行的程序

    1.ps aux | grep mxx.exe 查找可执行程序的进程id 2.gdb attach pid attach可执行程序的进程pid 3.continue/c 或者continue or c ...

  6. CodeSmith读取数据库

    这两天在看CodeSmith文档,因为官方文档在读数据库这一篇使用的是VB写的,对于C#使用者来说看起来很不方便,所以我改成C#的,顺便写下我自己的使用过程. 首先,要使用CodeSmith连接数据库 ...

  7. bower 和 npm 的区别

    前端技术和工程实践真的是突飞猛进啊,想当年,我这个半业余前端吭哧吭哧做页面的时候,哪有这么多东西可以用啊,现在先进到我都完全看不懂了.本文主要讲一下同是包管理器的bower和npm的差别. 主要也是在 ...

  8. docker cgroup 技术之memory(首篇)

    测试环境centos7 ,内核版本4.20 内核使用cgroup对进程进行分组,并限制进程资源和对进程进行跟踪.内核通过名为cgroupfs类型的虚拟文件系统来提供cgroup功能接口.cgroup有 ...

  9. Vue.js基础拾遗

    本篇目录: 模版语法 插值 指令 v-bind指令 v-on指令 计算属性与侦听器 计算属性VS方法 计算属性VS侦听属性 Class与Style绑定 绑定HTML Class 绑定内联样式 条件渲染 ...

  10. C/C++求职宝典21个重点笔记(常考笔试面试点)

    这是我之前准备找工作时看<C/C++求职宝典>一书做的笔记,都是一些笔试面试中常考的重点难点问题,但比较基础,适合初学者看. 1. char c = '\72'; 中的\72代表一个字符, ...