# BZOJ 1926: [Sdoi2010]粟粟的书架

## 1926: [Sdoi2010]粟粟的书架

Time Limit: 30 Sec  Memory Limit: 552 MB
Submit: 730  Solved: 286
[Submit][Status][Discuss]

## Description

rmen 的文章。粟粟家中有一个 R行C 列的巨型书架，书架的每一个位置都摆有一本书，上数第i 行、左数第j 列

y2i－y1i＋1﹚本书中挑选若干本垫在脚下，摘取苹果。粟粟每次取书时都能及时放回原位，并且她的书架不会再

5 5 7
14 15 9 26 53
58 9 7 9 32
38 46 26 43 38
32 7 9 50 28
8 41 9 7 17
1 2 5 3 139
3 1 5 5 399
3 3 4 5 91
4 1 4 1 33
1 3 5 4 185
3 3 4 3 23
3 1 3 3 108

6
15
2
Poor QLW
9
1
3

## Source

[Submit][Status][Discuss]

  1 #include <cstdio>
2
3 int R, C, M;
4
5 namespace case1
6 {
7     int P[500005];
8     int T[500005];
9     int S[500005];
10
11     int tot = 0;
12     int cnt[10000005];
13     int sum[10000005];
14     int lsn[10000005];
15     int rsn[10000005];
16
17     void insert(int &t, int p, int l, int r, int v)
18     {
19         t = ++tot;
20
21         lsn[t] = lsn[p];
22         rsn[t] = rsn[p];
23         cnt[t] = cnt[p] + 1;
24         sum[t] = sum[p] + v;
25
26         if (l != r)
27         {
28             int mid = (l + r) >> 1;
29
30             if (v <= mid)
31                 insert(lsn[t], lsn[p], l, mid, v);
32             else
33                 insert(rsn[t], rsn[p], mid + 1, r, v);
34         }
35     }
36
37     int query(int a, int b, int l, int r, int h)
38     {
39         if (l == r)
40             return (h + l - 1) / l;
41
42         int mid = (l + r) >> 1, s;
43
44         if ((s = sum[rsn[a]] - sum[rsn[b]]) >= h)
45             return query(rsn[a], rsn[b], mid + 1, r, h);
46         else
47             return query(lsn[a], lsn[b], l, mid, h - s) + cnt[rsn[a]] - cnt[rsn[b]];
48     }
49
50     inline void solve(void)
51     {
52         for (int i = 1; i <= C; ++i)
53         {
54             scanf("%d", P + i);
55             S[i] = S[i - 1] + P[i];
56             insert(T[i], T[i - 1], 1, 1000, P[i]);
57         }
58
59         for (int i = 1, l, r, h; i <= M; ++i)
60         {
61             scanf("%*d%d%*d%d%d", &l, &r, &h);
62
63             if (S[r] - S[l - 1] < h)
64                 puts("Poor QLW");
65             else
66                 printf("%d\n", query(T[r], T[l - 1], 1, 1000, h));
67         }
68     }
69 }
70
71 namespace case2
72 {
73     int P[205][205];
74     int F[205][205][1005];
75     int G[205][205][1005];
76
77     inline void solve(void)
78     {
79         for (int i = 1; i <= R; ++i)
80             for (int j = 1; j <= C; ++j)
81                 scanf("%d", &P[i][j]);
82
83         for (int i = 1; i <= R; ++i)
84             for (int j = 1; j <= C; ++j)
85                 for (int k = 1; k <= 1000; ++k)
86                     G[i][j][k] = G[i - 1][j][k] + G[i][j - 1][k] - G[i - 1][j - 1][k] + (P[i][j] >= k),
87                     F[i][j][k] = F[i - 1][j][k] + F[i][j - 1][k] - F[i - 1][j - 1][k] + (P[i][j] >= k ? P[i][j] : 0);
88
89         for (int i = 1, x1, y1, x2, y2, h; i <= M; ++i)
90         {
91             scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &h);
92
93             int lt = 1, rt = 1000, mid, ans = -1;
94
95             while (lt <= rt)
96             {
97                 mid = (lt + rt) >> 1;
98
99                 int S = F[x2][y2][mid] + F[x1 - 1][y1 - 1][mid] - F[x2][y1 - 1][mid] - F[x1 - 1][y2][mid];
100
101                 if (S >= h)
102                     lt = mid + 1, ans = mid;
103                 else
104                     rt = mid - 1;
105             }
106
107             if (ans == -1)
108                 puts("Poor QLW");
109             else
110                 printf("%d\n", G[x2][y2][ans + 1] + G[x1 - 1][y1 - 1][ans + 1] - G[x2][y1 - 1][ans + 1] - G[x1 - 1][y2][ans + 1] + (h - F[x2][y2][ans + 1] - F[x1 - 1][y1 - 1][ans + 1] + F[x1 - 1][y2][ans + 1] + F[x2][y1 - 1][ans + 1] + ans - 1) / ans);
111         }
112     }
113 }
114
115 signed main(void)
116 {
117     scanf("%d%d%d", &R, &C, &M);
118
119     if (R == 1)
120         case1::solve();
121     else
122         case2::solve();
123 }

@Author: YouSiki

posted @ 2017-02-15 10:38  YouSiki  阅读(150)  评论(0编辑  收藏