1. 程式人生 > >HDU 2795 線段樹單點更新

HDU 2795 線段樹單點更新

miss imp ember name specific ssi chan order start

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 }

HDU 2795 線段樹單點更新