結果

問題 No.1332 Range Nearest Query
ユーザー yuto1115yuto1115
提出日時 2020-10-31 00:02:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 543 ms / 2,500 ms
コード長 4,943 bytes
コンパイル時間 2,836 ms
コンパイル使用メモリ 211,460 KB
実行使用メモリ 12,132 KB
最終ジャッジ日時 2023-09-29 16:33:16
合計ジャッジ時間 22,832 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 477 ms
11,624 KB
testcase_04 AC 474 ms
11,636 KB
testcase_05 AC 486 ms
11,636 KB
testcase_06 AC 379 ms
11,844 KB
testcase_07 AC 379 ms
12,092 KB
testcase_08 AC 387 ms
12,132 KB
testcase_09 AC 388 ms
12,072 KB
testcase_10 AC 384 ms
11,840 KB
testcase_11 AC 383 ms
11,864 KB
testcase_12 AC 385 ms
11,832 KB
testcase_13 AC 383 ms
11,836 KB
testcase_14 AC 384 ms
11,932 KB
testcase_15 AC 376 ms
11,924 KB
testcase_16 AC 504 ms
11,992 KB
testcase_17 AC 513 ms
12,016 KB
testcase_18 AC 507 ms
11,932 KB
testcase_19 AC 504 ms
11,912 KB
testcase_20 AC 503 ms
11,840 KB
testcase_21 AC 497 ms
11,920 KB
testcase_22 AC 500 ms
11,836 KB
testcase_23 AC 543 ms
11,888 KB
testcase_24 AC 500 ms
11,880 KB
testcase_25 AC 502 ms
11,792 KB
testcase_26 AC 167 ms
12,128 KB
testcase_27 AC 287 ms
11,904 KB
testcase_28 AC 63 ms
5,676 KB
testcase_29 AC 66 ms
5,592 KB
testcase_30 AC 65 ms
5,436 KB
testcase_31 AC 54 ms
5,664 KB
testcase_32 AC 69 ms
5,496 KB
testcase_33 AC 67 ms
5,616 KB
testcase_34 AC 59 ms
5,560 KB
testcase_35 AC 61 ms
5,564 KB
testcase_36 AC 61 ms
5,596 KB
testcase_37 AC 66 ms
5,532 KB
testcase_38 AC 411 ms
8,664 KB
testcase_39 AC 321 ms
6,748 KB
testcase_40 AC 498 ms
12,020 KB
testcase_41 AC 347 ms
7,916 KB
testcase_42 AC 404 ms
8,584 KB
testcase_43 AC 376 ms
8,096 KB
testcase_44 AC 464 ms
11,308 KB
testcase_45 AC 446 ms
11,028 KB
testcase_46 AC 395 ms
8,532 KB
testcase_47 AC 429 ms
10,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

template<class Monoid>
class segtree {
    using T = typename Monoid::value_type;
    
    int n;
    vector<T> val;

public:
    constexpr segtree(int _n) {
        n = 1;
        while (n < _n) n *= 2;
        val = vector<T>(2 * n, Monoid::identity);
    }
    
    constexpr segtree(vector<T> init) {
        int _n = init.size();
        n = 1;
        while (n < _n) n *= 2;
        val = vector<T>(2 * n, Monoid::identity);
        rep(i, _n) val[i + n] = init[i];
        rrep(i, n) val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]);
    }
    
    // segment [l,r)
    T fold(int l, int r) {
        l += n, r += n;
        T fold_l = Monoid::identity;
        T fold_r = Monoid::identity;
        while (l < r) {
            if (l & 1) fold_l = Monoid::operate(fold_l, val[l++]);
            if (r & 1) fold_r = Monoid::operate(val[--r], fold_r);
            l >>= 1, r >>= 1;
        }
        return Monoid::operate(fold_l, fold_r);
    }
    
    template<class F>
    void update(int i, const F &f) {
        i += n;
        val[i] = f(val[i]);
        while (i > 1) {
            i >>= 1;
            val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]);
        }
    }
};

class Node {
public:
    using value_type = int;
    
    value_type value;
    
    Node(value_type value) : value(value) {}
    
    static constexpr value_type identity = -1;
    
    static constexpr value_type operate(const value_type &l, const value_type &r) {
        return max(l,r);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    vi X(n);
    cin >> X;
    vi pr = X;
    int q;
    cin >> q;
    vi l(q), r(q), x(q);
    rep(i, q) {
        cin >> l[i] >> r[i] >> x[i];
        l[i]--; r[i]--;
        pr.pb(x[i]);
    }
    sort(all(pr));
    pr.erase(unique(all(pr)),pr.end());
    int sz = pr.size();
    vi ord(q);
    iota(all(ord), 0);
    sort(all(ord), [&](int i,int j){ return r[i] < r[j]; });
    segtree<Node> st(sz);
    vi ans(q,inf);
    int now = 0;
    rep(i,n) {
        X[i] = lower_bound(all(pr), X[i]) - pr.begin();
        st.update(X[i], [&](auto) { return i; });
        while(now < q and r[ord[now]] == i) {
            int nl = l[ord[now]];
            int nr = r[ord[now]];
            int nx = x[ord[now]];
            nx = lower_bound(all(pr),nx)-pr.begin();
            if(st.fold(nx,sz) >= nl) {
                int ok = sz, ng = nx;
                auto f = [&](int t) -> bool {
                    return st.fold(nx,t) >= nl;
                };
                while(abs(ok-ng) > 1) {
                    int mid = (ng+ok)/2;
                    if(f(mid)) ok = mid;
                    else ng = mid;
                }
                chmin(ans[ord[now]],pr[ok-1]-pr[nx]);
            }
            if(st.fold(0,nx+1) >= nl) {
                int ok = 0, ng = nx+1;
                auto f = [&](int t) -> bool {
                    return st.fold(t,nx+1) >= nl;
                };
                while(abs(ok-ng) > 1) {
                    int mid = (ng+ok)/2;
                    if(f(mid)) ok = mid;
                    else ng = mid;
                }
                chmin(ans[ord[now]],pr[nx]-pr[ok]);
            }
            now++;
        }
    }
    for(int i : ans) cout << i << '\n';
}
0