传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4417
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
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
struct node{
int sum,ls,rs;
}tree[maxn*20];
int a[maxn],b[maxn],root[maxn];
int n,m,tot;
inline void init()
{
tot=0;
}
inline void build(int &cur,int L,int R)
{
cur=++tot;
tree[cur].sum=0;
if(L==R) return;
int mid=L+R>>1;
build(tree[cur].ls,L,mid);
build(tree[cur].rs,mid+1,R);
}
inline void update(int pre,int &cur,int L,int R,int x)
{
cur=++tot;
tree[cur]=tree[pre],tree[cur].sum++;
if(L==R) return;
int mid=L+R>>1;
if(x<=mid) update(tree[pre].ls,tree[cur].ls,L,mid,x);
else update(tree[pre].rs,tree[cur].rs,mid+1,R,x);
}
inline int query(int pre,int cur,int L,int R,int h)
{
if(L==R) return tree[cur].sum-tree[pre].sum;
int mid=L+R>>1;
int ret=0;
if(h<=mid) ret=query(tree[pre].ls,tree[cur].ls,L,mid,h);
else{
ret+=tree[tree[cur].ls].sum-tree[tree[pre].ls].sum;
ret+=query(tree[pre].rs,tree[cur].rs,mid+1,R,h);
}
return ret;
}
int main()
{
int t,Case=1;
scanf("%d",&t);
while(t--){
init();
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);
int ed=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+ed+1,a[i])-b;
build(root[0],1,ed);
for(int i=1;i<=n;i++) update(root[i-1],root[i],1,ed,a[i]);
printf("Case %d:\n",Case++);
while(m--){
int l,r,h;
scanf("%d%d%d",&l,&r,&h);
l++,r++;
h=upper_bound(b+1,b+ed+1,h)-b-1;
int ans=0;
if(h){
ans=query(root[l-1],root[r],1,ed,h);
}
printf("%d\n",ans);
}
}
return 0;
}
本文解析了一道以马里奥救公主为背景的算法题,利用线段树进行区间更新和查询操作,实现了高效求解马里奥跳跃过程中能够击中的砖块数量。
894

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



