HDU 2795 线段树单点更新

Billboard

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


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,如果找到就让这个数更新为此数减去询问值后的值。
暴力寻找得话会T,考虑适当的数据结构来简化操作。
进一步的思考时候发现,如果一段区间分为两部分只要前面部分的最大值>=询问值,我们就不必考虑后面部分的数了,由此想到使用线段树,维护一个区间最大值即可。由于更新和查找都是log级别不会T。
还有一点就是这个最大的区间并不一定是[1,H],H最大1e9,不可能开出这个多空间保存节点,我们注意到N最大就是20w,由于要尽可能的将可以放置的数放在前面,
所以最大也就占用20w行,换句话说R最小的最大区间应是 [1,min(H,N)];这里的区间[l,r]指的是从l行到r行,所以maxn[i]就是第i个节点对应的所有行中剩余的最大宽度。
还是不太熟练犯了许多小错误,例如将结点值与L,R搞混。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int maxn[(200000<<2)+20];
 4 int H,N,W,X;
 5 void build()
 6 {
 7  int M=X<<2;
 8  for(int i=1;i<=M;++i) maxn[i]=W;
 9 }
10 void update(int L,int R,int id,int tar,int x)
11 {
12    int m=(L+R)>>1,lc=id<<1,rc=(id<<1)|1;
13    if(L==R&&L==tar) {maxn[id]=x;return;}
14    if(tar<=m){
15      update(L,m,lc,tar,x);
16    }
17    else{
18     update(m+1,R,rc,tar,x);
19    }
20    maxn[id]=max(maxn[lc],maxn[rc]);
21 }
22 int ask(int L,int R,int id,int x)
23 {
24     if(maxn[id]<x) return -1;
25     if(L==R) {update(1,X,1,L,maxn[id]-x);return L;}
26     int m=(L+R)>>1,lc=id<<1,rc=(id<<1)|1;
27     if(maxn[lc]>=x){
28         return ask(L,m,lc,x);
29     }
30     else{
31         return ask(m+1,R,rc,x);
32     }
33 }
34 int main()
35 {
36     int i,j,k,wi;
37     while(cin>>H>>W>>N){X=min(H,N);
38         build();
39         for(i=1;i<=N;++i){
40             scanf("%d",&wi);
41             printf("%d\n",ask(1,X,1,wi));
42         }
43     }
44     return 0;
45 }

 

 
posted @ 2017-07-30 22:06  *zzq  阅读(133)  评论(0编辑  收藏  举报