結果

問題 No.1332 Range Nearest Query
ユーザー mtsdmtsd
提出日時 2021-01-08 22:05:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 434 ms / 2,500 ms
コード長 4,467 bytes
コンパイル時間 1,630 ms
コンパイル使用メモリ 134,452 KB
実行使用メモリ 35,372 KB
最終ジャッジ日時 2023-08-10 09:46:42
合計ジャッジ時間 19,315 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 382 ms
25,660 KB
testcase_04 AC 388 ms
24,624 KB
testcase_05 AC 381 ms
24,628 KB
testcase_06 AC 336 ms
34,292 KB
testcase_07 AC 340 ms
35,372 KB
testcase_08 AC 338 ms
34,108 KB
testcase_09 AC 342 ms
34,712 KB
testcase_10 AC 344 ms
34,620 KB
testcase_11 AC 342 ms
34,832 KB
testcase_12 AC 341 ms
33,988 KB
testcase_13 AC 337 ms
34,092 KB
testcase_14 AC 337 ms
34,564 KB
testcase_15 AC 344 ms
34,664 KB
testcase_16 AC 424 ms
33,964 KB
testcase_17 AC 434 ms
33,924 KB
testcase_18 AC 433 ms
34,064 KB
testcase_19 AC 430 ms
34,004 KB
testcase_20 AC 427 ms
35,280 KB
testcase_21 AC 426 ms
34,436 KB
testcase_22 AC 426 ms
34,308 KB
testcase_23 AC 427 ms
34,476 KB
testcase_24 AC 421 ms
34,264 KB
testcase_25 AC 413 ms
34,712 KB
testcase_26 AC 320 ms
33,916 KB
testcase_27 AC 276 ms
35,316 KB
testcase_28 AC 75 ms
7,744 KB
testcase_29 AC 79 ms
8,092 KB
testcase_30 AC 79 ms
7,864 KB
testcase_31 AC 72 ms
7,688 KB
testcase_32 AC 84 ms
7,704 KB
testcase_33 AC 84 ms
7,688 KB
testcase_34 AC 73 ms
7,704 KB
testcase_35 AC 75 ms
8,044 KB
testcase_36 AC 75 ms
7,812 KB
testcase_37 AC 79 ms
8,028 KB
testcase_38 AC 299 ms
20,396 KB
testcase_39 AC 171 ms
7,948 KB
testcase_40 AC 419 ms
34,868 KB
testcase_41 AC 221 ms
12,256 KB
testcase_42 AC 291 ms
20,232 KB
testcase_43 AC 249 ms
14,320 KB
testcase_44 AC 356 ms
24,316 KB
testcase_45 AC 341 ms
23,024 KB
testcase_46 AC 280 ms
15,600 KB
testcase_47 AC 318 ms
21,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}
template<typename T> class segtree{
private:
    int n,sz;
    vector<T> node;
public:
    segtree(const vector<T>& v) : n(1), sz((int)v.size()){
        while(n < sz) n *= 2;
        node.resize(2*n-1, numeric_limits<T>::max());
        for(int i = 0; i < sz; i++){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = min(node[i*2+1], node[i*2+2]);
        }
    }
    void update(int k,T a){
        k += n-1;
        node[k] = a;
        while(k>0){
            k = (k-1)/2;
            node[k] = min(node[2*k+1],node[2*k+2]);
        }
    }
    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 numeric_limits<T>::max();
        }
        if(a <= l && r <= b){
            return node[k];
        }else{
            T vl = query(a,b,2*k+1,l,(l+r)/2);
            T vr = query(a,b,2*k+2,(l+r)/2,r);
            return min(vl,vr);
        }
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout<<query(i,i+1)<< " ";
        }
        cout<<endl;
    }
};

template<typename T> class segtree2{
private:
    int n,sz;
    vector<T> node;
public:
    segtree2(const vector<T>& v) : n(1), sz((int)v.size()){
        while(n < sz) n *= 2;
        node.resize(2*n-1, numeric_limits<T>::min());
        for(int i = 0; i < sz; i++){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = max(node[i*2+1], node[i*2+2]);
        }
    }
    void update(int k,T a){
        k += n-1;
        node[k] = a;
        while(k>0){
            k = (k-1)/2;
            node[k] = max(node[2*k+1],node[2*k+2]);
        }
    }
    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 numeric_limits<T>::min();
        }
        if(a <= l && r <= b){
            return node[k];
        }else{
            T vl = query(a,b,2*k+1,l,(l+r)/2);
            T vr = query(a,b,2*k+2,(l+r)/2,r);
            return max(vl,vr);
        }
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout<<query(i,i+1)<< " ";
        }
        cout<<endl;
    }
};


int main(){
    int n;
    cin >> n;
    vector<int>a(n);
    rep(i,n)cin >> a[i];
    int q;
    cin >> q;
    vector<pair<pair<int,int>,pair<int,int> > > query;
    rep(zz,q){
        int l,r,x;
        cin >> l >> r >> x;
        query.push_back(MP(MP(x,zz),MP(l-1,r)));
    }
    vector<pair<pair<int,int>,pair<int,int>> > v;
    rep(i,n){
        v.push_back(MP(MP(a[i],-1),MP(i,0)));
    }
    rep(i,q){
        v.push_back(query[i]);
    }
    sort(all(v));
    vector<ll>p(n,-(1LL<<60));
    segtree2<ll> seg(p);
    vector<ll> res(q,1LL<<60);
    rep(i,n+q){
        if(v[i].first.second==-1){
            seg.update(v[i].second.first,v[i].first.first);
        }else{
            chmin(res[v[i].first.second],v[i].first.first -seg.query(v[i].second.first,v[i].second.second));
        }
    }
    vector<ll>qq(n,(1LL<<60));
    segtree<ll> segg(qq);
    reverse(all(v));
    rep(i,n+q){
        if(v[i].first.second==-1){
            segg.update(v[i].second.first,v[i].first.first);
        }else{
            chmin(res[v[i].first.second],segg.query(v[i].second.first,v[i].second.second)-v[i].first.first);
        }
    }
    rep(i,q){
        cout << res[i] << "\n";
    }
    return 0;
}
0