結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-10-30 03:24:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,638 bytes
コンパイル時間 4,387 ms
コンパイル使用メモリ 183,048 KB
実行使用メモリ 30,016 KB
最終ジャッジ日時 2023-09-29 16:28:29
合計ジャッジ時間 23,808 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 63 ms
5,920 KB
testcase_29 AC 65 ms
6,144 KB
testcase_30 AC 66 ms
5,764 KB
testcase_31 AC 59 ms
5,892 KB
testcase_32 AC 68 ms
6,080 KB
testcase_33 AC 67 ms
6,056 KB
testcase_34 AC 61 ms
6,108 KB
testcase_35 AC 63 ms
5,968 KB
testcase_36 AC 63 ms
5,996 KB
testcase_37 AC 65 ms
6,068 KB
testcase_38 AC 457 ms
27,156 KB
testcase_39 AC 178 ms
7,780 KB
testcase_40 RE -
testcase_41 AC 273 ms
13,900 KB
testcase_42 AC 510 ms
25,940 KB
testcase_43 AC 336 ms
18,764 KB
testcase_44 RE -
testcase_45 RE -
testcase_46 AC 414 ms
23,540 KB
testcase_47 AC 495 ms
30,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::vector;
using ll=long long;
#define endl "\n";
const int INF=INT_MAX;
//セグ木
struct Segtree{
    int N;
    vector<int> node;
    Segtree(int n){
        N=1;
        while(N<n) N*=2;
        node.resize(N*2-1,-INF);
    }
    void update(int ind,int val){
        ind+=N-1;
        node[ind]=val;
        while(ind){
            ind=(ind-1)/2;
            node[ind]=std::max(node[ind*2+1],node[ind*2+2]);
        }
    }
    int lower_bound(int left,int val,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        if(node[now]<val||r<=left) return r;
        if(now>=N-1) return l;
        int left_ind=lower_bound(left,val,now*2+1,l,(l+r)/2);
        if(left_ind==(l+r)/2) return lower_bound(left,val,now*2+2,(l+r)/2,r);
        return left_ind;
    }
};
const int max=2e5,inf=1e9;
int main(){
    //入力、宣言
    int N; cin>>N;
    assert(1<=N&&N<=max);
    vector<int> X(N);
    std::set<ll> set;
    for(int i=0;i<N;i++){
        cin>>X[i];
        assert(1<=X[i]&&X[i]<=inf);
        assert(!set.count(X[i]));
        set.insert(X[i]);
    }
    const int MAX=1e5;
    int Q; cin>>Q;
    assert(1<=Q&&Q<=MAX);
    vector<int> l(Q),r(Q),x(Q),ans(Q,inf);
    for(int i=0;i<Q;i++){
        cin>>l[i]>>r[i]>>x[i];
        assert(1<=l[i]&&l[i]<=r[i]&&r[i]<=N&&x[i]<=inf&&1<=x[i]);
        l[i]--;
        r[i]--;
    }
    //本題。1 回目では x≤X_i を満たすものについて、2 回目では x≥X_i を満たすものについて求めている。
    for(int _=0;_<2;_++){
        //座圧
        vector<int> comp(N),rev(N);
        {
            vector<std::pair<int,int>> memo(N);
            for(int i=0;i<N;i++) memo[i]=std::make_pair(X[i],i);
            sort(memo.begin(),memo.end());
            for(int i=0;i<N;i++){
                comp[memo[i].second]=i;
                rev[i]=memo[i].first;
            }
        }
        vector<vector<int>> left(N),ind(N);
        for(int i=0;i<Q;i++){
            left[r[i]].push_back(l[i]);
            ind[r[i]].push_back(i);
        }
        Segtree seg(N);
        for(int i=0;i<N;i++){
            seg.update(comp[i],i);
            for(int j=0;j<left[i].size();j++){
                int l=lower_bound(rev.begin(),rev.end(),x[ind[i][j]])-rev.begin();
                int index=seg.lower_bound(l,left[i][j]);
                if(index<N) ans[ind[i][j]]=std::min(ans[ind[i][j]],rev[index]-x[ind[i][j]]);
            }
        }
        //上下を反転
        for(int i=0;i<N;i++) X[i]=-X[i];
        for(int i=0;i<Q;i++) x[i]=-x[i];
    }
    for(int i=0;i<Q;i++) cout<<ans[i]<<endl;
}
0