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;
    }
  • 相关阅读:
    MySQL 4.1x 中文乱码效果
    linux内核中的“捏造化”
    Ubuntu开发者峰会在布拉格举行
    Decode 函数的用法
    Solaris 10拆卸jdk1.6及点窜成默许JDK
    教你编写高机能的mysql语法
    DirectShow9.0在vs2005中存在的问题解决
    Unicode,unicoidebig,Asci,UTF8文件read和write
    自已写了个GDI类,实现了相对路径载入任意类型的图片函数,并加一个在CRECT矩形上贴图的函数(5月25日写)
    两种解析EDIT控件上文本的方式
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11301116.html
Copyright © 2011-2022 走看看