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. VC++ 读写注冊表,注冊文件图标关联

    #include <string> #include <iostream> #include <Windows.h> #include <shlobj.h&g ...

  2. 【BZOJ1513】[POI2006]Tet-Tetris 3D 二维线段树

    [BZOJ1513][POI2006]Tet-Tetris 3D Description Task: Tetris 3D "Tetris" 游戏的作者决定做一个新的游戏, 一个三维 ...

  3. Hive高级

    HiveServer2 概述: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Overview2 客户端: https:// ...

  4. TensorFlow_action

    安装TensorFlow  包依赖 C:\Users\sas> pip3 install --upgrade tensorflow Collecting tensorflow Downloadi ...

  5. IO多路复用的作用?并列举实现机制以及区别?

    I/O多路复用是用于提升效率,单个进程可以同时监听多个网络连接IO. 举例:通过一种机制,可以监视多个文件描述符,一旦描述符就绪(读就绪和写就绪),能通知程序进行相应的读写操作,I/O多路复用避免阻塞 ...

  6. Spring mvc 与 strust

    1. 机制:spring mvc的入口是servlet,而struts2是filter 2. 性能:spring会稍微比struts快.spring mvc是基于方法,单例(servlet也是单例): ...

  7. postgres=# psql -U postgres -h 127.0.0.1 -p 5432 -d dreamstart_dev -w

    postgres=# psql -U postgres -h 127.0.0.1 -p 5432 -d dreamstart_dev -wpostgres-# \dNo relations found ...

  8. Redis持久化——多实例部署(四)

    Redis单线程架构导致无法充分利用CPU特性,通常的做法是在一台机器上部署多个实例. 当多个实例开启AOF重写后,彼此之间会产生对CPU和IO的竞争. 对于单机部署多Redis部署,如果同一时刻运作 ...

  9. 基于CocoaPods的iOS项目模块化实践

    什么是CocoaPods? CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has ove ...

  10. 每天一个Linux命令(25)chgrp命令

          chgrp命令用来改变文件或目录所属的用户组. (1)用法:     用法:  chgrp  [选项参数] [组] [文件] 或 chgrp  [选项]   组文件...   POSIX ...