# BZOJ1926: [Sdoi2010]粟粟的书架

## 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

## Solution

（调试没删结果一直wa调了1h）

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 500010, M = 5500100;

namespace io {
#define gc getchar
inline void in(int &x) {
x = 0; int f = 1; char c = gc();
while(c < '0' || c > '9') { if(c=='-') f = -1; c = gc(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = gc(); }
x *= f;
}
inline void in_l(ll &x) {
x = 0; ll f = 1; char c = gc();
while(c < '0' || c > '9') { if(c=='-') f = -1; c = gc(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = gc(); }
x *= f;
}
}using namespace io;

/*

*/

int a[N], root[N], tot;
int R, C, m;

struct tree {
int lc, rc, cnt;
int sum;
} t[M];
#define lc(x) (t[x].lc)
#define rc(x) (t[x].rc)
int len = 0;
ll S[N];
void upd(int last, int &rt, int l, int r, int pos, ll v) {
t[rt=++tot] = t[last]; t[rt].cnt++; t[rt].sum += v;
if(l == r) return;
int mid = (l + r) >> 1;
if(pos <= mid) upd(lc(last), lc(rt), l, mid, pos, v);
else upd(rc(last), rc(rt), mid + 1, r, pos, v);
}
ll query(int L, int R, int l, int r, int k) {
int sum = t[lc(R)].cnt - t[lc(L)].cnt, mid = (l + r) >> 1;
if(l == r) return l * k;
if(k <= sum) return query(lc(L), lc(R), l, mid, k);
else return t[lc(R)].sum - t[lc(L)].sum + query(rc(L), rc(R), mid + 1, r, k - sum);
}
void solve() {
for(int i = 1; i <= C; ++i) in(a[i]), S[i] = S[i - 1] + a[i];
for(int i = 1; i <= C; ++i) {
upd(root[i-1], root[i], 1, 1000, a[i], a[i]);
}
for(int i = 1; i <= m; ++i) {
int o, l, r; ll H;
in(o), in(l), in(o), in(r); in_l(H);
if(S[r] - S[l - 1] < H) puts("Poor QLW");
else {
int L = 0, R = r - l + 1, ans = R;
while(L <= R) {
int mid = (L + R) >> 1; //前mid不选
ll sum = (S[r] - S[l - 1]) - query(root[l - 1], root[r], 1, 1000, mid);
if(sum >= H) ans = (r-l+1) - mid, L = mid + 1;
else R = mid - 1;
//               printf("%lld %d %lld\n", (S[r] - S[l - 1]), mid, sum);
}
printf("%d\n", ans);
}
}
}
#undef lc
#undef rc
}

int v[1010][202][202];
int num[1010][202][202], a[202][202];

ll calc(int k, int l1, int r1, int l2, int r2) {
return v[k][l2][r2] + v[k][l1-1][r1-1] - v[k][l1-1][r2] - v[k][l2][r1-1];
}

ll calc_num(int k, int l1, int r1, int l2, int r2) {
return num[k][l2][r2] + num[k][l1-1][r1-1] - num[k][l1-1][r2] - num[k][l2][r1-1];
}

void solve() {
for(int i = 1; i <= R; ++i) {
for(int j = 1; j <= C; ++j) scanf("%d", &a[i][j]);
}
for(int k = 0; k <= 1000; ++k)
for(int i = 1; i <= R; ++i) {
for(int j = 1; j <= C; ++j) {
num[k][i][j] = num[k][i][j-1] + num[k][i-1][j] - num[k][i-1][j-1],
v[k][i][j] = v[k][i][j-1] + v[k][i-1][j] - v[k][i-1][j-1];
if(a[i][j] >= k) num[k][i][j]++, v[k][i][j] += a[i][j];
}
}
for(int i = 1; i <= m; ++i) {
int l1, r1, l2, r2; ll H;
in(l1), in(r1), in(l2), in(r2), in_l(H);
if(calc(0, l1, r1, l2, r2) < H) puts("Poor QLW");
else {
int ans = 0, l = 1, r = 1000;
while(l <= r) {
int mid = (l + r) >> 1;
if(calc(mid, l1, r1, l2, r2) >= H) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", calc_num(ans, l1, r1, l2, r2) - (calc(ans, l1, r1, l2, r2) - H) / ans);
}
}
}
}

int main() {
in(R), in(C), in(m);