zoukankan      html  css  js  c++  java
  • 线段树:HDU2795-Billboard(建树方式比较新奇)

    Billboard

    Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 23138 Accepted Submission(s): 9570

    Problem Description

    At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

    On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.

    Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.

    When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.

    If there is no valid location for a new announcement, it is not put on the billboard (that’s why some programming contests have no participants from this university).

    Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.

    Input

    There are multiple cases (no more than 40 cases).

    The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.

    Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.

    Output

    For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can’t be put on the billboard, output “-1” for this announcement.

    Sample Input

    3 5 5
    2
    4
    3
    3
    3

    Sample Output

    1
    2
    1
    3
    -1


    解题心得:

    1. 这个线段树的建树方式和模板的不同,他是将题意上的公告版顺时针旋转九十度,也就是从公告板高的一半开始建立节点,l和r都代表的是高度,只有当l等于r的时候才可以w的加减,而他的父节点代表的是他的两个子节点的最大的值,方便在搜索的时候找到可以进行加减的高度。但是要注意有一个优先级就是先从上到下,从左到右,在找的时候要先从左方开始找。
    2. 建树的图大概就是这样(不太规范,看看就好):
      这里写图片描述

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5+100;
    struct node
    {
        int l,r,w;
    }tree[maxn*4];
    int h,w,n,ans;
    
    
    //父节点仅仅代表两个子节点的最大值,用来在搜索的时候看是否可以传递到他的子节点中void updata(int root)
    {
        tree[root].w = max(tree[root<<1].w,tree[root<<1|1].w);
    }
    
    //先建一个树
    void build_tree(int l,int r,int root)
    {
        tree[root].l = l,tree[root].r = r;
        if(l == r)
        {
            tree[root].w = w;
            return ;
        }
        int mid = (l + r) >> 1;
        build_tree(l,mid,root<<1);
        build_tree(mid+1,r,root<<1|1);
        tree[root].w = w;
    }
    
    void query(int num,int root)
    {
        if(num > tree[1].w)//当前公告版最大的空处都不能放下的时候答案为-1
        {
            ans = -1;
            return ;
        }
        if(tree[root].l == tree[root].r)//只能够在最后一层子节点上面进行计算
        {
            tree[root].w -= num;
            ans = tree[root].l;
            return ;
        }
        if(tree[root<<1].w >= num)//先从左方开始找
            query(num,root<<1);
        else if(tree[root<<1|1].w >= num)
            query(num,root<<1|1);
        updata(root);//向上维护
    }
    
    int main()
    {
        while(scanf("%d%d%d",&h,&w,&n) != EOF)
        {
            h = min(h,200000);
            build_tree(1,h,1);
            while(n--)
            {
                int now;
                scanf("%d",&now);
                query(now,1);
                printf("%d
    ",ans);
            }
        }
    }
    
  • 相关阅读:
    集合
    网络
    File类
    laoshi
    石子合并《1》
    看球的巴士
    打鼹鼠~~线性DP
    题目分享:Wooden Sticks-线性动归
    pycharm怎么切换python版本
    Windows10下CMD输入Python没反应的解决方案
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107311.html
Copyright © 2011-2022 走看看