HDU 4417 Super Mario (树状数组 + 离线)
来源:互联网 发布:8090端口是干嘛的 编辑:程序博客网 时间:2024/05/23 12:31
题意:
给你一堆数, m 次询问, 每次询问问你区间里 小于等于x 的数有多少个?
思路:
很容易往 数据结构 + 离线 上想。
一开始写了一发发现想偏了。然后就没然后了。。
==========================
先把询问存下来, 按照h 排序, 在给那一堆数存下来, 排序。
这样枚举询问, 在写个 a数组的指针 , 只要比当前h小, 就往后挪就行, 挪的时候, 给那个位置 + 1.
这样就是一个简单的区间和了。
树状数组就成了。(基于上次那个分治的教训, 能用树状数组就别用线段树了)
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000 + 10;int T, n, m;long long c[maxn];inline lowbit(int x){ return x&-x;}int sum(int x){ int ans = 0; while(x > 0){ ans += c[x]; x -= lowbit(x); } return ans;}void add(int x, int v){ while(x <= n){ c[x] += v; x += lowbit(x); }}struct Node{ int val, id; void read(int i){ id = i; scanf("%d",&val); }}a[maxn];bool cmp_Node(Node a, Node b ){ return a.val < b.val;}struct node{ int l,r,h; int id; void read(int i){ id = i; scanf("%d %d %d",&l, &r, &h); }}p[maxn];int ans[maxn];bool cmp(node a, node b){ return a.h < b.h;}int main(){ scanf("%d",&T);int ks = 0; while(T--){ memset(c,0,sizeof c); scanf("%d %d",&n, &m); for (int i = 0; i < n; ++i){ a[i].read(i+1); } for (int i = 0; i < m; ++i){ p[i].read(i); } sort(p, p + m, cmp); sort(a, a + n, cmp_Node); int j = 0; for (int i = 0; i < m; ++i){ while(j < n && a[j].val <= p[i].h){ add(a[j].id, 1); ++j; } ans[ p[i].id ] = sum(p[i].r + 1) - sum(p[i].l); } printf("Case %d:\n", ++ks); for (int i = 0; i < m; ++i){ printf("%d\n", ans[i]); } } return 0;}
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7056 Accepted Submission(s): 3030
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.)
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
110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3
Sample Output
Case 1:4003120151
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
Recommend
liuyiding | We have carefully selected several similar problems for you: 6113 6112 6111 6110 6109
阅读全文
0 0
- HDU 4417 Super Mario (树状数组 + 离线)
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之树状数组篇)
- hdu 4417 Super Mario 树状数组+离线操作
- hdu 4417 Super Mario(离线树状数组|划分树)
- HDU 4417 Super Mario (树状数组、离线处理)
- HDU 4417 Super Mario [树状数组+离线操作]【数据结构】
- HDU-4417:Super Mario(树状数组+离线操作)
- hdu 4417 Super Mario (树状数组)
- HDU 4417 Super Mario (树状数组)
- HDU 4417 Super Mario(12年杭州网络赛-H题-离线 + 树状数组)
- HDU 4417 Super Mario (树状数组+离线处理)(划分树+二分答案)
- HDU 4417 —— Super Mario(树状数组,离散化,离线处理)
- HDU 4417 Super Mario(离线线段树or树状数组)
- HDU 4417 Super Mario(树状数组离线处理 or 主席树)
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)
- HDU 4417 Super Mario 树状数组
- hdu 4417 Super Mario(划分树或树状数组)
- HDU 4417 Super Mario--离线树状数组、划分树、线段树
- Git使用笔记
- 数组的合并与拆分
- 求最大公约数问题
- c#程序在部分电脑运行异常解决方法(System.Runtime.InteropServices.COMException (0x80070422): 无法启动服务)
- hibernate简单应用
- HDU 4417 Super Mario (树状数组 + 离线)
- 问题 D: 地宫取宝
- 12个小球中,有一个小球质量M与其他11个小球不同,只能测试3次,怎么可以找出它?
- ZOJ 3469 Food Delivery (区间DP)
- 【HDU-4135】Co-prime
- CGI详解
- 冒泡排序
- Uva 136 Ugly Numbers(丑数)
- C++ sizeof的使用总结