HDU - 4417 Super Mario 主席树+离散

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

题解:区间小于等于k的个数,同样是 主席树模板题 与这个类似 只是转化为求个数  点击查看

// 主席树模板
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
#define M(a, b) memset(a, b, sizeof(a))
#define lowbit(x) (x&(-x))
typedef long long ll;
const int N=1e5+10;

struct node
{
    int l,r;
    int val;
}tree[N * 21];

int n,q,a[N],id[N],root[N],cnt;
int build(int l,int r)
{
    int cur=cnt++;
    tree[cur].val=0;
    if(l==r)
    {
        tree[cur].l=0;
        tree[cur].r=0;
        return cur;
    }
    int mid=(r+l)>>1;
    tree[cur].l=build(l,mid);
    tree[cur].r=build(mid+1,r);
    return cur;
}
int update(int up,int tar,int l,int r)
{
    int cur=cnt++;
    tree[cur]=tree[up];
    tree[cur].val++;
    if(l==r) return cur;
    int mid=(r+l)>>1;
    if(tar<=mid) tree[cur].l=update(tree[up].l,tar,l,mid);
    else tree[cur].r=update(tree[up].r,tar,mid+1,r);
    return cur;
}
int query(int pl,int pr,int l,int r,int k)
{
    if(r<=k) return tree[pr].val-tree[pl].val;
    int mid=(r+l)>>1;
    int res=0;
    res+=query(tree[pl].l,tree[pr].l,l,mid,k);
    if(k>mid) res+=query(tree[pl].r,tree[pr].r,mid+1,r,k);
    return res;
}
int main()
{
    int T,nn=1;
    scanf("%d",&T);
    while(T--)
    {
        map<int,int> mp;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            id[i]=a[i];
        }
        sort(id+1,id+1+n);
        int len=unique(id+1,id+1+n)-(id+1);
        cnt=0;
        root[0]=build(1,len);
        for(int i=1;i<=n;i++)
        {
            int p=lower_bound(id+1,id+1+len,a[i])-id;
            root[i]=update(root[i-1],p,1,len);
        }
        int l,r,k;
        printf("Case %d:\n",nn++);
        while(q--)
        {
            scanf("%d%d%d",&l,&r,&k);
            l++,r++;
            k=upper_bound(id+1,id+1+len,k)-id-1;等于k可能没有所以求第一个大于k的再减一即可
          //  cout<<k<<endl;
            if(k==0) printf("0\n");
            else printf("%d\n",query(root[l-1],root[r],1,len,k));
        }
    }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值