zoukankan      html  css  js  c++  java
  • 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.

    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

    代码:
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    
    struct node
    {
        int l,r;
        int num;
    }tree[maxn<<2];
    
    struct node1
    {
        int pos,l,r,h;
        bool friend operator <(node1 x, node1 y)
        {
            return x.h<y.h;
        }
    }h[maxn];
    struct node2
    {
        int pos,h;
        bool friend operator <(node2 x,node2 y)
        {
            return x.h<y.h;
        }
    }p[maxn];
    
    void pushup(int m)
    {
        tree[m].num=(tree[m<<1].num+tree[m<<1|1].num);
        return;
    }
    void build(int m,int l,int r)
    {
        tree[m].l=l;
        tree[m].r=r;
        tree[m].num=0;
        if(l==r)
        {
            return ;
        }
        int mid=(tree[m].l+tree[m].r)>>1;
        build(m<<1,l,mid);
        build(m<<1|1,mid+1,r);
        return;
    }
    void update(int m,int index,int val)
    {
        if(tree[m].l==index&&tree[m].l==tree[m].r)
        {
            tree[m].num=val;
            return ;
        }
        int mid=(tree[m].l+tree[m].r)>>1;
        if(index<=mid)
        {
            update(m<<1,index,val);
        }
        else
        {
            update(m<<1|1,index,val);
        }
        pushup(m);
        return ;
    }
    int  query(int m,int l,int r)
    {
        if(tree[m].l==l&&tree[m].r==r)
        {
            return tree[m].num;
        }
        int mid=(tree[m].l+tree[m].r)>>1;
        if(r<=mid)
        {
            return query(m<<1,l,r);
        }
        else if(l>mid)
        {
            return query(m<<1|1,l,r);
        }
        else
        {
            return query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
        }
    }
    int a[maxn];
    int main()
    {
       int T;
       cin>>T;
       for(int k=1;k<=T;k++)
       {
           int n,m;
           scanf("%d%d",&n,&m);
           for(int t=1;t<=n;t++)
        {
            scanf("%d",&p[t].h);
            p[t].pos=t;
        }
        for(int t=1;t<=m;t++)
        {
            scanf("%d%d%d",&h[t].l,&h[t].r,&h[t].h);
            h[t].l++;
            h[t].r++;
            h[t].pos=t;
        }
    
        printf("Case %d:
    ",k);
        build(1,1,n);
        sort(p+1,p+n+1);
        sort(h+1,h+m+1);
        int j=1;
        int s=1;
        while(s<=n)
        {
            if(p[s].h>h[j].h)
            {
            //    cout<<h[j].l<<" "<<h[j].r<<endl;
                a[h[j].pos]=query(1,h[j].l,h[j].r);
                j++;
                if(j>m)
                {
                    break;
                }
            }
            else
            {
                update(1,p[s].pos,1);
                s++;
            }
        }
        while(j<=m)
        {
                a[h[j].pos]=query(1,h[j].l,h[j].r);
                j++;
        }
        for(int t=1;t<=m;t++)
        {
            printf("%d
    ",a[t]);
        }
       }
    
       return 0;
    }
  • 相关阅读:
    Python List+Tuple+Dict+Set小结
    小命要紧~风热风寒感冒
    IDEA忽略编译报错
    IDEA新建一个Spring Boot项目
    ip2region.jar实现ip转地址
    IDEA控制台中文乱码问题
    IDEA报错Plugin "XXX" was not loaded: required plugin "Java EE: EJB, JPA, Servlets" is disabled.
    IDEA Error:java: Compilation failed: internal java compiler error
    Failed to start component [StandardEngine[Catalina].StandardHost[localhost]
    浏览网页隐藏服务器IP
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11301116.html
Copyright © 2011-2022 走看看