結果
問題 | No.1332 Range Nearest Query |
ユーザー |
![]() |
提出日時 | 2024-08-15 06:28:12 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 325 ms / 2,500 ms |
コード長 | 1,538 bytes |
コンパイル時間 | 3,042 ms |
コンパイル使用メモリ | 258,256 KB |
実行使用メモリ | 67,068 KB |
最終ジャッジ日時 | 2024-08-15 06:28:29 |
合計ジャッジ時間 | 16,693 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 48 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/segtree>using namespace std;using namespace atcoder;#define int long longstruct Query {int value, left, right, index;bool operator<(const Query& other) const {if (value != other.value)return value < other.value;return left < other.left;}};int op(int a, int b) {return max(a, b);}int inf=1ll<<60;int e() {return -inf; // effectively negative infinity}signed main() {ios::sync_with_stdio(false);cin.tie(0);int N;cin >> N;vector<int> X(N);for (int i = 0; i < N; ++i) {cin >> X[i];}int Q;cin >> Q;vector<Query> query0, query1;vector<int> ans(Q, inf);for (int i = 0; i < N; ++i) {query0.push_back({X[i], -1, -1, i});query1.push_back({-X[i], -1, -1, i});}for (int i = 0; i < Q; ++i) {int l, r, x;cin >> l >> r >> x;--l;query0.push_back({x, l, r, i});query1.push_back({-x, l, r, i});}sort(query0.begin(), query0.end());sort(query1.begin(), query1.end());for (auto& query : {query0, query1}) {segtree<int, op, e> seg(N);for (auto& q : query) {if (q.left == -1) {seg.set(q.index, q.value);} else {ans[q.index] = min(ans[q.index], q.value - seg.prod(q.left, q.right));}}}for (int i = 0; i < Q; ++i) {cout << ans[i] << "\n";}return 0;}