hdu4417 主席树求区间小于等于K

本文提供了一道来自HDU ACM在线评测系统的编程题的解答过程,题目要求计算马里奥在跳跃高度限制下,能击中的砖块数量。使用主席树数据结构解决区间查询问题,实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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
 
主席树板子题,求区间小于等于h的数有多少,注意题目给的查询区间是从0开始的
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100005
int a[maxn],b[maxn],T[maxn<<5],L[maxn<<5],R[maxn<<5],sum[maxn<<5],tot;
inline int update(int pre,int l,int r,int x)
{
    int rt=++tot;
    L[rt]=L[pre];
    R[rt]=R[pre];
    sum[rt]=sum[pre]+1;
    if(l<r)
    {
        int mid=l+r>>1;
        if(x<=mid)L[rt]=update(L[pre],l,mid,x);
        else R[rt]=update(R[pre],mid+1,r,x);
    }
    return rt;
}
inline int query(int u,int v,int ql,int qr,int l,int r)
{
    if(ql<=l&&qr>=r)return sum[v]-sum[u];
    int mid=l+r>>1,ans=0;
    if(ql<=mid)ans+=query(L[u],L[v],ql,qr,l,mid);
    if(qr>mid)ans+=query(R[u],R[v],ql,qr,mid+1,r);
    return ans;
}
int main()
{
    int t;
    cin>>t;
    for(int W=1;W<=t;W++)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(b+1,b+1+n);
        int len=unique(b+1,b+1+n)-b-1;
        T[0]=sum[0]=L[0]=R[0]=tot=0;
        for(int i=1;i<=n;i++)
        {
            int pos=lower_bound(b+1,b+1+len,a[i])-b;
            T[i]=update(T[i-1],1,len,pos);
        }
        cout<<"Case "<<W<<":"<<endl;
        for(int i=1;i<=m;i++)
        {
            int l,r,h;
            cin>>l>>r>>h;
            int pos=upper_bound(b+1,b+1+len,h)-b;
            pos--;
            if(!pos)cout<<"0"<<endl;
            else
            {
                cout<<query(T[l],T[r+1],1,pos,1,len)<<endl;
            }
        }
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/chen99/p/11293955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值