結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-11-23 04:22:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,500 ms
コード長 1,798 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 179,260 KB
実行使用メモリ 11,904 KB
最終ジャッジ日時 2023-09-30 23:17:59
合計ジャッジ時間 14,549 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 252 ms
8,908 KB
testcase_04 AC 247 ms
9,200 KB
testcase_05 AC 248 ms
9,004 KB
testcase_06 AC 205 ms
11,592 KB
testcase_07 AC 206 ms
11,660 KB
testcase_08 AC 205 ms
11,548 KB
testcase_09 AC 204 ms
11,652 KB
testcase_10 AC 204 ms
11,496 KB
testcase_11 AC 203 ms
11,432 KB
testcase_12 AC 204 ms
11,572 KB
testcase_13 AC 204 ms
11,680 KB
testcase_14 AC 208 ms
11,488 KB
testcase_15 AC 203 ms
11,280 KB
testcase_16 AC 270 ms
11,824 KB
testcase_17 AC 277 ms
11,324 KB
testcase_18 AC 270 ms
11,556 KB
testcase_19 AC 279 ms
11,728 KB
testcase_20 AC 275 ms
11,376 KB
testcase_21 AC 270 ms
11,488 KB
testcase_22 AC 273 ms
11,356 KB
testcase_23 AC 273 ms
11,780 KB
testcase_24 AC 270 ms
11,904 KB
testcase_25 AC 272 ms
11,592 KB
testcase_26 AC 162 ms
11,888 KB
testcase_27 AC 138 ms
11,524 KB
testcase_28 AC 45 ms
5,048 KB
testcase_29 AC 47 ms
5,052 KB
testcase_30 AC 47 ms
5,052 KB
testcase_31 AC 40 ms
5,096 KB
testcase_32 AC 52 ms
5,024 KB
testcase_33 AC 52 ms
5,048 KB
testcase_34 AC 43 ms
5,144 KB
testcase_35 AC 45 ms
5,044 KB
testcase_36 AC 45 ms
5,124 KB
testcase_37 AC 48 ms
5,156 KB
testcase_38 AC 201 ms
8,344 KB
testcase_39 AC 112 ms
5,048 KB
testcase_40 AC 269 ms
11,444 KB
testcase_41 AC 142 ms
5,904 KB
testcase_42 AC 193 ms
8,284 KB
testcase_43 AC 160 ms
6,860 KB
testcase_44 AC 229 ms
9,348 KB
testcase_45 AC 214 ms
8,652 KB
testcase_46 AC 184 ms
7,120 KB
testcase_47 AC 206 ms
8,904 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=1<<30;
//セグ木
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 Query(int left,int right,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        if(left>=r||right<=l) return -inf;
        if(left<=l&&r<=right) return node[now];
        return std::max(Query(left,right,now*2+1,l,(l+r)/2),Query(left,right,now*2+2,(l+r)/2,r));
    }
};
int main(){    
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    //入力、宣言
    int N; cin>>N;
    vector<std::pair<int,int>> data;
    //data[i].second が 0 以下なら座標を、1 以上ならクエリを表す
    for(int i=0;i<N;i++){
        int X; cin>>X;
        data.push_back(std::make_pair(X,-i));
    }
    int Q; cin>>Q;
    vector<int> l(Q),r(Q);
    for(int i=0;i<Q;i++){
        int x; cin>>l[i]>>r[i]>>x;
        data.push_back(std::make_pair(x,i+1));
    }
    sort(data.begin(),data.end());
    vector<int> ans(Q,inf);
    //本題
    for(int _=0;_<2;_++){
        Segtree seg(N);
        for(auto p:data){
            if(p.second<=0) seg.update(-p.second,p.first);
            else{
                int index=p.second-1;
                ans[index]=std::min(ans[index],p.first-seg.Query(l[index]-1,r[index]));
            }
        }
        for(auto &p:data) p.first=inf-p.first;
        reverse(data.begin(),data.end());
    }
    for(int i=0;i<Q;i++) cout<<ans[i]<<endl;
}
0