# BZOJ1926 [Sdoi2010]粟粟的书架 【主席树 + 二分 + 前缀和】

## 题目

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

## 题解

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
#define lbt(x) (x & -x)
using namespace std;
const int maxn = 500005,maxm = 10000005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int ls[maxm],rs[maxm],sum[maxm],cnt[maxm],rt[maxn],siz;
int n,m,Q,N = 1000;
void modify(int& u,int pre,int l,int r,int pos){
u = ++siz; ls[u] = ls[pre]; rs[u] = rs[pre];
cnt[u] = cnt[pre] + 1; sum[u] = sum[pre] + pos;
if (l == r) return;
int mid = l + r >> 1;
if (mid >= pos) modify(ls[u],ls[pre],l,mid,pos);
else modify(rs[u],rs[pre],mid + 1,r,pos);
}
int query(int u,int v,int l,int r,int k){
if (cnt[u] - cnt[v] == k) return sum[u] - sum[v];
if (l == r) return (sum[u] - sum[v]) / (cnt[u] - cnt[v]) * k;
int mid = l + r >> 1,t = cnt[rs[u]] - cnt[rs[v]];
if (t < k) return query(ls[u],ls[v],l,mid,k - t) + sum[rs[u]] - sum[rs[v]];
else return query(rs[u],rs[v],mid + 1,r,k);
}
void solve1(){
for (int i = 1; i <= m; i++)
int L,R,h;
while (Q--){
if (sum[rt[R]] - sum[rt[L]] < h){
puts("Poor QLW"); continue;
}
int l = 1,r = cnt[rt[R]] - cnt[rt[L]],mid;
while (l < r){
mid = l + r >> 1;
if (query(rt[R],rt[L],1,N,mid) >= h) r = mid;
else l = mid + 1;
}
printf("%d\n",l);
}
}
int num[205][205][1005],tot[205][205][1005],x,y,xx,yy,h;
int S(int mid){
return num[xx][yy][mid] - num[x - 1][yy][mid] - num[xx][y - 1][mid] + num[x - 1][y - 1][mid];
}
int C(int mid){
return tot[xx][yy][mid] - tot[x - 1][yy][mid] - tot[xx][y - 1][mid] + tot[x - 1][y - 1][mid];
}
void solve2(){
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
int x = read();
for (int k = x; k; k--)
num[i][j][k] = x,tot[i][j][k] = 1;
for (int k = 1; k <= N; k++)
num[i][j][k] += num[i - 1][j][k] + num[i][j - 1][k] - num[i - 1][j - 1][k],
tot[i][j][k] += tot[i - 1][j][k] + tot[i][j - 1][k] - tot[i - 1][j - 1][k];
}
int l,r,mid;
while (Q--){
if (S(1) < h) {puts("Poor QLW"); continue;}
l = 1; r = 1000;
while (l < r){
mid = l + r + 1 >> 1;
if (S(mid) >= h) l = mid;
else r = mid - 1;
}
int t = h - S(l + 1);
printf("%d\n",C(l + 1) + (t % l == 0 ? t / l : t / l + 1));
}
}
int main(){