結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-11-05 05:37:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 780 ms / 2,500 ms
コード長 2,617 bytes
コンパイル時間 4,391 ms
コンパイル使用メモリ 183,804 KB
実行使用メモリ 45,656 KB
最終ジャッジ日時 2023-09-29 16:42:36
合計ジャッジ時間 29,115 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 682 ms
39,480 KB
testcase_04 AC 682 ms
39,424 KB
testcase_05 AC 692 ms
39,292 KB
testcase_06 AC 493 ms
43,864 KB
testcase_07 AC 495 ms
44,116 KB
testcase_08 AC 488 ms
43,724 KB
testcase_09 AC 495 ms
43,800 KB
testcase_10 AC 498 ms
43,980 KB
testcase_11 AC 489 ms
43,944 KB
testcase_12 AC 493 ms
43,868 KB
testcase_13 AC 487 ms
43,744 KB
testcase_14 AC 489 ms
43,952 KB
testcase_15 AC 498 ms
43,876 KB
testcase_16 AC 754 ms
45,444 KB
testcase_17 AC 779 ms
45,656 KB
testcase_18 AC 760 ms
45,536 KB
testcase_19 AC 772 ms
45,464 KB
testcase_20 AC 767 ms
45,460 KB
testcase_21 AC 762 ms
45,460 KB
testcase_22 AC 780 ms
45,464 KB
testcase_23 AC 770 ms
45,532 KB
testcase_24 AC 759 ms
45,532 KB
testcase_25 AC 776 ms
45,516 KB
testcase_26 AC 429 ms
41,752 KB
testcase_27 AC 415 ms
41,816 KB
testcase_28 AC 63 ms
6,056 KB
testcase_29 AC 66 ms
6,052 KB
testcase_30 AC 66 ms
5,952 KB
testcase_31 AC 59 ms
5,980 KB
testcase_32 AC 68 ms
6,000 KB
testcase_33 AC 68 ms
5,948 KB
testcase_34 AC 61 ms
6,092 KB
testcase_35 AC 63 ms
6,120 KB
testcase_36 AC 63 ms
6,032 KB
testcase_37 AC 65 ms
6,200 KB
testcase_38 AC 478 ms
26,940 KB
testcase_39 AC 181 ms
7,516 KB
testcase_40 AC 747 ms
44,560 KB
testcase_41 AC 278 ms
13,756 KB
testcase_42 AC 454 ms
26,148 KB
testcase_43 AC 348 ms
18,816 KB
testcase_44 AC 618 ms
35,208 KB
testcase_45 AC 558 ms
32,344 KB
testcase_46 AC 429 ms
23,260 KB
testcase_47 AC 521 ms
29,872 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=3e5,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]);
    }
    int Q; cin>>Q;
    assert(1<=Q&&Q<=max/3);
    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