#include <bits/stdc++.h>
using namespace std;
#define int long long
#define NAME "A"
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define MASK(x) (1ll << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
const int inf = 1e18;
int n, m, q, L;
vector<vector<int>> a;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
bool inside(int x, int y) {
return 1 <= x && x <= n && 1 <= y && y <= m;
}
void input() {
cin >> n >> m >> L;
a.assign(n + 1, vector<int>(m + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
}
namespace subtask1 {
vector<vector<pair<int, int>>> mx;
vector<vector<vector<vector<bool>>>> d;
bool check() {
return n <= 3000;
}
void bfs(vector<vector<bool>> &d, int u, int v) {
queue<pair<int, int>> q;
d[u][v] = 1;
q.push({u, v});
while (q.size()) {
int x = q.front().fi;
int y = q.front().se;
if (a[mx[u][v].fi][mx[u][v].se] < a[x][y]) {
mx[u][v] = {x, y};
}
q.pop();
for (int i = 0; i < 4; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (inside(xx, yy) && d[xx][yy] == 0 && a[u][v] + L >= a[xx][yy]) {
d[xx][yy] = 1;
q.push({xx, yy});
}
}
}
}
void solve() {
d.assign(n + 1, vector<vector<vector<bool>>>(m + 1));
mx.assign(n + 1, vector<pair<int, int>>(m + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
d[i][j].assign(n + 1, vector<bool>(m + 1));
bfs(d[i][j], i, j);
}
}
cin >> q;
while (q--) {
int x, y, xx, yy;
cin >> x >> y >> xx >> yy;
int ans = 1;
while (d[x][y][xx][yy] == 0) {
ans++;
if (make_pair(x, y) == mx[x][y]) {
ans = -1;
break;
}
int u = mx[x][y].fi, v = mx[x][y].se;
x = u, y = v;
}
cout << ans << "\n";
}
}
}
namespace subtask2 {
vector<vector<int>> ans;
void process() {
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;
pq.push({a[1][1], 1, 1});
int cur = a[1][1];
int mx = a[1][1];
int res = 0;
while (pq.size()) {
int val, x, y;
tie(val, x, y) = pq.top();
pq.pop();
if (ans[x][y] != inf) continue;
if (val <= cur + L) {
ans[x][y] = res;
}
else if (val <= mx + L) {
res++;
cur = mx;
ans[x][y] = res;
}
else continue;
mx = max(mx, val);
for (int i = 0; i < 4; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (inside(xx, yy)) {
pq.push({a[xx][yy], xx, yy});
}
}
}
}
void solve() {
ans.assign(n + 1, vector<int>(m + 1, inf));
process();
cin >> q;
while (q--) {
int x, y, xx, yy;
cin >> x >> y >> xx >> yy;
if (ans[xx][yy] == inf) {
ans[xx][yy] = -1;
}
else {
ans[xx][yy]++;
}
cout << ans[xx][yy] << "\n";
}
}
};
signed main() {
if (fopen(NAME".INP", "r")) {
freopen(NAME".INP", "r", stdin);
freopen(NAME".OUT", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
input();
if (subtask1::check()) return subtask1::solve(), 0;
return subtask2::solve(), 0;
return 0;
}
Compilation message (stderr)
travel.cpp: In function 'int main()':
travel.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(NAME".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen(NAME".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |