首页 > 代码库 > 【HDOJ】2795 Billboard

【HDOJ】2795 Billboard

线段树。注意h范围(小于等于n)。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define MAXN 200005
 5 #define lson l, mid, rt<<1
 6 #define rson mid+1, r, rt<<1|1
 7 #define mymax(x, y) (x>y) ? x:y
 8 
 9 int nums[MAXN<<2];
10 int h, w;
11 
12 void PushUP(int rt) {
13     nums[rt] = mymax(nums[rt<<1], nums[rt<<1|1]);
14 }
15 
16 void build(int l, int r, int rt) {
17     int mid;
18     nums[rt] = w;
19 
20     if (l == r)
21         return ;
22 
23     mid = (l+r)>>1;
24     build(lson);
25     build(rson);
26 }
27 
28 int query(int wi, int l, int r, int rt) {
29     int mid, ret;
30 
31     if (l == r) {
32         nums[rt] -= wi;
33         return l;
34     }
35     mid = (l+r)>>1;
36 
37     if (nums[rt<<1] >= wi)
38         ret = query(wi, lson);
39     else
40         ret = query(wi, rson);
41 
42     PushUP(rt);
43     return ret;
44 }
45 
46 int main() {
47     int n, wi;
48 
49     while (scanf("%d%d%d", &h, &w, &n) != EOF) {
50         if (h > n)
51             h = n;      // h < n
52         build(1, h, 1);
53         while (n--) {
54             scanf("%d", &wi);
55             if (nums[1] < wi)
56                 printf("-1\n");
57             else
58                 printf("%d\n", query(wi, 1, h, 1));
59         }
60     }
61 
62     return 0;
63 }