結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 294 ms
11,500 KB
testcase_04 AC 311 ms
11,496 KB
testcase_05 AC 299 ms
11,500 KB
testcase_06 AC 147 ms
15,856 KB
testcase_07 AC 151 ms
15,848 KB
testcase_08 AC 152 ms
15,712 KB
testcase_09 AC 148 ms
15,852 KB
testcase_10 AC 143 ms
15,856 KB
testcase_11 AC 149 ms
15,852 KB
testcase_12 AC 147 ms
15,856 KB
testcase_13 AC 147 ms
15,836 KB
testcase_14 AC 147 ms
15,852 KB
testcase_15 AC 148 ms
15,740 KB
testcase_16 AC 340 ms
15,856 KB
testcase_17 AC 332 ms
15,720 KB
testcase_18 AC 340 ms
15,720 KB
testcase_19 AC 333 ms
15,852 KB
testcase_20 AC 329 ms
15,852 KB
testcase_21 AC 327 ms
15,852 KB
testcase_22 AC 342 ms
15,848 KB
testcase_23 AC 340 ms
15,852 KB
testcase_24 AC 329 ms
15,980 KB
testcase_25 AC 330 ms
15,860 KB
testcase_26 AC 179 ms
15,852 KB
testcase_27 AC 162 ms
15,852 KB
testcase_28 AC 34 ms
5,376 KB
testcase_29 AC 35 ms
5,504 KB
testcase_30 AC 36 ms
5,504 KB
testcase_31 AC 30 ms
5,504 KB
testcase_32 AC 38 ms
5,376 KB
testcase_33 AC 39 ms
5,376 KB
testcase_34 AC 32 ms
5,504 KB
testcase_35 AC 33 ms
5,376 KB
testcase_36 AC 33 ms
5,504 KB
testcase_37 AC 36 ms
5,376 KB
testcase_38 AC 212 ms
10,600 KB
testcase_39 AC 94 ms
5,632 KB
testcase_40 AC 324 ms
15,852 KB
testcase_41 AC 130 ms
6,784 KB
testcase_42 AC 201 ms
10,476 KB
testcase_43 AC 155 ms
8,044 KB
testcase_44 AC 256 ms
11,244 KB
testcase_45 AC 249 ms
10,860 KB
testcase_46 AC 182 ms
8,304 KB
testcase_47 AC 223 ms
10,856 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