zoukankan      html  css  js  c++  java
  • 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:
    ",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
    ");
                        continue;
                    }
                    printf("%d
    ",query(1,len,rt[l-1],rt[r],1,k1));
                }
            }
        }
    }

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

  • 相关阅读:
    web安全:防止浏览器记住或自动填写用户名和密码(表单)的解决方案
    Sql CLR创建一个简单的表值函数
    html中实现某区域内右键自定义菜单
    HTML5单页框架View.js介绍
    Java使用apache的开源数据处理框架commons-dbutils完成查询结果集的各种处理输出(8种方式)
    【EAS】【BOS】如果使用BOS开发工具连接远程应用服务进行调试
    VSFTPD 550 Delete operation failed.
    两款不错的js甘特图控件
    我的node-webkit笔记
    node-webkit,nwjs 系统托盘【Tray】实践
  • 原文地址:https://www.cnblogs.com/MengX/p/9104963.html
Copyright © 2011-2022 走看看