ACM学习历程—HDU4417 Super Mario(树状数组 && 离线)
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(树状数组 && 离线)的更多相关文章
- hdu-4417 Super Mario(树状数组 + 划分树)
题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu4417 Super Mario (树状数组/分块/主席树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...
- ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...
- Super Mario 树状数组离线 || 线段树
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4417 Super Mario 树状数组||主席树
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- [HDU 4417] Super Mario (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个 ...
- 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]< ...
- SPOJ DQUERY树状数组离线or主席树
D-query Time Limit: 227MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submit Status ...
- D-query SPOJ 树状数组+离线
D-query SPOJ 树状数组+离线/莫队算法 题意 有一串正数,求一定区间中有多少个不同的数 解题思路--树状数组 说明一下,树状数组开始全部是零. 首先,我们存下所有需要查询的区间,然后根据右 ...
随机推荐
- Entity Framework(1)——Connections and Models
原文:https://msdn.microsoft.com/en-us/data/jj592674 应该选择CodeFirst.ModelFirst还是databaseFirst网上已经很多资料了,这 ...
- vs重复编译
VS用了这么久都没有这样的问题,昨天突然发现在自己电脑时间不对了,就调了下,以后这问题都来了.每次运行项目都要重新编译下,不管改不改底层代码.这让我很痛苦,浪费大量时间,找了好久才得到答案: .时间问 ...
- SDOI2012 Round1 day2 拯救小云公主(dis)解题报告
#include<cstdio> #include<cmath> #include<iostream> using namespace std; typedef l ...
- JavaScript点击事件-一个按钮触发另一个按钮
<input type="button" value="Click" id="C" onclick="Go();" ...
- 在函数中如何获取 线程对象、线程唯一ID
threading.current_thread() threading.current_thread().ident
- 我的Android进阶之旅------>如何将Activity变为半透明的对话框?
我的Android进阶之旅------>如何将Activity变为半透明的对话框?可以从两个方面来考虑:对话框和半透明. 在定义Activity时指定Theme.Dialog主题就可以将Acti ...
- [转载]Java web应用中的常见字符编码问题的解决方法
以下是 Java web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...
- abap Excel 导入
ABAP 将EXECL数据导入SAP内表的几个步骤. 本文转自:http://blog.csdn.net/szlaptop/article/details/8663451 http://www.c ...
- python 发送邮件的两种方式【终极篇】
一,利用python自带的库 smtplib简单高效 from email.mime.multipart import MIMEMultipart from email.mime.text impor ...
- Django中日志管理
在settings中设置日志的相关信息,然后再逻辑代码区就可以保存相应的信息了 #简单设置: LOGGING = { 'version': 1, 'disable_existing_loggers': ...