Billboard(线段树单点更新)easy

Billboard

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


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

 

题目的意思是给你 h,w,n,代表墙的高度、宽度、查询的次数,每次查询给你一张海报的宽度,海报的高度为1,你需要按顺序将这些海报粘贴到墙上,不能覆盖已经使用了的区域,粘贴海报时,你应该选择比较高的地方,同一高度,尽量靠左边

 

这里我们以高度建树,这里高度虽然给的范围是1e9,但是n只有200000,所以高度最高为2e5,在每一个节点设用一个数来保存剩余的宽度,查询到的每一个高度更新当前的剩余值

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 struct node
 6 {
 7     int l,r;
 8     int mx;
 9 } t[200005*4];
10 int h,w,n,k;
11 int ans[200005];
12 void build(int rt,int l,int r)
13 {
14     t[rt].l=l;
15     t[rt].r=r;
16     //t[rt].mx=w;
17     if(t[rt].l==t[rt].r)
18     {
19         t[rt].mx=w;
20         return ;
21     }
22     int mid=(l+r)>>1;
23     build(rt<<1,l,mid);
24     build(rt<<1|1,mid+1,r);
25     t[rt].mx=max(t[rt<<1].mx,t[rt<<1|1].mx);
26 
27 }
28 void update(int rt,int a)///a更新
29 {
30     if(t[rt].l==t[rt].r)
31     {
32         t[rt].mx-=a;
33         ans[k]=t[rt].l;
34         return ;
35     }
36     if(a<=t[rt<<1].mx)
37         update(rt<<1,a);
38     else
39         update(rt<<1|1,a);
40     t[rt].mx=max(t[rt<<1].mx,t[rt<<1|1].mx);
41 }
42 int main()
43 {
44 
45     while(~scanf("%d%d%d",&h,&w,&n))
46     {
47         if(h>n)
48             h=n;
49         build(1,1,h);
50         k=0;
51         for(int i=1; i<=n; i++)
52         {
53             int x;
54             scanf("%d",&x);
55            // printf("*%d\n",t[1].mx);
56             if(x>t[1].mx)
57                 ans[k]=-1;
58             else
59                 update(1,x);
60                 k++;
61         }
62         for(int i=0; i<k; i++)
63             printf("%d\n",ans[i]);
64 
65     }
66     return 0;
67 }

 

posted @ 2019-05-08 19:47  Q1311605467  阅读(252)  评论(0编辑  收藏  举报