HDU-4417 Super Mario

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.

InputThe 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.)OutputFor 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

 题意:求区间[l-r]的<=k的个数

裸的主席树,离散化一下就行了。

#include<bits/stdc++.h>
using namespace std;
#define Debug(x) cout<<#x<<":"<<(x)<<endl
typedef long long ll;
const int maxn = 100000+5;
struct node
{
    int l,r,num;
}tree[maxn*20];
int a[maxn],b[maxn],rt[maxn],cnt;
void pushup(int k)
{
    tree[k].num=tree[tree[k].l].num+tree[tree[k].r].num;
}
void build(int l,int r,int &x)
{
    x=++cnt;
    tree[x].num=0;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(l,m,tree[x].l);
    build(m+1,r,tree[x].r);
    pushup(x);
}
int query(int l,int r,int pre,int now,int x,int y)
{
    if(x<=l&&r<=y) return tree[now].num-tree[pre].num;
    int m=(l+r)>>1;
    int ans=0;
    if(x<=m) ans+=query(l,m,tree[pre].l,tree[now].l,x,y);
    if(y>m) ans+=query(m+1,r,tree[pre].r,tree[now].r,x,y);
    return ans;
}
void updata(int l,int r,int pre,int &now,int k)
{
    now=++cnt;
    tree[now]=tree[pre];
    if(l==r)
    {
        tree[now].num++;
        return ;
    }
    int m=(l+r)>>1;
    if(k<=m) updata(l,m,tree[pre].l,tree[now].l,k);
    else updata(m+1,r,tree[pre].r,tree[now].r,k);
    pushup(now);
}
int main()
{
    int n,m,t,p=1;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case %d:\n",p++);
        scanf("%d %d",&n,&m);
        {
            cnt=0;
            for(int i=1; i<=n; i++)
                scanf("%d",&a[i]),b[i]=a[i];
            sort(b+1,b+1+n);
            int len=unique(b+1,b+1+n)-(b+1);
            for(int i=1; i<=n; i++)
                a[i]=lower_bound(b+1,b+1+len,a[i])-b;
            build(1,len,rt[0]);
            for(int i=1; i<=n; i++)
                updata(1,len,rt[i-1],rt[i],a[i]);
            while(m--)
            {
                int l,r,k1,k;
                scanf("%d %d %d",&l,&r,&k);
                l++;
                r++;
                k1=lower_bound(b+1,b+1+len,k)-b;
                if(b[k1]!=k) k1--;
                if(k1==0)//不判断会出现MLE 
                {
                    printf("0\n");
                    continue;
                }
                printf("%d\n",query(1,len,rt[l-1],rt[r],1,k1));
            }
        }
    }
}

 

 

 

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

转载于:https://www.cnblogs.com/MengX/p/9104963.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值