結果

問題 No.1332 Range Nearest Query
ユーザー mtsdmtsd
提出日時 2021-01-08 21:51:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,885 bytes
コンパイル時間 1,346 ms
コンパイル使用メモリ 126,664 KB
実行使用メモリ 24,032 KB
最終ジャッジ日時 2024-04-28 02:44:18
合計ジャッジ時間 6,546 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
}

int a[300000];
int res;        //区間内の種類の数
multiset<int>st;
class Mo{
private:
    vector<int> left, right,x;
    const int width;
    void add(const int id);
    void del(const int id);

public:
    vector<int> ans;

    Mo(const int n) : width((int)sqrt(n)){}
    // クエリ[l,r)
    void insert(const int l, const int r,const int xx){
        left.push_back(l), right.push_back(r);
        x.push_back(xx);
    }
    void solve(){
        const int sz = (int)left.size();
        int nl = 0, nr = 0;
        vector<int> ord(sz);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](const int a, const int b){
            const int c = left[a] / width, d = left[b] / width;
            return (c == d) ? ((c & 1) ? (right[b] < right[a]) : (right[a] < right[b])) : (c < d);
        });
        ans.resize(sz);
        for(const int id : ord){
            while(nl > left[id]) add(--nl);
            while(nr < right[id]) add(nr++);  //add
            while(nl < left[id]) del(nl++);
            while(nr > right[id]) del(--nr);  //del
            auto k = st.lower_bound(x[id]);
            int tmp = inf;
            if(k!=st.end()){
                tmp = (*k)-x[id];
            }
            if(k!=st.begin()){
                k--;
                chmin(tmp,x[id]-(*k));
            }
            ans[id] = tmp;
        }
    }
};

//idは元の配列のインデックス
void Mo::add(const int id)
{
    st.insert(a[id]);
}

void Mo::del(const int id)
{
    st.erase(st.find(a[id]));
}

int main(){
    int n;
    cin >> n;
    rep(i,n)cin >> a[i];
    int q;
    cin >> q;
    Mo mo(n);
    rep(zz,q){
        int l,r,x;
        cin >> l >> r >> x;
        mo.insert(l-1,r,x);
    }
    mo.solve();
    rep(i,q){
        cout << mo.ans[i] << "\n";
    }
    return 0;
}
0