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. 驱动程序分层分离概念_总线驱动设备模型_P

    分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...

  2. 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

    [BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other.     But r ...

  3. H5动静分离

    1. 动静分离的实现思路(类似于iOS.安卓的思路,后台提供数据接口,前端用ajax异步请求json数据,再把json数据渲染到页面) 动静分离是将网站静态资源(HTML,JavaScript,CSS ...

  4. 栈 堆 stack heap

    点餐 做菜 Stack and Heap 堆和栈的区别 - Grandyang - 博客园 https://www.cnblogs.com/grandyang/p/4933011.html 在和计算机 ...

  5. Data Decisions: DSP vs. DMP

    http://www.cmo.com/features/articles/2016/3/9/data-decisions-dsp-vs-dmp.html As marketers assess the ...

  6. PAT 1058. 选择题(20)

    批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多. 输入格式: 输入在第一行给出两个正整数N(<=1000)和M(<=100),分别是学生人数和多 ...

  7. android禁止横屏

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  8. 11.23 Eclipse

    一.Eclipse Workspace File-->Switch Workspace 编译环境:Window -- Preferences – Java - Compiler 运行环境:Win ...

  9. python基础2 ---python数据类型一

    python的数据类型 一.什么是数据类型以及数据类型的分类 1.数据类型的定义:python使用对象模型来存储数据,每一个数据类型都有一个内置的类,每新建一个数据,实际就是在初始化生成一个对象,即所 ...

  10. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...