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
| #include <cstdio> #include <algorithm> using namespace std; const int Mn(205),Mp(1050),Mc(500500);
struct pnode { long long sz,sum; pnode *ls,*rs; }*roots[Mn][Mc],*nil; void init_nil() { nil = new pnode((pnode) {0,0,nil,nil}); } void init_root0(pnode*& rt,int l,int r) { rt = new pnode(); rt->sz = rt->sum = 0; if(l==r) { rt->ls = rt->rs = nil; return; } else { int mid((l+r)/2); init_root0(rt->ls,l,mid); init_root0(rt->rs,mid+1,r); } } void mdf(pnode*& lv,int mp,int l,int r) { lv = new pnode(*lv); lv->sz += 1; lv->sum += mp; if(l==r) { return; } int mid((l+r)/2); if(mp<=mid) { mdf(lv->ls,mp,l,mid); } else { mdf(lv->rs,mp,mid+1,r); } }
pnode *lrt[Mn], *rrt[Mn]; int qry(int rtl,int l,int r,int h) { if(l==r) { return h/l+(h%l!=0); } int mid((l+r)/2); int rsum(0); for(int i(1);i<=rtl;++i) { rsum += rrt[i]->rs->sum - lrt[i]->rs->sum; } if(rsum<h) { int rsz(0); for(int i(1);i<=rtl;++i) { rsz += rrt[i]->rs->sz - lrt[i]->rs->sz; lrt[i] = lrt[i]->ls; rrt[i] = rrt[i]->ls; } return rsz + qry(rtl,l,mid,h-rsum); } else { for(int i(1);i<=rtl;++i) { lrt[i] = lrt[i]->rs; rrt[i] = rrt[i]->rs; } return qry(rtl,mid+1,r,h); } }
int main() { init_nil(); int r,c,m; scanf("%d %d %d",&r,&c,&m); for(int i(1);i<=r;++i) { init_root0(roots[i][0],1,Mp); for(int j(1);j<=c;++j) { roots[i][j] = roots[i][j-1]; int p; scanf("%d",&p); mdf(roots[i][j],p,1,Mp); } } while(m--) { int x1,y1,x2,y2,h; scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&h); int hsum(0); for(int i(x1);i<=x2;++i) { lrt[i-x1+1] = roots[i][y1-1]; rrt[i-x1+1] = roots[i][y2]; hsum += rrt[i-x1+1]->sum - lrt[i-x1+1]->sum; } if(hsum<h) { printf("Poor QLW\n"); } else { printf("%d\n",qry(x2-x1+1,1,Mp,h)); } } return 0; }
|