結果
| 問題 |
No.1332 Range Nearest Query
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2021-01-08 23:18:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,902 bytes |
| コンパイル時間 | 2,211 ms |
| コンパイル使用メモリ | 207,940 KB |
| 最終ジャッジ日時 | 2025-01-17 14:33:05 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 TLE * 21 |
ソースコード
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
template<class T>
void chmin(T& a, const T& b) {
a = min<T>(a, b);
}
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
int BSIZE;
struct Q {
int i;
int l, r, x;
//bool operator<(Q& y) {
// int bx = l / BSIZE, by = y.l / BSIZE;
// return (bx < by) || (bx == by && r < y.r);
//}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
BSIZE = sqrt(n);
VI x(n);
rep(i, n) cin >> x[i];
int q;
cin >> q;
vector<Q> qs(q);
rep(i, q) {
qs[i].i = i;
cin >> qs[i].l >> qs[i].r >> qs[i].x;
qs[i].l--;
}
sort(all(qs), [](Q x, Q y) {
int bx = x.l / BSIZE, by = y.l / BSIZE;
return (bx < by) || (bx == by && x.r < y.r);
});
int cur_b = -1, blast = -1;
VI ans(q);
set<int> s;
int p = -1;
rep(i, q) {
int l = qs[i].l, r = qs[i].r, z = qs[i].x;
int b = l / BSIZE;
if (b != cur_b) {
cur_b = b;
s.clear();
blast = BSIZE * (b + 1);
p = blast;
}
while(p < r) s.insert(x[p++]);
auto it = s.lower_bound(z);
int tans = 1001001001;
if (it != s.end()) {
chmin(tans, *it - z);
}
if (it != s.begin()) {
it--;
chmin(tans, z - *it);
}
for(int j = min({blast, n, r}) - 1; j >= l; --j) {
chmin(tans, abs(x[j] - z));
}
ans[qs[i].i] = tans;
}
rep(i, q) cout << ans[i] << '\n';
}
Kude