結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-11-23 04:17:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,787 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 178,736 KB
実行使用メモリ 12,292 KB
最終ジャッジ日時 2023-09-30 23:17:38
合計ジャッジ時間 14,878 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 254 ms
9,044 KB
testcase_04 WA -
testcase_05 AC 245 ms
9,112 KB
testcase_06 AC 216 ms
11,436 KB
testcase_07 AC 220 ms
11,576 KB
testcase_08 AC 216 ms
11,536 KB
testcase_09 AC 213 ms
11,416 KB
testcase_10 AC 213 ms
11,544 KB
testcase_11 AC 216 ms
11,476 KB
testcase_12 AC 218 ms
11,416 KB
testcase_13 AC 213 ms
11,516 KB
testcase_14 AC 215 ms
11,464 KB
testcase_15 AC 216 ms
11,596 KB
testcase_16 AC 271 ms
11,392 KB
testcase_17 AC 271 ms
12,292 KB
testcase_18 AC 274 ms
11,376 KB
testcase_19 AC 270 ms
11,556 KB
testcase_20 AC 269 ms
11,360 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 269 ms
11,640 KB
testcase_25 AC 269 ms
11,484 KB
testcase_26 AC 198 ms
11,412 KB
testcase_27 AC 151 ms
11,440 KB
testcase_28 AC 59 ms
5,048 KB
testcase_29 AC 60 ms
5,156 KB
testcase_30 AC 60 ms
5,092 KB
testcase_31 AC 53 ms
5,204 KB
testcase_32 AC 65 ms
5,200 KB
testcase_33 AC 66 ms
5,040 KB
testcase_34 AC 56 ms
5,144 KB
testcase_35 AC 57 ms
5,036 KB
testcase_36 AC 58 ms
5,068 KB
testcase_37 AC 60 ms
5,124 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 263 ms
11,416 KB
testcase_41 WA -
testcase_42 AC 191 ms
8,248 KB
testcase_43 WA -
testcase_44 AC 228 ms
9,860 KB
testcase_45 WA -
testcase_46 AC 182 ms
7,088 KB
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

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*=-1;
        sort(data.begin(),data.end());
    }
    for(int i=0;i<Q;i++) cout<<ans[i]<<endl;
}
0