Super Mario

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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


解题心得:

  1. 题意就是有n个水管,每个水管都有高度,马里奥要从水管a走到水管b,此时马里奥能跨过的高度为h,问你在区间[a,b]之间水管高度不大于h的有多少个。
  2. 这个题一看就知道是线段树,但是线段树每一个节点记录的状态肯定不能记录多个高度的数量。很容易想到解决办法,没办法记录多个高度,那就从低的高度开始累加,所以在面对询问的时候将询问按照高度排序,在询问之前更新数量,插入不大于当前询问高度的水管,然后每次用线段树记录当前答案存下来,然后再按照之前询问的顺序输出就行了。
  3. 为了不TLE,要记录水管的位置,然后排序,按照顺序插入。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<string>
using namespace std;
const int maxn = 1e5+100;
struct tree {
int cnt,l,r;
}node[maxn<<2]; struct Brick{
int va,pos;
friend bool operator < (const Brick a, const Brick b){
return a.va < b.va;
}
}brick[maxn]; int n,m,ans[maxn]; struct Query{
int l,r,h,pos;
} qu[maxn]; bool cmp(Query a,Query b){
return a.h < b.h;
} void init(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%d",&brick[i].va);
brick[i].pos = i;
}
for(int i=0;i<m;i++){
scanf("%d%d%d",&qu[i].l,&qu[i].r,&qu[i].h);
qu[i].pos = i;
}
sort(brick,brick+n);//水管排序
sort(qu,qu+m,cmp);//询问排序
} void build(int root,int l,int r){
node[root].cnt = 0;
node[root].l = l;
node[root].r = r;
if(l == r)
return ;
int mid = (l+r) >> 1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
} void insert_tree(int pos,int root,int l,int r){
node[root].cnt++;
if(l == r)
return ;
int mid = (l+r)>>1;
if(mid < pos)
insert_tree(pos,root<<1|1,mid+1,r);
else
insert_tree(pos,root<<1,l,mid);
} int query(int l,int r,int root,int L,int R){
if(l == L && r == R)
return node[root].cnt;
int mid = (L + R)>>1;
if(mid >= r)
return query(l,r,root<<1,L,mid);
else if(mid < l)
return query(l,r,root<<1|1,mid+1,R);
else
return query(l,mid,root<<1,L,mid) + query(mid+1,r,root<<1|1,mid+1,R);
} void get_ans(){
int k = 0;
for(int i=0;i<m;i++){
while(k<n){
if(brick[k].va <= qu[i].h){
insert_tree(brick[k].pos,1,0,n-1);//先插入
k++;
}
else
break;
}
int Ans = query(qu[i].l,qu[i].r,1,0,n-1);//再询问
ans[qu[i].pos] = Ans;//把答案给记录下来
}
} void Print(){
for(int i=0;i<m;i++)
printf("%d\n",ans[i]);
} int main(){
int t,T = 1;
scanf("%d",&t);
while(t--){
printf("Case %d:\n",T++);
init();
build(1,0,n-1);
get_ans();
Print();
}
return 0;
}

HDU:4417-Super Mario(离线线段树)的更多相关文章

  1. hdu 4417 Super Mario 离线线段树

    思路:将点按值从小到大排序,询问按h从小到大排序. 在建立线段树,按h的大小更新树并得到该次查询的结果! 代码如下: #include<iostream> #include<stdi ...

  2. HDU 4417 Super Mario(线段树)

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

  3. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

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

  4. HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)

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

  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 ( 离线树状数组 )

    把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...

  7. hdu 4417 Super Mario (主席树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...

  8. HDU 4417 Super Mario(主席树 区间不超过k的个数)题解

    题意:问区间内不超过k的个数 思路:显然主席树,把所有的值离散化一下,然后主席树求一下小于等于k有几个就行.注意,他给你的k不一定包含在数组里,所以问题中的询问一起离散化. 代码: #include& ...

  9. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  10. HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)

    突然想到的节约时间的方法,感觉6翻了  给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...

随机推荐

  1. Spring Cloud Ribbon负载均衡

    目录 一.简介 二.客户端负载均衡 三.RestTemplate详解 GET请求 POST请求 PUT请求 DELETE请求 一.简介 ​ Spring Cloud Ribbon是一个基于HTTP 和 ...

  2. synchronized重入后抛出异常,锁释放了吗

    synchronized: 用于同步方法或者代码块,使得多个线程在试图并发执行同一个代码块的时候,串行地执行.以达到线程安全的目的. 允许重入: 在多线程的时候是这样的,但是对于单线程,是允许重入的, ...

  3. 动态页面技术----EL技术、JSTL技术,javaEE的开发模式

    1 EL技术 1.1 EL 表达式 EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写, EL出现的目的是要替代jsp页面中脚本的编写,就是简化java代码. ...

  4. kafka基础一

    基本概念: 消息系统的组成由生产者,消费者以及存储系统.消费者从存储系统中读取生产者生产的消息.Kafka作为分布式的消息系统支持多个生产者多个消费者,写消息时允许多个生产者写到同一个Partitio ...

  5. JAVA 面试重点知识个人总结

    一.集合: 1 .Collection(是java.util下的接口) 和 Collections(是java.util下的类). 2 .List, Set,是否继承自Collection接口,Map ...

  6. Servlet高级部分Listener

    监听器的使用场景: ①:统计在线人数   ②:实现单一登录[一个账号只能在一台机器上登录] Servlet中的8大监听器: 1.         ServletContextListener [接口方 ...

  7. 牛客NOIP提高组(三)题解

    心路历程 预计得分:$30 + 0 + 0 = 30$ 实际得分:$0+0+0= 0$ T1算概率的时候没模爆long long了... A 我敢打赌这不是noip难度... 考虑算一个位置的概率,若 ...

  8. 构建第一个spring boot2.0应用之项目启动运行的几种方式(二)

    方法一. 配置Run/Debug Configuration  选择Main Class为项目 Application启动类(入口main方法) (2).进行项目目录,即包含pom.xml的目录下,启 ...

  9. uLua学习之读取外部Lua脚本(四)

    前言 上节说到了Lua脚本与unity3d中C#脚本的数据交互,但是我感觉上节中的数理方式不太好,因为我们是把Lua脚本以字符串形式粘贴到C#脚本中的,如果读取配置数据都这样做的话,那就太可怕了.想想 ...

  10. Android商城开发系列(十一)—— 首页秒杀布局实现

    首页秒杀布局如下图: 布局使用的是LinearLayout和RecyclerView去实现,新建seckkill_item.xml,代码如下所示: <?xml version="1.0 ...