1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cstdlib>
6 #include<cmath>
7 using namespace std;
8
9 const int ONE=205;
10 const int TWO=5500005;
11 const int INF=1005;
12
13 int n,m,T;
14 int x,Need,cnt;
15 int res_num,res_value;
16 int a[ONE][ONE];
17 int X1,X2,Y1,Y2;
18
19 struct power
20 {
21 int root;
22 int left,right;
23 int value,num;
24 }Node[TWO];
25
26 struct point
27 {
28 int num;
29 int value;
30 }tal[ONE][ONE][INF];
31
32
33 int get()
34 {
35 int res,Q=1; char c;
36 while( (c=getchar())<48 || c>57)
37 if(c==‘-‘)Q=-1;
38 if(Q) res=c-48;
39 while((c=getchar())>=48 && c<=57)
40 res=res*10+c-48;
41 return res*Q;
42 }
43
44 void Update(int &x,int y,int l,int r,int Val)
45 {
46 x=++cnt;
47 Node[x].value=Node[y].value+Val;
48 Node[x].num=Node[y].num+1;
49 Node[x].left=Node[y].left;
50 Node[x].right=Node[y].right;
51 if(l==r) return;
52 int mid=(l+r)/2;
53 if(Val<=mid) Update(Node[x].left,Node[y].left,l,mid,Val);
54 if(mid+1<=Val) Update(Node[x].right,Node[y].right,mid+1,r,Val);
55 }
56
57 void Query(int x,int y,int l,int r,int Need)
58 {
59 if(l==r)
60 {
61 if(Need && l)
62 {
63 int num=Need/l;
64 res_num+=num;
65 Need-=num*l;
66 if(Need>0) res_num++,Need-=l;
67 }
68 res_value=Need;
69 return;
70 }
71
72 int mid=(l+r)/2;
73 int value=Node[ Node[y].right ].value-Node[ Node[x].right ].value;
74 if(value > Need)
75 Query(Node[x].right,Node[y].right,mid+1,r,Need);
76 else
77 {
78 res_num+=Node[ Node[y].right ].num-Node[ Node[x].right ].num;
79 Query(Node[x].left,Node[y].left,l,mid,Need-value);
80 }
81 }
82
83
84 int Getvalue(int Val)
85 {
86 return tal[X2][Y2][Val].value + tal[X1-1][Y1-1][Val].value - tal[X1-1][Y2][Val].value - tal[X2][Y1-1][Val].value;
87 }
88
89 int Getnum(int Val)
90 {
91 return tal[X2][Y2][Val].num + tal[X1-1][Y1-1][Val].num - tal[X1-1][Y2][Val].num - tal[X2][Y1-1][Val].num;
92 }
93
94 void Deal(int ans)
95 {
96 res_num=Getnum(ans+1);
97 res_value=Getvalue(ans+1);
98 Need-=res_value;
99 if(Need>=0)
100 {
101 int num=Need/ans;
102 res_num+=num;
103 Need-=num*ans;
104 if(Need>0) res_num++,Need-=ans;
105 }
106 }
107
108
109 void PartOne()
110 {
111 for(int i=1;i<=n;i++)
112 for(int j=1;j<=m;j++)
113 {
114 a[i][j]=get();
115 }
116
117 for(int k=0;k<=1000;k++)
118 {
119 for(int i=1;i<=n;i++)
120 {
121 for(int j=1;j<=m;j++)
122 {
123 tal[i][j][k].value=tal[i][j-1][k].value;
124 tal[i][j][k].num=tal[i][j-1][k].num;
125 if(a[i][j]>=k) tal[i][j][k].value+=a[i][j],tal[i][j][k].num++;
126 }
127
128 for(int j=1;j<=m;j++)
129 {
130 tal[i][j][k].value+=tal[i-1][j][k].value;
131 tal[i][j][k].num+=tal[i-1][j][k].num;
132 }
133 }
134 }
135
136
137 while(T--)
138 {
139 Need=0;
140 X1=get(); Y1=get(); X2=get(); Y2=get(); Need=get();
141 if(Getvalue(0)<Need)
142 {
143 printf("Poor QLW\n");
144 continue;
145 }
146 int l=0,r=1001;
147
148 int ans=1;
149 while(l<=r)
150 {
151 int mid=(l+r)/2;
152 if(Getvalue(mid)>Need)
153 {
154 ans=mid;
155 l=mid+1;
156 }
157 else r=mid-1;
158 }
159
160 if(!ans) ans=1;
161 res_num=res_value=0;
162 Deal(ans);
163 printf("%d\n",res_num);
164 }
165 }
166
167 void PartTwo()
168 {
169 for(int i=1;i<=m;i++)
170 {
171 x=get();
172 Update(Node[i].root,Node[i-1].root,0,INF,x);
173 }
174
175 int x1,x2,y1,y2;
176 while(T--)
177 {
178 x1=get(); y1=get(); x2=get(); y2=get(); Need=get();
179 int l=0,r=1000;
180
181 res_num=res_value=0;
182 Query(Node[y1-1].root,Node[y2].root,0,INF,Need);
183 if(res_value>0) printf("Poor QLW\n");
184 else
185 printf("%d\n",res_num);
186 }
187 }
188
189 int main()
190 {
191 n=get(); m=get(); T=get();
192 if(n!=1) PartOne();
193 else PartTwo();
194 }