码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-4417 Super Mario

时间:2018-05-29 15:00:29      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:inpu   sort   font   mes   各路   mario   离散化   lse   number   

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:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

HDU-4417 Super Mario

标签:inpu   sort   font   mes   各路   mario   离散化   lse   number   

原文地址:https://www.cnblogs.com/MengX/p/9104963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!