結果

問題 No.1332 Range Nearest Query
ユーザー definedefine
提出日時 2021-01-09 11:41:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 394 ms / 2,500 ms
コード長 4,753 bytes
コンパイル時間 2,872 ms
コンパイル使用メモリ 214,616 KB
実行使用メモリ 15,980 KB
最終ジャッジ日時 2024-04-28 19:03:40
合計ジャッジ時間 17,862 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 394 ms
11,500 KB
testcase_04 AC 346 ms
11,500 KB
testcase_05 AC 337 ms
11,500 KB
testcase_06 AC 155 ms
15,848 KB
testcase_07 AC 154 ms
15,852 KB
testcase_08 AC 157 ms
15,848 KB
testcase_09 AC 155 ms
15,836 KB
testcase_10 AC 156 ms
15,852 KB
testcase_11 AC 156 ms
15,852 KB
testcase_12 AC 153 ms
15,980 KB
testcase_13 AC 155 ms
15,848 KB
testcase_14 AC 154 ms
15,848 KB
testcase_15 AC 155 ms
15,980 KB
testcase_16 AC 380 ms
15,852 KB
testcase_17 AC 385 ms
15,852 KB
testcase_18 AC 375 ms
15,852 KB
testcase_19 AC 384 ms
15,852 KB
testcase_20 AC 380 ms
15,980 KB
testcase_21 AC 370 ms
15,980 KB
testcase_22 AC 380 ms
15,848 KB
testcase_23 AC 377 ms
15,852 KB
testcase_24 AC 372 ms
15,848 KB
testcase_25 AC 391 ms
15,852 KB
testcase_26 AC 189 ms
15,852 KB
testcase_27 AC 172 ms
15,856 KB
testcase_28 AC 36 ms
5,376 KB
testcase_29 AC 38 ms
5,504 KB
testcase_30 AC 38 ms
5,504 KB
testcase_31 AC 31 ms
5,632 KB
testcase_32 AC 41 ms
5,504 KB
testcase_33 AC 40 ms
5,504 KB
testcase_34 AC 34 ms
5,376 KB
testcase_35 AC 36 ms
5,376 KB
testcase_36 AC 36 ms
5,504 KB
testcase_37 AC 38 ms
5,504 KB
testcase_38 AC 236 ms
10,732 KB
testcase_39 AC 98 ms
5,664 KB
testcase_40 AC 392 ms
15,852 KB
testcase_41 AC 140 ms
6,784 KB
testcase_42 AC 226 ms
10,604 KB
testcase_43 AC 169 ms
8,044 KB
testcase_44 AC 297 ms
11,248 KB
testcase_45 AC 285 ms
10,988 KB
testcase_46 AC 210 ms
8,428 KB
testcase_47 AC 262 ms
10,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
#define len(s) (ll) s.size()

template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
constexpr ll inf = 3e18;
#line 3 "/home/defineprogram/Desktop/Library/structure/SegmentTree.cpp"

template <typename Monoid,
          typename OperatorMonoid,
          Monoid (*f)(Monoid, Monoid, int),
          Monoid (*g)(Monoid, OperatorMonoid, int),
          OperatorMonoid (*h)(OperatorMonoid, OperatorMonoid, int)>
struct Segtree {
    int size = 1;

   private:
    vector<Monoid> dat;
    vector<OperatorMonoid> lazy;
    Monoid M;
    OperatorMonoid OM;

   public:
    void eval(int k, int l, int r) {
        if (lazy[k] != OM) {
            dat[k] = g(dat[k], lazy[k], r - l);
            if (r - l > 1) {
                lazy[(k << 1) + 1] = h(lazy[(k << 1) + 1], lazy[k], r - l);
                lazy[(k << 1) + 2] = h(lazy[(k << 1) + 2], lazy[k], r - l);
            }
            lazy[k] = OM;
        }
    }
    void update(int a, int b, OperatorMonoid M, int k = 0, int l = 0, int r = -1) {
        if (r == -1) r = size;
        eval(k, l, r);
        if (r <= a || b <= l) return;
        if (a <= l && r <= b) {
            lazy[k] = h(lazy[k], M, r - l);
            eval(k, l, r);
            return;
        }
        update(a, b, M, (k << 1) + 1, l, (l + r) >> 1);
        update(a, b, M, (k << 1) + 2, (l + r) >> 1, r);
        dat[k] = f(dat[(k << 1) + 1], dat[(k << 1) + 2], r - l);
    }
    Monoid query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r == -1) r = size;
        eval(k, l, r);
        if (r <= a || b <= l) return M;
        if (a <= l && r <= b) return dat[k];
        Monoid lv = query(a, b, (k << 1) + 1, l, (l + r) >> 1);
        Monoid rv = query(a, b, (k << 1) + 2, (l + r) >> 1, r);
        return f(lv, rv, r - l);
    }
    template <class C>
    int minLeft(int a, int b, C &check, Monoid x, int k = 0, int l = 0, int r = -1) {
        if (r == -1) r = size;
        eval(k, l, r);
        if (r <= a || b <= l || !check(dat[k], x)) return -1;
        if (r - l == 1) return l;
        int lv = minLeft(a, b, check, x, (k << 1) + 1, l, (l + r) >> 1);
        if (lv != -1) return lv;
        return minLeft(a, b, check, x, (k << 1) + 2, (l + r) >> 1, r);
    }
    template <class C>
    int maxRight(int a, int b, C &check, Monoid x, int k = 0, int l = 0, int r = -1) {
        if (r == -1) r = size;
        eval(k, l, r);
        if (r <= a || b <= l || !check(dat[k], x)) return -1;
        if (r - l == 1) return l;
        int rv = maxRight(a, b, check, x, (k << 1) + 2, (l + r) >> 1, r);
        if (rv != -1) return rv;
        return maxRight(a, b, check, x, (k << 1) + 1, l, (l + r) >> 1);
    }
    Segtree(int x, Monoid M, OperatorMonoid OM)
        : M(M), OM(OM) {
        while (size < x) size <<= 1;
        dat.resize((size << 1) - 1, M);
        lazy.resize((size << 1) - 1, OM);
    }
};

/*
@brief Lazy Segment Tree
@docs docs/SegmentTree.md
*/
#line 3 "main.cpp"

int N,X[1<<19];
auto f=[](int a,int b,int sz){return max(a,b);};
auto g=[](int a,int b,int sz){return b;};
auto check=[](int a,int b){return a>=b;};
struct S{
    int l,r,x,idx;
};
int Q;
S query[1<<17];
int ans[1<<17];
int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin>>N;
    vector<int>xx;
    rep(i,N){
        cin>>X[i];
        xx.push_back(X[i]);
    }
    sort(all(xx));xx.erase(unique(all(xx)),xx.end());
    rep(i,N){
        X[i]=lower_bound(all(xx),X[i])-xx.begin();
    }
    cin>>Q;
    rep(i,Q){
        cin>>query[i].l>>query[i].r>>query[i].x;query[i].idx=i;
        query[i].l--;
    }
    sort(query,query+Q,[](S a,S b){return a.r<b.r;});
    Segtree<int,int,f,g,g>segtree(N,-1,-1);
    int cur=0;
    rep(i,Q){
        while(cur<query[i].r){
            segtree.update(X[cur],X[cur]+1,cur);
            cur++;
        }
        int p=lower_bound(all(xx),query[i].x)-xx.begin();
        int r=segtree.minLeft(p,N,check,query[i].l);
        int l=segtree.maxRight(0,p,check,query[i].l);
        ans[query[i].idx]=1e9;
        if(l!=-1){
            ans[query[i].idx]=query[i].x-xx[l];
        }
        if(r!=-1){
            chmin(ans[query[i].idx],xx[r]-query[i].x);
        }
    }
    rep(i,Q)cout<<ans[i]<<"\n";
}
0