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

题目大意是求序列中lt到rt范围内比h小的数的个数。

虽然序列是固定的,没有修改操作,但是要想实现在线操作还是很难的。

于是考虑了离线。

首先将所有的查询保存下来。然后按照h从小到大进行排序,当然会纪录下这个查询在原来是第几个。

然后从最小的h开始查,这样的话,就能保证小h查询的满足的元素,大h也是满足的。

于是只需要把序列的元素先去掉,然后从小到大再放回去,查h的时候,保证比h小的元素都回归到序列中。

于是操作可以描述为以下几点:

1、 从小到大查询h。

2、 用一个数组,0表示元素还没有回归序列,1表示已经回归序列。

3、 查询h的时候保证比h小的元素都回归序列(首先肯定要对序列中的元素进行排序)

4、 查lt到rt比h小的元素的个数,也就是查询区间内1的个数,也是是区间和。用树状数组维护这段序列。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long using namespace std; const int maxN = 1e5+;
int n, m, ans[maxN]; struct Node
{
int val;
int id;
}s[maxN]; bool cmpNode(Node x, Node y)
{
return x.val < y.val;
} struct Query
{
int lt, rt;
int h;
int id;
}q[maxN]; bool cmpQuery(Query x, Query y)
{
return x.h < y.h;
} //树状数组
int d[maxN]; int lowbit(int x)
{
return x&(-x);
} void add(int id, int pls)
{
while(id <= maxN)//id最大是maxN
{
d[id] += pls;
id += lowbit(id);
}
} int sum(int to)
{
int s = ;
while(to > )
{
s = s+d[to];
to -= lowbit(to);
}
return s;
} int query(int from, int to)
{
return sum(to)-sum(from-);
} void input()
{
memset(d, , sizeof(d));
memset(s, , sizeof(s));
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i)
{
scanf("%d", &s[i].val);
s[i].id = i;
}
sort(s+, s++n, cmpNode);
for (int i = ; i <= m; ++i)
{
scanf("%d%d%d", &q[i].lt, &q[i].rt, &q[i].h);
q[i].id = i;
}
sort(q+, q++m, cmpQuery);
} void work()
{
int top = ;
for (int i = ; i <= m; ++i)
{
while (top <= n && s[top].val <= q[i].h)
{
add(s[top].id, );
top++;
}
ans[q[i].id] = query(q[i].lt+, q[i].rt+);
}
} void output()
{
for (int i = ; i <= m; ++i)
printf("%d\n", ans[i]);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
input();
work();
printf("Case %d:\n", times);
output();
}
return ;
}

ACM学习历程—HDU4417 Super Mario(树状数组 && 离线)的更多相关文章

  1. hdu-4417 Super Mario(树状数组 + 划分树)

    题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Other ...

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

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

  3. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  4. Super Mario 树状数组离线 || 线段树

    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) Prob ...

  6. [HDU 4417] Super Mario (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个 ...

  7. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  8. SPOJ DQUERY树状数组离线or主席树

    D-query Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status ...

  9. D-query SPOJ 树状数组+离线

    D-query SPOJ 树状数组+离线/莫队算法 题意 有一串正数,求一定区间中有多少个不同的数 解题思路--树状数组 说明一下,树状数组开始全部是零. 首先,我们存下所有需要查询的区间,然后根据右 ...

随机推荐

  1. EasyNVR无插件直播服务器软件使用详情功能 - 录像功能说明

    背景介绍 EasyNVR不仅仅拥有无插件的直播功能,更拥有对于直播录像的存储和日期检索功能: 本篇博文主要用于介绍EasyNVR的录像功能. 之前有博文介绍相关的录像功能,本篇主要为了介绍录像的新功能 ...

  2. zoj 3721 Final Exam Arrangement【贪心】

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721 来源:http://acm.hust.edu.cn/vjudg ...

  3. iptables的例子1

      练习1:实现主机防火墙   设置主机防火墙策略为DROP: iptables -t filter -P INPUT DROP iptables -t filter -P OUTPUT DROP i ...

  4. java.util包下面的类---------01---示意图

    一直在使用util包下面的这些类,甚至有些没用过的,想要都去认识认识他们!也许在未来的一天可以用到! 图太大不好截图!部分没有截全!

  5. CSS 布局实例系列(一)总结CSS居中的多种方法

    使用 CSS 让页面元素居中可能是我们页面开发中最常见的拦路虎啦,接下来总结一下常见的几种居中方法吧. 1. 首先来聊聊水平居中: text-align 与 inline-block 的配合 就像这样 ...

  6. linux c编程:文件夹操作

    创建目录: 用mkdir函数创建目录: mkdir(const char *pathname, mode_t mode) 参数mode有下列的组合: S_ISUID 04000 文件的执行时设置用户I ...

  7. lazyload.js参数说明

    lazyload.js是jQuery的一个插件,可以用来实现图片异步加载. lazyload插件如何添加参数: $("img").lazyload({ //参数添加到此位置,建议一 ...

  8. sqlalchemy——多表操作

    一对多:一对一 # one -- many class Students(Base): __tablename__ = "students" sid = Column(Intege ...

  9. Linux 上通过rpm安装mysql

    安装mysql之前要remove掉系统自带的mysql: rpm -qa | grep "MySQL*"    和rpm -qa | grep mysql  要确保卸载干净 rpm ...

  10. Python 运算符(算术运算符(+,-,*,**,/,//),逻辑运算符(not , or ,and),比较运算符(>,<,>=,=<),复合运算符(+=,-=,*=,/=,**=,//=))

    # 一.算术运算符(+,-,*,**, /, //, %) # 加法运算符+ print(1 + 2) # 字符串相连 ") # 重载 print([1,2] + [3,4]) # 幂运算* ...