結果
| 問題 |
No.2786 RMQ on Grid Path
|
| コンテスト | |
| ユーザー |
applejam
|
| 提出日時 | 2025-09-26 01:44:33 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,971 bytes |
| コンパイル時間 | 5,791 ms |
| コンパイル使用メモリ | 335,908 KB |
| 実行使用メモリ | 512,424 KB |
| 最終ジャッジ日時 | 2025-09-26 01:45:58 |
| 合計ジャッジ時間 | 72,916 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 16 MLE * 19 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vmi = vector<mint>;
using vvmi = vector<vmi>;
using vvvmi = vector<vvmi>;
#define all(a) (a).begin(), (a).end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
using p = pair<int, int>;
int dh[4] = {0, 0, -1, 1};
int dw[4] = {-1, 1, 0, 0};
int main(){
int h, w; cin >> h >> w;
vvi grid(h, vi(w)); rep(i, h)rep(j, w)cin >> grid[i][j];
vector<p> vp(h*w);
rep(i, h)rep(j, w)vp[i*w+j] = {grid[i][j], i*w+j};
sort(all(vp));
int n = h*w;
int b = (int)sqrt(n);
dsu d(n);
vi pos; for(int i=0; i<n; i+=b)pos.push_back(i);
pos.push_back(n);
int itr = 1;
vector<dsu> vd; vd.push_back(d);
int now = 0;
rep(i, n){
int u = vp[i].second;
now = vp[i].first;
int x = u/w, y = u%w;
rep(j, 4){
int nx = x + dh[j], ny = y + dw[j];
if(nx < 0 || nx >= h || ny < 0 || ny >= w)continue;
if(now >= grid[nx][ny]){
d.merge(u, nx*w + ny);
}
}
if(i >= pos[itr]){
vd.push_back(d);
itr++;
}
}
vd.push_back(d);
int len = vd.size();
vvi part(len);
int Q; cin >> Q;
vvi query(Q, vi(4));
rep(i, Q){
rep(j, 4)cin >> query[i][j];
rep(j, 4)query[i][j]--;
int a = query[i][0]*w + query[i][1], b = query[i][2]*w + query[i][3];
int l = 0, r = len-1;
int mid;
while(r - l > 1){
mid = (l+r)/2;
if(vd[mid].same(a, b)){
r = mid;
}else{
l = mid;
}
}
part[r].push_back(i);
}
vi ans(Q, 1e9);
rep2(i, 1, len){
if(part[i].size() == 0)continue;
dsu d = vd[i-1];
int now = 0;
rep2(j, pos[i-1], min(pos[i]+1, n)){
int u = vp[j].second;
now = vp[j].first;
int x = u/w, y = u%w;
rep(k, 4){
int nx = x + dh[k], ny = y + dw[k];
if(nx < 0 || nx >= h || ny < 0 || ny >= w)continue;
if(now >= grid[nx][ny]){
d.merge(u, nx*w + ny);
}
}
for(auto k : part[i]){
int a = query[k][0]*w + query[k][1], b = query[k][2]*w + query[k][3];
if(d.same(a, b)){
ans[k] = min(ans[k], now);
}
}
}
}
rep(i, Q)cout << ans[i] << endl;
return 0;
}
applejam