Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7933 Accepted Submission(s): 3365
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
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int a[maxn], b[maxn], tot, rt[maxn];
struct tree{
int sz, lson, rson;
}c[maxn << 5];
int build(int l, int r){
int id = ++tot;
c[id].sz = 0;
if(l == r) return id;
int mid = l + r >> 1;
c[id].lson = build(l, mid);
c[id].rson = build(mid + 1, r);
return id;
}
int insert(int rt, int pos, int l, int r){
int id = ++tot;
c[id].sz = c[rt].sz;
c[id].lson = c[rt].lson;
c[id].rson = c[rt].rson;
if(l == r){
c[id].sz++; return id;
}
int mid = l + r >> 1;
if(pos <= mid) c[id].lson = insert(c[id].lson, pos, l, mid);
else c[id].rson = insert(c[id].rson, pos, mid + 1, r);
c[id].sz = c[c[id].lson].sz + c[c[id].rson].sz;
return id;
}
int query(int pre, int rt, int l, int r, int L, int R){
if(L > R) return 0;
if(l >= L && r <= R){
return c[rt].sz - c[pre].sz;
}
int mid = l + r >> 1, ans = 0;
if(mid >= L) ans += query(c[pre].lson, c[rt].lson, l, mid, L, R);
if(mid < R) ans += query(c[pre].rson, c[rt].rson, mid + 1, r, L, R);
return ans;
}
int main(){
int T, casenum = 1;
scanf("%d", &T);
while(T--){
printf("Case %d:\n", casenum++);
int n, m, num, pos, l, r, h;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + 1, b + n + 1);
num = unique(b + 1, b + 1 + n) - b - 1;
tot = 0;
rt[0] = build(1, num);
for(int i = 1; i <= n; ++i){
pos = lower_bound(b + 1, b + 1 + num, a[i]) - b;
rt[i] = insert(rt[i - 1], pos, 1, num);
}
for(int i = 1; i <= m; ++i){
scanf("%d %d %d", &l, &r, &h);
pos = upper_bound(b + 1, b + num + 1, h) - b - 1;
printf("%d\n", query(rt[l], rt[r + 1], 1, num, 1, pos));
}
}
}
/*
题意:
1e5个数,1e5次询问,每次询问区间有多少个数小于等于H。
思路:
主席树 板题。注意一下H的边界情况和离散时的正确性。
*/
本文介绍了一道关于SuperMario的游戏算法题目,玩家需计算马里奥在特定高度跳跃时能够击中的砖块数量。文章提供了详细的输入输出示例及代码实现,采用主席树的数据结构解决区间查询问题。
684

被折叠的 条评论
为什么被折叠?



