HDU 4417 Super Mario (树状数组 + 离线)
2017-08-12 20:48
453 查看
题意:
给你一堆数, 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;
}
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.)
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
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
Recommend
liuyiding | We have carefully selected several similar problems for you: 6113 6112 6111 6110 6109
Statistic | Submit | Discuss | Note
给你一堆数, 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.)
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
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
Recommend
liuyiding | We have carefully selected several similar problems for you: 6113 6112 6111 6110 6109
Statistic | Submit | Discuss | Note
相关文章推荐
- HDU 4417 Super Mario (树状数组、离线处理)
- HDU 4417 Super Mario(离线线段树or树状数组)
- HDU 4417 Super Mario--离线树状数组、划分树、线段树
- HDU 4417 —— Super Mario(树状数组,离散化,离线处理)
- HDU 4417 Super Mario(树状数组离线处理 or 主席树)
- HDU 4417 Super Mario ( 离线树状数组 )
- hdu-4417 Super Mario(树状数组 + 划分树)
- [HDU 4417] Super Mario (树状数组)
- HDU 4417 Super Mario (树状数组)
- hdu 4417 离线树状数组 胡乱搞搞
- HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛1008题(树状数组或者线段树)
- hdu 4417 Super Mario 树状数组||主席树
- hdu 4417 树状数组 离线处理
- hdu 4417 Super Mario (树状数组)
- hdu 4417 Super Mario(划分树或树状数组)
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之树状数组篇)
- hdu 4417 离线树状数组
- HDU 4417 Super Mario(线段树离线处理/主席树)
- HDU 4417 Super Mario [树状数组+离线操作]【数据结构】
- HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)