# 【BZOJ1926】【SDOI2010】粟粟的书架（DP，主席树，二分）

BZOJ 同时被 3 个专栏收录
218 篇文章 0 订阅
91 篇文章 0 订阅
83 篇文章 0 订阅

## Solution

### R,C≤200 R , C ≤ 200 $R,C\le 200$

fi,j,k f i , j , k $f_{i,j,k}$表示子矩阵 (1,1)(i,j) ( 1 , 1 ) ( i , j ) $(1,1)(i,j)$中大于等于 k k $k$的数的和；
${g}_{i,j,k}$$g_{i,j,k}$表示子矩阵 (1,1)(i,j) ( 1 , 1 ) ( i , j ) $(1,1)(i,j)$中大于等于 k k $k$的个数。

## Source

/************************************************
* Au: Hany01
* Date: Feb 27th, 2018
* Prob: [BZOJ1926][SDOI2010] 粟粟的书架
* Email: hany01@foxmail.com
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}

inline void File()
{
#ifdef hany01
freopen("bzoj1926.in", "r", stdin);
freopen("bzoj1926.out", "w", stdout);
#endif
}

const int maxn = 500005, maxm = 205, maxk = 1005;

int a[maxm][maxm], n, m, sum[maxm][maxm][maxk], num[maxm][maxm][maxk], q;

inline void Solve1()
{
For(i, 1, n) For(j, 1, m) {
For(k, 1, a[i][j])
sum[i][j][k] = sum[i - 1][j][k] + sum[i][j - 1][k] - sum[i - 1][j - 1][k] + a[i][j],
num[i][j][k] = num[i - 1][j][k] + num[i][j - 1][k] - num[i - 1][j - 1][k] + 1;
For(k, a[i][j] + 1, 1000)
sum[i][j][k] = sum[i - 1][j][k] + sum[i][j - 1][k] - sum[i - 1][j - 1][k],
num[i][j][k] = num[i - 1][j][k] + num[i][j - 1][k] - num[i - 1][j - 1][k];
}
static int x1, y1, x2, y2, h, l, r, mid, Sum, Num;
while (q --) {
l = 1, r = 1000;
while (l < r) {
mid = (l + r + 1) >> 1;
if (sum[x2][y2][mid] + sum[x1][y1][mid] - sum[x1][y2][mid] - sum[x2][y1][mid] >= h) l = mid; else r = mid - 1;
}
Sum = sum[x2][y2][l] + sum[x1][y1][l] - sum[x1][y2][l] - sum[x2][y1][l];
Num = num[x2][y2][l] + num[x1][y1][l] - num[x1][y2][l] - num[x2][y1][l];
if (Sum >= h) printf("%d\n", Num - (Sum - h) / l); else puts("Poor QLW");
}
}

struct President_Tree
{
struct Node
{
int val, l, r, num;
}tr[maxn * 20];
int cnt;

#define mi ((l + r) >> 1)

int update(int last, int l, int r, int value)
{
int now = ++ cnt;
tr[now] = tr[last], ++ tr[now].num, tr[now].val += value;
if (l < r) {
if (value <= mi) tr[now].l = update(tr[last].l, l, mi, value);
else tr[now].r = update(tr[last].r, mi + 1, r, value);
}
return now;
}

int query(int x, int y, int l, int r, int k)
{
if (l == r) return l * k;
int num = tr[tr[y].l].num - tr[tr[x].l].num;
if (num >= k) return query(tr[x].l, tr[y].l, l, mi, k);
return tr[tr[y].l].val - tr[tr[x].l].val + query(tr[x].r, tr[y].r, mi + 1, r, k - num);
}

}PT;

inline void Solve2()
{
static int a[maxn], rt[maxn], l, r, mid, x, y, h, all, tmp;
n = m;
For(i, 1, n) rt[i] = PT.update(rt[i - 1], 1, 1000, a[i] = read());
while (q --) {
all = PT.query(rt[x - 1], rt[y], 1, 1000, y - x + 1);
if (all < h) { puts("Poor QLW"); continue; }
l = 1, r = y - x + 1;
while (l < r) {
mid = (l + r) >> 1;
if (all - PT.query(rt[x - 1], rt[y], 1, 1000, y - x + 1 - mid) >= h) r = mid; else l = mid + 1;
}
printf("%d\n", l);
}
}

int main()
{
File();
if (n > 1) Solve1(); else Solve2();
return 0;
}
//一年将尽夜，万里未归人。
//    -- 戴叔伦《除夜宿石头驿》
• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

01-08 94
04-07 974

01-07 172
07-01 23
04-10 22
02-22 43
12-26 640
03-09 776
10-27 335
04-02 1139
03-18 29
08-09 1万+
09-18 159
11-25 7356
03-29 678
03-06 87