HDU 4417 Super Mario
2016-08-01 23:30
351 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417
Total Submission(s): 4715 Accepted Submission(s): 2161
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.
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.)
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
4
0
0
3
1
2
0
1
5
1
思路:在我的博客POJ 2481 Cows中对这类题大概讲了一下解题思路。这里再重复一遍:对于这种二维属性的区间求个数问题,如果其属性满足简单的大小关系,我们可以先对一维属性进行排序,然后按照该顺序,在另一维上进行计数。对于这题,我们可以对高度进行从小到大的排序,然后对区间端点进行计数(单点更新),最后对区间进行求和(区间求和)即可。可以用线段树实现,也可以用树状数组实现。这里用的是树状数组,原因还是老样子,代码简洁,量少。详见代码。
附上AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 100005;
struct node{
int x, id;
bool operator < (const node & p) const {
return x < p.x;
}
} num[maxn];
struct range{
int l, r, h, id;
bool operator < (const range & p) const {
return h < p.h;
}
} query[maxn];
int cnt[maxn], ans[maxn];
int n, m;
int lowbit(int x){
return x&(-x);
}
void add(int p){
while (p <= n){
++cnt[p];
p += lowbit(p);
}
}
int sum(int p){
int res = 0;
while (p > 0){
res += cnt[p];
p -= lowbit(p);
}
return res;
}
int main(){
int T;
scanf("%d", &T);
for (int i=1; i<=T; ++i){
scanf("%d%d", &n, &m);
for (int j=0; j<n; ++j){
scanf("%d", &num[j].x);
num[j].id = j+1;
}
for (int j=0; j<m; ++j){
scanf("%d%d%d", &query[j].l, &query[j].r, &query[j].h);
++query[j].l, ++query[j].r, query[j].id=j;
}
sort(num, num+n);
sort(query, query+m);
// for (int j=0; j<n; ++j)
// printf("%d\n", num[j].x);
// for (int j=0; j<m; ++j)
// printf("%d\n", query[j].h);
memset(cnt, 0, sizeof(int)*(n+1));
for (int j=0, k=0; j<m; ++j){
while (k<n && query[j].h>=num[k].x){
add(num[k].id);
++k;
}
ans[query[j].id] = sum(query[j].r)-sum(query[j].l-1);
}
printf("Case %d:\n", i);
for (int j=0; j<m; ++j)
printf("%d\n", ans[j]);
}
return 0;
}
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4715 Accepted Submission(s): 2161
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 tothe 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
110 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 OnlineRecommend
liuyiding思路:在我的博客POJ 2481 Cows中对这类题大概讲了一下解题思路。这里再重复一遍:对于这种二维属性的区间求个数问题,如果其属性满足简单的大小关系,我们可以先对一维属性进行排序,然后按照该顺序,在另一维上进行计数。对于这题,我们可以对高度进行从小到大的排序,然后对区间端点进行计数(单点更新),最后对区间进行求和(区间求和)即可。可以用线段树实现,也可以用树状数组实现。这里用的是树状数组,原因还是老样子,代码简洁,量少。详见代码。
附上AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 100005;
struct node{
int x, id;
bool operator < (const node & p) const {
return x < p.x;
}
} num[maxn];
struct range{
int l, r, h, id;
bool operator < (const range & p) const {
return h < p.h;
}
} query[maxn];
int cnt[maxn], ans[maxn];
int n, m;
int lowbit(int x){
return x&(-x);
}
void add(int p){
while (p <= n){
++cnt[p];
p += lowbit(p);
}
}
int sum(int p){
int res = 0;
while (p > 0){
res += cnt[p];
p -= lowbit(p);
}
return res;
}
int main(){
int T;
scanf("%d", &T);
for (int i=1; i<=T; ++i){
scanf("%d%d", &n, &m);
for (int j=0; j<n; ++j){
scanf("%d", &num[j].x);
num[j].id = j+1;
}
for (int j=0; j<m; ++j){
scanf("%d%d%d", &query[j].l, &query[j].r, &query[j].h);
++query[j].l, ++query[j].r, query[j].id=j;
}
sort(num, num+n);
sort(query, query+m);
// for (int j=0; j<n; ++j)
// printf("%d\n", num[j].x);
// for (int j=0; j<m; ++j)
// printf("%d\n", query[j].h);
memset(cnt, 0, sizeof(int)*(n+1));
for (int j=0, k=0; j<m; ++j){
while (k<n && query[j].h>=num[k].x){
add(num[k].id);
++k;
}
ans[query[j].id] = sum(query[j].r)-sum(query[j].l-1);
}
printf("Case %d:\n", i);
for (int j=0; j<m; ++j)
printf("%d\n", ans[j]);
}
return 0;
}
相关文章推荐
- hdu 4417 Super Mario
- Hdu(4417)Super Mario
- 【划分树+二分】HDU 4417 Super Mario
- hdu-4417-Super Mario
- HDU 4417 Super Mario(划分树)#by zh
- Hdu 4417 Super Mario(主席树+二分)
- HDU - 4417 Super Mario (静态区间查询<=x的个数)
- HDU 4417 Super Mario
- HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
- 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_TLE
- HDU 4417 - Super Mario (求区间小于h的数 划分树 二分 线段树 树状数组)
- HDU 4417 Super Mario
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)
- HDU 4417 Super Mario(离线线段树)