1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include <cstdio> #include <algorithm> using namespace std; int n, m, q; namespace BruteForce { const int N = 210; int mn = 1000, mx = 1, w[N][N], c[1010][N][N], s[1010][N][N]; int main() { for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf("%d", &w[i][j]); mx = max(mx, w[i][j]); mn = min(mn, w[i][j]); } for (int k = 1; k <= 1000; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { c[k][i][j] = c[k][i - 1][j] + c[k][i][j - 1] - c[k][i - 1][j - 1] + (w[i][j] >= k); s[k][i][j] = s[k][i - 1][j] + s[k][i][j - 1] - s[k][i - 1][j - 1] + (w[i][j] >= k ? w[i][j] : 0); } for (int l1, r1, l2, r2, h; q; q--) { scanf("%d%d%d%d%d", &l1, &r1, &l2, &r2, &h); if (s[1][l2][r2] - s[1][l1 - 1][r2] - s[1][l2][r1 - 1] + s[1][l1 - 1][r1 - 1] < h) { puts("Poor QLW"); continue; } int l = mn, r = mx; while (l < r) { int mid = l + r + 1 >> 1; s[mid][l2][r2] - s[mid][l1 - 1][r2] - s[mid][l2][r1 - 1] + s[mid][l1 - 1][r1 - 1] < h ? r = mid - 1 : l = mid; } printf("%d\n", c[l][l2][r2] - c[l][l1 - 1][r2] - c[l][l2][r1 - 1] + c[l][l1 - 1][r1 - 1] - (s[l][l2][r2] - s[l][l1 - 1][r2] - s[l][l2][r1 - 1] + s[l][l1 - 1][r1 - 1] - h) / l); } return 0; } } namespace PresidentTree { const int N = 5e5 + 10, M = 2e5 + 10; struct Node { long long s; int l, r, c; } tr[M]; int idx; int w[N], rt[N]; void pushup(int x) { tr[x].c = tr[tr[x].l].c + tr[tr[x].r].c; tr[x].s = tr[tr[x].l].s + tr[tr[x].r].s; } void modify(int &x, int l, int r, int t, int k) { tr[++idx] = tr[x]; x = idx; if (l == r) { tr[x].c++; tr[x].s += k; return; } int mid = l + r >> 1; if (t <= mid) modify(tr[x].l, l, mid, t, k); else modify(tr[x].r, mid + 1, r, t, k); pushup(x); } int query(int x1, int x2, int l, int r, int h) { if (l == r) return (h - 1) / l + 1; int mid = l + r >> 1; int rc = tr[tr[x2].r].c - tr[tr[x1].r].c; long long rs = tr[tr[x2].r].s - tr[tr[x1].r].s; if (h <= rs) return query(tr[x1].r, tr[x2].r, mid + 1, r, h); return query(tr[x1].l, tr[x2].l, l, mid, h - rs) + rc; } int main() { for (int i = 1; i <= m; i++) { scanf("%d", &w[i]); rt[i] = rt[i - 1]; modify(rt[i], 1, 1000, w[i], w[i]); } for (int a, l, r, h; q; q--) { scanf("%d%d%d%d%d", &a, &l, &a, &r, &h); if (tr[rt[r]].s - tr[rt[l - 1]].s < h) puts("Poor QLW"); else printf("%d\n", query(rt[l - 1], rt[r], 1, 1000, h)); } return 0; } } int main() { scanf("%d%d%d", &n, &m, &q); if (n == 1) return PresidentTree::main(); else return BruteForce::main(); }
|