結果

問題 No.1332 Range Nearest Query
ユーザー hir355hir355
提出日時 2021-01-08 23:15:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 507 ms / 2,500 ms
コード長 2,327 bytes
コンパイル時間 2,334 ms
コンパイル使用メモリ 188,848 KB
実行使用メモリ 29,636 KB
最終ジャッジ日時 2023-08-10 12:04:30
合計ジャッジ時間 23,709 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 465 ms
20,468 KB
testcase_04 AC 462 ms
20,512 KB
testcase_05 AC 460 ms
20,540 KB
testcase_06 AC 399 ms
29,552 KB
testcase_07 AC 399 ms
29,448 KB
testcase_08 AC 399 ms
29,592 KB
testcase_09 AC 398 ms
29,580 KB
testcase_10 AC 394 ms
29,404 KB
testcase_11 AC 398 ms
29,580 KB
testcase_12 AC 399 ms
29,584 KB
testcase_13 AC 396 ms
29,432 KB
testcase_14 AC 400 ms
29,456 KB
testcase_15 AC 399 ms
29,396 KB
testcase_16 AC 497 ms
29,564 KB
testcase_17 AC 494 ms
29,516 KB
testcase_18 AC 496 ms
29,436 KB
testcase_19 AC 493 ms
29,636 KB
testcase_20 AC 496 ms
29,464 KB
testcase_21 AC 502 ms
29,496 KB
testcase_22 AC 498 ms
29,520 KB
testcase_23 AC 507 ms
29,496 KB
testcase_24 AC 494 ms
29,380 KB
testcase_25 AC 497 ms
29,520 KB
testcase_26 AC 396 ms
29,464 KB
testcase_27 AC 348 ms
29,496 KB
testcase_28 AC 203 ms
6,220 KB
testcase_29 AC 199 ms
6,228 KB
testcase_30 AC 199 ms
6,104 KB
testcase_31 AC 195 ms
6,372 KB
testcase_32 AC 197 ms
6,084 KB
testcase_33 AC 196 ms
6,236 KB
testcase_34 AC 196 ms
6,184 KB
testcase_35 AC 200 ms
6,116 KB
testcase_36 AC 199 ms
6,160 KB
testcase_37 AC 194 ms
6,152 KB
testcase_38 AC 375 ms
18,164 KB
testcase_39 AC 258 ms
7,192 KB
testcase_40 AC 484 ms
29,376 KB
testcase_41 AC 295 ms
9,368 KB
testcase_42 AC 367 ms
17,852 KB
testcase_43 AC 326 ms
12,492 KB
testcase_44 AC 428 ms
19,740 KB
testcase_45 AC 410 ms
19,016 KB
testcase_46 AC 359 ms
13,260 KB
testcase_47 AC 402 ms
18,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/segtree>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
#define INF_LL 100000000000000000LL
#define INF 2000000000
#define MOD 1000000007
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for(int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define int long long
// typedef float double;
// typedef priority_queue prique;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}

ll modpow(ll a, ll b, ll m) {
    if(b == 0)
        return 1;
    ll t = modpow(a, b / 2, m);
    if(b & 1) {
        return (t * t % m) * a % m;
    } else {
        return t * t % m;
    }
}

struct edge {
    int to;
    ll cost;
    edge(int t, ll c) { to = t, cost = c; }
};
typedef vector<vector<edge>> graph;

// using mint = modint998244353;

int minop(int x, int y) {return min(x, y);}
int maxop(int x, int y) {return max(x, y);}
int mine() {return INF;}
int maxe() {return -INF;}

signed main() {
    int n, q;
    cin >> n;
    vector<pair<int, int>> a(n);
    rep(i, n) {cin >> a[i].first;a[i].second = i;}
    segtree<int, maxop, maxe> seg1(n);
    segtree<int, minop, mine> seg2(n);
    rep(i, n) seg2.set(i, a[i].first);
    sort(all(a));
    cin >> q;
    vector<tuple<int, int, int, int>> query(q);
    rep(i, q){
        int l, r, x;
        cin >> l >> r >> x;
        query[i] = {x, l, r, i};
    }
    sort(all(query));
    int idx = 0;
    rep(i, q){
        int x, l, r, g;
        tie(x, l, r, g) = query[i];
        while(idx < n && x > a[idx].first){
            seg1.set(a[idx].second, seg2.get(a[idx].second));
            seg2.set(a[idx].second, mine());
            idx++;
        }
        get<0>(query[i]) = min(seg2.prod(l - 1, r) - x, x - seg1.prod(l - 1, r));
    }
    sort(begin(query), end(query), [](const auto &x, const auto &y){return get<3>(x) < get<3>(y);});
    rep(i, q){
        cout << get<0>(query[i]) << endl;
    }
    return 0;
}
0