# [Sdoi2010]粟粟的书架

## [Sdoi2010]粟粟的书架

/*
mail: mleautomaton@foxmail.com
author: MLEAutoMaton
This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
#define ERROR puts("Poor QLW");
int r,c,m,h,x1,x2,y1,y2;
const int N=210,NN=2000010;
int p[N][N],sum[N][N][1010],num[N][N][1010];
int Num(int x1,int y1,int x2,int y2,int mid){return num[x2][y2][mid]-num[x2][y1-1][mid]-num[x1-1][y2][mid]+num[x1-1][y1-1][mid];}
int Sum(int x1,int y1,int x2,int y2,int mid){return sum[x2][y2][mid]-sum[x2][y1-1][mid]-sum[x1-1][y2][mid]+sum[x1-1][y1-1][mid];}
int number(int x1,int y1,int x2,int y2,int h,int mid){return Num(x1,y1,x2,y2,mid)-(Sum(x1,y1,x2,y2,mid)-h)/mid;}
bool check(int mid){return Sum(x1,y1,x2,y2,mid)>=h;}
void work1()
{
int mx=0;
REP(i,1,r)REP(j,1,c)p[i][j]=gi(),mx=max(mx,p[i][j]);
REP(k,0,mx)REP(i,1,r)REP(j,1,c)
{
num[i][j][k]=num[i-1][j][k]+num[i][j-1][k]-num[i-1][j-1][k]+(p[i][j]>=k);
sum[i][j][k]=sum[i-1][j][k]+sum[i][j-1][k]-sum[i-1][j-1][k]+((p[i][j]>=k)?p[i][j]:0);
}
while(m--)
{
x1=gi(),y1=gi(),x2=gi(),y2=gi(),h=gi();
int l=0,r=mx,ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid)){ans=mid;l=mid+1;}
else r=mid-1;
}
if(ans!=-1)printf("%d\n",number(x1,y1,x2,y2,h,ans));
else ERROR;
}
}
struct node
{
int ls,rs,sum,siz;
}t[NN*5];
int a[NN],n,cnt,rt[NN];
void modify(int x,int &o,int l,int r,int pos)
{
o=++cnt;t[o]=t[x];
t[o].sum+=pos;t[o].siz++;if(l==r)return;
int mid=(l+r)>>1;
if(pos<=mid)modify(t[x].ls,t[o].ls,l,mid,pos);
else modify(t[x].rs,t[o].rs,mid+1,r,pos);
}
int query(int x,int y,int l,int r,int sum)
{
if(l==r)return sum/l+(sum%l>0);
int mid=(l+r)>>1,S=(t[t[y].rs].sum-t[t[x].rs].sum);
if(S>=sum)return query(t[x].rs,t[y].rs,mid+1,r,sum);
else return query(t[x].ls,t[y].ls,l,mid,sum-S)+t[t[y].rs].siz-t[t[x].rs].siz;
}
void work2()
{
n=c;
for(int i=1;i<=n;i++)a[i]=gi();
for(int i=1;i<=n;i++)
modify(rt[i-1],rt[i],1,1000,a[i]),a[i]+=a[i-1];
while(m--)
{
x1=gi(),y1=gi(),x2=gi(),y2=gi();h=gi();
if(a[y2]-a[y1-1]<h){ERROR;}
else printf("%d\n",query(rt[y1-1],rt[y2],1,1000,h));
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif
r=gi();c=gi();m=gi();
if(r!=1 || c<=200)work1();
else work2();
return 0;
}

posted @ 2019-09-10 22:14  QwQGJH  阅读(121)  评论(0编辑  收藏  举报