whdywjd @ 2023-04-15 12:49:11
// LUOGU_RID: 100000000
#define Happy_New_Year 2023
#define NO_pts 45
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define lf double
#define eps 1e-8
#define inf (1ll << 60)
#define pi 3.1415926535897932
#define _pb push_back
#define _mp make_pair
#define _1 first
#define _2 second
#define MAX_N 614
#define MAX_M 62342
#define MAX_TN 654023
using namespace std;
ll read(){ll x=0;char c=0,v=1;while(c<'0'||c>'9'){c=getchar();if(c=='-')v=-1;}while(!(c<'0'||c>'9')){x=(x<<1)+(x<<3)+c-'0';c=getchar();}return x*v;}void write(ll p){if(p<0){putchar('-');write(-p);return;}if(p<10){putchar(p+'0');return;}write(p/10);putchar((p%10)+'0');}
lf Read(){char c=0,v=1;lf x=0,u=1;while(48>c||57<c){c=getchar();if(c==45)v=-1;}while(47<c&&58>c){x=10.0*x+c-48.0;c=getchar();}if(c!='E'&&c!='e'&&c!='.')return v*x;else if(c=='.'){c=getchar();while(48>c||57<c)c=getchar();while(47<c&&58>c){x=10.0*x+c-48.0;c=getchar();u*=10;}}if(c!='E'&&c!='e')return v*x/u;ll y=0;lf e=1;char w=1;while(48>c||57<c){c=getchar();if(c==45)w=0;}while(47<c&&58>c){y=10*y+c-48;c=getchar();}if(w)while(y--)e*=10.0;else while(y--)e/=10.0;return v*x*e/u;}void Write(lf x, int t){if(-eps < x && x < eps){putchar('0');return;}if(x < -eps){putchar('-');Write(-x, t);return;}ll c = 1;while(t--)c *= 10;x *= c;ll p = (ll)(x + 0.5);write(p / c);if(c != 1){putchar('.');write(p % c);}}
char gtc(){char c;do c = getchar();while(c < 33);return c;}
struct mfixed{int c;mfixed(int x) { c = x; }mfixed operator << (int p){write(p);return *this;}mfixed operator << (ll p){write(p);return *this;}mfixed operator << (char c){putchar(c);return *this;}mfixed operator << (const char* s){printf("%s", s);return *this;}mfixed operator << (lf x){Write(x, c);return *this;}mfixed operator << (mfixed b){return b;}mfixed operator >> (int& x){x = read();return *this;}mfixed operator >> (ll& x){x = read();return *this;}mfixed operator >> (char& x){x = gtc();return *this;}mfixed operator >> (char* s){scanf("%s", s);return *this;}mfixed operator >> (lf& x){x = Read();return *this;}} myio(6);
template<size_t N, int s>struct jnode{int n, m;pair<ll, int> jv[N];int ja[N];ll jo[N];jnode() { n = s; }void insert(ll x) { jv[n] = _mp(x, n), n++; }void rebuild(){sort(jv + s, jv + n);for(int i = s; i < n; i++)if(i == s || jv[i]._1 != jv[i - 1]._1)jo[++m] = jv[i]._1, ja[jv[i]._2] = m;else ja[jv[i]._2] = m;}int operator [] (int x) { return ja[x]; }ll operator () (int x) { return jo[ja[x]]; }ll decode(int id) { return jo[id]; }void upper_bound(ll x) { return upper_bound(jo + 1, jo + m + 1, x) - jo; }void lower_bound(ll x) { return lower_bound(jo + 1, jo + m + 1, x) - jo; }};
template<size_t N>
struct tav
{
ll t[N][N];
vector<pair<int, int> > vec;
void update(int x, int y, ll k)
{
for(int i = x; i < N; i += (i & -i))
for(int j = y; j < N; j += (j & -j))
t[i][j] += k, vec._pb(_mp(i, j));
}
ll query(int x, int y)
{
if(!x || !y)
return 0;
ll ans = 0;
for(int i = x; i; i -= (i & -i))
for(int j = y; j; j -= (j & -j))
ans += t[i][j];
return ans;
}
ll query(int x1, int y1, int x2, int y2)
{
return query(x2, y2) - query(x1 - 1, x2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1);
}
void clear()
{
for(auto i: vec)
t[i._1][i._2] = 0;
vec.clear();
}
};
tav<MAX_N> t;
struct node
{
char c; //1:insert 2:query
char f; //0:l 1:r
int id;
int x1, y1, x2, y2;
ll k;
ll ans;
node() { ; }
node(char _c, int _id, int _x1, int _y1, int _x2, int _y2, ll _k) :
c(_c), id(_id), x1(_x1), y1(_y1), x2(_x2), y2(_y2), k(_k) { ; }
void pDown(ll mid)
{
if(c == 1)
{
if(k <= mid)
f = 0, t.update(x1, y1, 1);
else
f = 1;
}
else
{
int a = t.query(x1, y1, x2, y2);
if(a < k)
f = 1, k -= a;
else
f = 0;
}
}
bool operator < (node b) { return k < b.k; }
} qs[MAX_TN];
vector<node> f0, f1;
void wzc(ll l, ll r, int ql, int qr)
{
if(ql > qr)
return;
if(l == r)
{
for(int i = ql; i <= qr; i++)
qs[i].ans = l;
return;
}
ll mid = (l + r) >> 1;
for(int i = ql; i <= qr; i++)
qs[i].pDown(mid);
t.clear();
for(int i = ql; i <= qr; i++)
if(qs[i].f)
f1._pb(qs[i]);
else
f0._pb(qs[i]);
int v = ql - 1;
for(auto i: f0)
qs[++v] = i;
f0.clear();
int qmid = v;
for(auto i: f1)
qs[++v] = i;
f1.clear();
wzc(l, mid, ql, qmid);
wzc(mid + 1, r, qmid + 1, qr);
}
int n, m;
int qv;
ll ans[MAX_TN];
void MAIN()
{
n = read();
m = read();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
qs[++qv] = node(1, 0, i, j, i, j, read());
sort(qs + 1, qs + qv + 1);
for(int i = 1; i <= m; i++)
{
int x1 = read();
int y1 = read();
int x2 = read();
int y2 = read();
qs[++qv] = node(2, i, x1, y1, x2, y2, read());
}
wzc(0, 1e9, 1, qv);
for(int i = 1; i <= qv; i++)
ans[qs[i].id] = qs[i].ans;
for(int i = 1; i <= m; i++)
printf("%lld\n", ans[i]);
}
void CLEAR()
{
;
}
void EXPERIMENT()
{
;
}
int main()
{
//freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
EXPERIMENT();
int T = 1;
while(T--)
{
MAIN();
CLEAR();
}
return 0;
}