結果

問題 No.1332 Range Nearest Query
ユーザー definedefine
提出日時 2021-01-09 10:34:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,808 bytes
コンパイル時間 2,846 ms
コンパイル使用メモリ 217,236 KB
実行使用メモリ 19,056 KB
最終ジャッジ日時 2024-04-28 17:46:16
合計ジャッジ時間 8,555 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
13,880 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 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 #

#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
#define len(s) (ll) s.size()

template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
constexpr ll inf = 3e18;
#line 3 "/home/defineprogram/Desktop/Library/structure/Mo.cpp"

struct Mo {
    vector<int> left, right, order;
    vector<bool> v;
    int width, l = 0, r = 0, cur = 0;
    Mo(int n) : width(sqrt(n)), v(n) {}
    void insert(int l, int r) {
        left.push_back(l);
        right.push_back(r);
    }
    void init() {
        order.resize(len(left));
        iota(all(order), 0);
        sort(all(order), [&](int a, int b) {
            if (left[a] / width != left[b] / width) return left[a] < left[b];
            return right[a] < right[b];
        });
    }
    int process() {
        int id = order[cur];
        while (l > left[id]) dis(--l);
        while (r < right[id]) dis(r++);
        while (l < left[id]) dis(l++);
        while (r > right[id]) dis(--r);
        return order[cur++];
    }
    inline void dis(int idx) {
        v[idx].flip();
        if (v[idx])
            add(idx);
        else
            del(idx);
    }
    void add(int);
    void del(int);
};
#line 3 "/home/defineprogram/Desktop/Library/structure/BIT.cpp"

template <class T>
class BIT {
    ll N;
    vector<T> bit;
    void add_(ll x, T y) {
        while (x <= N) {
            bit[x] += y;
            x += x & -x;
        }
    }
    T sum_(ll x) {
        T res = 0;
        while (x > 0) {
            res += bit[x];
            x -= x & -x;
        }
        return res;
    }

   public:
    ll lower_bound(T w) {
        if (w <= 0) return -1;
        ll x = 0;
        ll k = 1;
        while (k * 2 <= N) k *= 2;
        for (; k > 0; k /= 2) {
            if (x + k <= N && bit[x + k] < w) {
                w -= bit[x + k];
                x += k;
            }
        }
        return x;
    }
    void add(ll x, T y) { add_(x + 1, y); }
    T sum(ll l, ll r) { return sum_(r) - sum_(l); }
    BIT(ll x) : N(x), bit(x + 1) {}
};
/*
@brief Binary Indexed Tree
@docs docs/BIT.md
*/
#line 4 "main.cpp"

int N,X[1<<19];
BIT<int> bit(1),bit2(1);
multiset<int>st;
void Mo::add(int idx){
    bit.add(X[idx],1);
    if(bit.sum(X[idx],X[idx]+1)==1){
        bit2.add(X[idx],1);
    }
}
void Mo::del(int idx){
    bit.add(X[idx],-1);
    if(bit.sum(X[idx],X[idx]+1)==0){
        bit2.add(X[idx],-1);
    }
}

int Q,L[1<<17],R[1<<17],x[1<<17];
int ans[1<<17];
int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin>>N;
    vector<int>xx;
    rep(i,N){
        cin>>X[i];
        xx.push_back(X[i]);
    }
    xx.push_back(-1e9);xx.push_back(2e9);
    sort(all(xx));xx.erase(unique(all(xx)),xx.end());
    rep(i,N)X[i]=lower_bound(all(xx),X[i])-xx.begin();
    bit=BIT<int>(len(xx));
    bit2=BIT<int>(len(xx));
    bit.add(0,1);bit2.add(0,1);
    bit.add(len(xx)-1,1);bit2.add(len(xx)-1,1);
    Mo mo(len(xx));
    cin>>Q;
    rep(i,Q){
        cin>>L[i]>>R[i]>>x[i];
        L[i]--;
        mo.insert(L[i],R[i]);
    }
    mo.init();
    rep(i,Q){
        int id=mo.process();
        int idx=lower_bound(all(xx),x[id])-xx.begin();
        int memo=bit2.sum(0,idx);
        int l=bit2.lower_bound(memo),r=bit2.lower_bound(memo+1);
        ans[id]=min(x[id]-xx[l],xx[r]-x[id]);
    }
    rep(i,Q){
        cout<<ans[i]<<"\n";
    }
}
0