# 1926: [Sdoi2010]粟粟的书架 主席树+二分答案

#include<iostream>
#include<cstdio>
#define ll long long
#define M 10000005
#define N 205
using namespace std;
int n,m,cnt,Q;
int sum[M];
int size[M],ls[M],rs[M];
int s[1005][N][N];
int ss[1005][N][N];
int a[500005],b[N][N],v[1005];
int root[500005];
inline int read()
{
int a=0,f=1; char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
return a*f;
}
void update(int l,int r,int x,int &y,int val)
{
y=++cnt;
ls[y]=ls[x]; rs[y]=rs[x]; sum[y]=sum[x]+val; size[y]=size[x]+1;
if (l==r) return;
int mid=l+r>>1;
if (val<=mid) update(l,mid,ls[x],ls[y],val);
else update(mid+1,r,rs[x],rs[y],val);
}
int find(int l,int r,int x,int y,int val)
{
int mid=l+r>>1;
if (l==r) return sum[y]-sum[x];
if (val<=mid) return find(l,mid,ls[x],ls[y],val)+sum[rs[y]]-sum[rs[x]];
else return find(mid+1,r,rs[x],rs[y],val);
}
int get_size(int l,int r,int x,int y,int val)
{
int mid=l+r>>1;
if (l==r) return size[y]-size[x];
if (val<=mid) return get_size(l,mid,ls[x],ls[y],val)+size[rs[y]]-size[rs[x]];
else return get_size(mid+1,r,rs[x],rs[y],val);
}
inline void solve1()
{
for (int i=1;i<=m;i++) a[i]=read(),v[a[i]]++;
for (int i=1;i<=m;i++)
update(1,1000,root[i-1],root[i],a[i]);
while (Q--)
{
int x,l,r,H;
x=read(); l=read(); x=read(); r=read(); H=read();
int L=1,R=1000,ans=-1,last=0;
while (L<=R)
{
int mid=L+R>>1;
int tmp=find(1,1000,root[l-1],root[r],mid);
if (tmp>=H) last=tmp,ans=mid,L=mid+1; else R=mid-1;
}
if (ans==-1) {puts("Poor QLW"); continue;}
int now=get_size(1,1000,root[l-1],root[r],ans),rest=v[ans];
while (rest&&last-ans>=H) rest--,now--,last-=ans;
printf("%d\n",now);
}
}
int calc(int mid,int x1,int y1,int x2,int y2)
{
return ss[mid][x2][y2]+ss[mid][x1-1][y1-1]-ss[mid][x1-1][y2]-ss[mid][x2][y1-1];
}
int get_size1(int mid,int x1,int y1,int x2,int y2)
{
return s[mid][x2][y2]+s[mid][x1-1][y1-1]-s[mid][x1-1][y2]-s[mid][x2][y1-1];
}
inline void solve2()
{
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
b[i][j]=read(),v[b[i][j]]++;
for (int k=1;k<=1000;k++)
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
s[k][i][j]=s[k][i-1][j]+s[k][i][j-1]-s[k][i-1][j-1]+(b[i][j]==k?1:0),ss[k][i][j]=s[k][i][j]*k;
for (int k=1000;k;k--)
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
ss[k][i][j]+=ss[k+1][i][j],s[k][i][j]+=s[k+1][i][j];
while (Q--)
{
int x1=read(),y1=read(),x2=read(),y2=read(),H=read();
int L=1,R=1000,ans=-1,last=0;
while (L<=R)
{
int mid=L+R>>1;
int tmp=calc(mid,x1,y1,x2,y2);
if (tmp>=H) last=tmp,ans=mid,L=mid+1; else R=mid-1;
}
if (ans==-1) {puts("Poor QLW"); continue;}
int now=get_size1(ans,x1,y1,x2,y2),rest=v[ans];
while (rest&&last-ans>=H) rest--,now--,last-=ans;
printf("%d\n",now);
}
}
int main()
{
n=read(); m=read(); Q=read();
if (n==1) solve1(); else solve2();
return 0;
}
07-10 99

02-07 2万+
02-19 63
01-07 158
10-27 320
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客