結果

問題 No.1332 Range Nearest Query
ユーザー milanis48663220milanis48663220
提出日時 2021-01-09 00:35:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,875 ms / 2,500 ms
コード長 4,145 bytes
コンパイル時間 1,517 ms
コンパイル使用メモリ 134,240 KB
実行使用メモリ 50,688 KB
最終ジャッジ日時 2024-04-28 06:56:20
合計ジャッジ時間 44,757 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1,743 ms
46,572 KB
testcase_04 AC 1,748 ms
46,576 KB
testcase_05 AC 1,749 ms
46,632 KB
testcase_06 AC 1,144 ms
49,556 KB
testcase_07 AC 1,154 ms
49,664 KB
testcase_08 AC 1,165 ms
49,668 KB
testcase_09 AC 1,157 ms
49,536 KB
testcase_10 AC 1,141 ms
49,664 KB
testcase_11 AC 1,151 ms
49,540 KB
testcase_12 AC 1,152 ms
49,668 KB
testcase_13 AC 1,147 ms
49,784 KB
testcase_14 AC 1,160 ms
49,920 KB
testcase_15 AC 1,155 ms
49,536 KB
testcase_16 AC 1,834 ms
50,564 KB
testcase_17 AC 1,875 ms
50,432 KB
testcase_18 AC 1,824 ms
50,432 KB
testcase_19 AC 1,842 ms
50,432 KB
testcase_20 AC 1,839 ms
50,432 KB
testcase_21 AC 1,822 ms
50,688 KB
testcase_22 AC 1,857 ms
50,556 KB
testcase_23 AC 1,855 ms
50,560 KB
testcase_24 AC 1,858 ms
50,560 KB
testcase_25 AC 1,857 ms
50,404 KB
testcase_26 AC 476 ms
40,628 KB
testcase_27 AC 1,002 ms
50,560 KB
testcase_28 AC 244 ms
10,096 KB
testcase_29 AC 246 ms
10,224 KB
testcase_30 AC 250 ms
10,352 KB
testcase_31 AC 228 ms
10,224 KB
testcase_32 AC 247 ms
10,228 KB
testcase_33 AC 270 ms
10,224 KB
testcase_34 AC 474 ms
10,356 KB
testcase_35 AC 240 ms
10,224 KB
testcase_36 AC 232 ms
10,352 KB
testcase_37 AC 240 ms
10,092 KB
testcase_38 AC 1,451 ms
35,528 KB
testcase_39 AC 1,106 ms
21,016 KB
testcase_40 AC 1,836 ms
49,524 KB
testcase_41 AC 1,242 ms
25,544 KB
testcase_42 AC 1,470 ms
34,728 KB
testcase_43 AC 1,303 ms
29,532 KB
testcase_44 AC 1,648 ms
44,860 KB
testcase_45 AC 1,630 ms
40,196 KB
testcase_46 AC 1,417 ms
33,260 KB
testcase_47 AC 1,546 ms
40,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>
#include <cmath>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

int n;
int X[300000];
int q;
int l[100000], r[100000], x[100000], q_idx[100000];
map<int, int> compress;
vector<int> recover;

void input(){
    cin >> n;
    for(int i = 0; i < n; i++) cin >> X[i];
    cin >> q;
    vector<vector<int>> v;
    for(int i = 0; i < q; i++){
        int l, r, x;
        cin >> l >> r >> x; l--; r--;
        v.push_back({r, l, x, i});
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < q; i++){
        l[i] = v[i][1], r[i] = v[i][0], x[i] = v[i][2], q_idx[i] = v[i][3];
    }
    {
        set<int> st;
        for(int i = 0; i < n; i++) st.insert(X[i]);
        for(int i = 0; i < q; i++) st.insert(x[i]);
        int i = 0;
        for(int y : st){
            compress[y] = i;
            recover.push_back(y);
            i++;
        }
    }
}

int calc_max(int a, int b){ return max(a, b); } 

const int INF = 2e9;

void solve(){
    int m = compress.size();
    segtree<int> sgt(m, -INF, calc_max);
    int cur_r = -1;
    vector<int> ret(q);
    for(int i = 0; i < q; i++){
        // cout << l[i] << ',' << r[i] << endl;
        int ans = INF;
        while(cur_r < r[i]){
            cur_r++;
            sgt.update(compress[X[cur_r]], cur_r);
        }
        int idx = compress[x[i]];
        // idxより小さい最大の値
        if(sgt.query(0, idx+1) >= l[i]){
            if(sgt.query(idx, idx+1) >= l[i]){
                ans = 0;
            }else{
                int lb = 0, rb = idx;
                while(rb-lb > 1){
                    int c = (lb+rb)/2;
                    if(sgt.query(c, idx+1) >= l[i]) lb = c;
                    else rb = c;
                }
                chmin(ans, abs(x[i]-recover[lb]));
            }
        }
        // idxより大きい最小の値
        if(sgt.query(idx, m) >= l[i]){
            if(sgt.query(idx, idx+1) >= l[i]){
                ans = 0;
            }else{
                int lb = idx, rb = m;
                while(rb-lb > 1){
                    int c = (lb+rb)/2;
                    if(sgt.query(idx, c+1) >= l[i]) rb = c;
                    else lb = c;
                }
                chmin(ans, abs(x[i]-recover[rb]));
            }   
        }
        ret[q_idx[i]] = ans;
    }
    for(int i = 0; i < q; i++) cout << ret[i] << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    solve();
}
0