結果

問題 No.1332 Range Nearest Query
ユーザー T101010101T101010101
提出日時 2023-06-26 16:47:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 13,746 bytes
コンパイル時間 6,076 ms
コンパイル使用メモリ 393,548 KB
実行使用メモリ 14,396 KB
最終ジャッジ日時 2024-07-03 11:45:54
合計ジャッジ時間 23,169 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma region Macros

// #pragma GCC target("avx, avx2, fma")
// #pragma GCC optimize("O3,unroll-loops")

#include <bits/extc++.h>
#include <immintrin.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using namespace __gnu_pbds;

// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// using Bint = mp::cpp_int;
// using Bdouble = mp::number<mp::cpp_dec_float<128>>;

#define TO_STRING(var) # var
#define pb emplace_back
#define int ll
#define endl '\n'
#define sqrt __builtin_sqrtl

using ll = long long;
using ld = long double;
const ld PI = acos(-1);
const ld EPS = 1e-10;
const int INF = 1 << 30;
const ll INFL = 1LL << 61;
const int MOD = 998244353;
// const int MOD = 1000000007;

const vector<int> dx = {0, 1, -1, 0, 1, 1, -1, -1}; // → ↓ ← ↑ ↘ ↙ ↖ ↗
const vector<int> dy = {1, 0, 0, -1, 1, -1, -1, 1};

struct Edge {
    int from, to;
    int cost;
    Edge(int to, int cost) : from(-1), to(to), cost(cost) {}
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    Edge &operator=(const int &x) {
        to = x;
        return *this;
    }
};

__attribute__((constructor))
void constructor() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int POW(int x, int n) {
    __int128_t ret = 1;
    // if (ret >= INFL) return INFL;
    if (n < 0) { cout << "error" << endl; return 0; }
    else if (x == 1 or n == 0) ret = 1;
    else if (x == -1 && n % 2 == 0) ret = 1; 
    else if (x == -1) ret = -1; 
    else if (n % 2 == 0) ret = POW(x * x, n / 2);
    else ret = x * POW(x, n - 1);

    if (ret > 8e18) ret = 0;
    return ret;
}
int floor(int x, int y) { return (x > 0 ? x / y : (x - y + 1) / y); }
int ceil(int x, int y) { return (x > 0 ? (x + y - 1) / y : x / y); }
ll per(int x, int y) {
    if (y == 0) {
        cout << "error" << endl;
        return INFL;
    }
    if (x >= 0 && y > 0) return x / y;
    if (x >= 0 && y < 0) return x / y - (x % y < 0);
    if (x < 0 && y < 0) return x / y + (x % y < 0);
    // if (x < 0 && y > 0) 
    return x / y - (x % y < 0);
}
ll mod(int x, int y) {
    if (y == 0) {
        cout << "error" << endl;
        return INFL;
    }
    if (x >= 0 && y > 0) return x % y;
    if (x >= 0 && y < 0) return x % y;
    if (x < 0 && y < 0) {
        __int128_t ret = x % y;
        ret += (__int128_t)abs(y) * INFL;
        ret %= abs(y);
        return ret;
    }
    // if (x < 0 && y > 0) {
        __int128_t ret = x % y;
        ret += (__int128_t)abs(y) * INFL;
        ret %= abs(y);
        return ret;
    // }
}

template <class T> bool chmax(T &a, const T& b) {
    if (a < b) { a = b; return true; }
    return false;
}
template <class T> bool chmin(T &a, const T& b) {
    if (a > b) { a = b; return true; }
    return false;
}

int countl_zero(int N) { return __builtin_clzll(N); }
int countl_one(int N) {
    int ret = 0; while (N % 2) { N /= 2; ret++; }
    return ret;
}
int countr_zero(int N) { return __builtin_ctzll(N); }
int countr_one(int N) {
    int ret = 0, k = 63 - __builtin_clzll(N);
    while (k != -1 && (N & (1LL << k))) { k--; ret++; }
    return ret;
}
int popcount(int N) { return __builtin_popcountll(N); }
int unpopcount(int N) { return 64 - __builtin_clzll(N) - __builtin_popcountll(N); }

int top_bit(int N) { return 63 - __builtin_clzll(N);} // 2^kの位
int bot_bit(int N) { return __builtin_ctz(N);} // 2^kの位
int MSB(int N) { return 1 << (63 - __builtin_clzll(N)); } // mask

int bit_width(int N) { return 64 - __builtin_clzll(N); } // 桁数
int ceil_log2(int N) { return 63 - __builtin_clzll(N); }
int bit_floor(int N) { return 1 << (63 - __builtin_clzll(N)); }
int floor_log2(int N) { return 64 - __builtin_clzll(N-1); }
int bit_ceil(int N) { return 1 << (64 - __builtin_clzll(N-1)) - (N==1); }

class UnionFind {
public:
	UnionFind() = default;
    UnionFind(int N) : par(N), sz(N, 1) {
        iota(par.begin(), par.end(), 0);
    }

	int root(int x) {
		if (par[x] == x) return x;
		return (par[x] = root(par[x]));
	}

	bool unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);

        if (rx == ry) return false;
		if (sz[rx] < sz[ry]) swap(rx, ry);

		sz[rx] += sz[ry];
		par[ry] = rx;

        return true;
	}

	bool issame(int x, int y) { return (root(x) == root(y)); }
	int size(int x) { return sz[root(x)]; }

    vector<vector<int>> groups(int N) {
        vector<vector<int>> G(N);
        for (int x = 0; x < N; x++) {
            G[root(x)].push_back(x);
        }
		G.erase(
            remove_if(G.begin(), G.end(),
                [&](const vector<int>& V) { return V.empty(); }),
                    G.end());
        return G;
    }

private:
	vector<int> par;
	vector<int> sz;
};

template<int mod> class Modint{
public:
    int val = 0;
    Modint(int x = 0) { while (x < 0) x += mod; val = x % mod; }
    Modint(const Modint &r) { val = r.val; }

    Modint operator -() { return Modint(-val); } // 単項
    Modint operator +(const Modint &r) { return Modint(*this) += r; }
    Modint operator +(const int &q) { Modint r(q); return Modint(*this) += r; }
    Modint operator -(const Modint &r) { return Modint(*this) -= r; }
    Modint operator -(const int &q) { Modint r(q); return Modint(*this) -= r; }
    Modint operator *(const Modint &r) { return Modint(*this) *= r; }
    Modint operator *(const int &q) { Modint r(q); return Modint(*this) *= r; }
    Modint operator /(const Modint &r) { return Modint(*this) /= r; }
    Modint operator /(const int &q) { Modint r(q); return Modint(*this) /= r; }
    
    Modint& operator ++() { val++; if (val >= mod) val -= mod; return *this; } // 前置
    Modint operator ++(signed) { ++*this; return *this; } // 後置
    Modint& operator --() { val--; if (val < 0) val += mod; return *this; }
    Modint operator --(signed) { --*this; return *this; }
    Modint &operator +=(const Modint &r) { val += r.val; if (val >= mod) val -= mod; return *this; }
    Modint &operator +=(const int &q) { Modint r(q); val += r.val; if (val >= mod) val -= mod; return *this; }
    Modint &operator -=(const Modint &r) { if (val < r.val) val += mod; val -= r.val; return *this; }
    Modint &operator -=(const int &q) { Modint r(q);  if (val < r.val) val += mod; val -= r.val; return *this; }
    Modint &operator *=(const Modint &r) { val = val * r.val % mod; return *this; }
    Modint &operator *=(const int &q) { Modint r(q); val = val * r.val % mod; return *this; }
    Modint &operator /=(const Modint &r) {
        int a = r.val, b = mod, u = 1, v = 0;
        while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}
        val = val * u % mod; if (val < 0) val += mod;
        return *this;
    }
    Modint &operator /=(const int &q) {
        Modint r(q); int a = r.val, b = mod, u = 1, v = 0;
        while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}
        val = val * u % mod; if (val < 0) val += mod;
        return *this;
    }

    bool operator ==(const Modint& r) { return this -> val == r.val; }
    bool operator <(const Modint& r) { return this -> val < r.val; }
    bool operator !=(const Modint& r) { return this -> val != r.val; }
};

using mint = Modint<MOD>;

istream &operator >>(istream &is, mint& x) {
    int t; is >> t;
    x = t;
    return (is);
}
ostream &operator <<(ostream &os, const mint& x) {
    return os << x.val;
}
mint modpow(const mint &x, int n) {
    if (n == 0) return 1;
    mint t = modpow(x, n / 2);
    t = t * t;
    if (n & 1) t = t * x;
    return t;
}

int modpow(__int128_t x, int n, int mod) {
    __int128_t ret = 1;
    while (n > 0) {
        if (n % 2 == 1) ret = ret * x % mod;
        x = x * x % mod;
        n /= 2;
    }
    return ret;
}

int modinv(__int128_t x, int mod) {
    if (x == 1) return 1;
    return mod - modinv(mod % x, mod) * (mod / x) % mod;
}

vector<mint> fac, finv, Inv;
void COMinit(int N) {
    fac.resize(N + 1);
    finv.resize(N + 1);
    Inv.resize(N + 1);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    Inv[1] = 1;
    for (int i = 2; i <= N; i++) {
        fac[i] = fac[i-1] * mint(i);
        Inv[i] = -Inv[MOD % i] * mint(MOD / i);
        finv[i] = finv[i - 1] * Inv[i];
    }
}

mint COM(int N, int K) {
    if (N < K) return 0;
    if (N < 0 or K < 0) return 0;
    return fac[N] * finv[K] * finv[N - K];
}

#pragma endregion

struct bit_vector {
    using u32 = uint32_t;
    using i64 = int64_t;
    using u64 = uint64_t;

    static constexpr u32 w = 64;
    vector<u64> block;
    vector<u32> count;
    u32 n, zeros;

    inline u32 get(u32 i) const { return u32(block[i / w] >> (i % w)) & 1u; }
    inline void set(u32 i) { block[i / w] |= 1LL << (i % w); }

    bit_vector() {}
    bit_vector(int _n) { init(_n); }
    __attribute__((optimize("O3", "unroll-loops"))) void init(int _n) {
        n = zeros = _n;
        block.resize(n / w + 1, 0);
        count.resize(block.size(), 0);
    }

    __attribute__((target("popcnt"))) void build() {
        for (u32 i = 1; i < block.size(); i++)
        count[i] = count[i - 1] + _mm_popcnt_u64(block[i - 1]);
        zeros = rank0(n);
    }

    inline u32 rank0(u32 i) const { return i - rank1(i); }
    __attribute__((target("bmi2,popcnt"))) inline u32 rank1(u32 i) const {
        return count[i / w] + _mm_popcnt_u64(_bzhi_u64(block[i / w], i % w));
    }
};

template <typename T>
struct WaveletMatrix {
    using u32 = uint32_t;
    using i64 = int64_t;
    using u64 = uint64_t;

    int n, lg;
    vector<T> A;
    vector<bit_vector> bv;

    WaveletMatrix(u32 _n) : n(max<u32>(_n, 1)), A(n) {}
    WaveletMatrix(const vector<T>& _a) : n(_a.size()), A(_a) { build(); }

    __attribute__((optimize("O3"))) void build() {
        lg = __lg(max<T>(*max_element(begin(A), end(A)), 1)) + 1;
        bv.assign(lg, n);
        vector<T> cur = A, nxt(n);
        for (int h = lg - 1; h >= 0; h--) {
            for (int i = 0; i < n; i++) {
                if ((cur[i] >> h) & 1) bv[h].set(i);
            }
            bv[h].build();
            array<decltype(begin(nxt)), 2> it{begin(nxt), begin(nxt) + bv[h].zeros};
            for (int i = 0; i < n; i++) *it[bv[h].get(i)]++ = cur[i];
            swap(cur, nxt);
        }
        return;
    }

    void set(u32 i, const T& x) { 
        assert(x >= 0);
        A[i] = x; 
    }

    inline pair<u32, u32> succ0(int l, int r, int h) const {
        return make_pair(bv[h].rank0(l), bv[h].rank0(r));
    }

    inline pair<u32, u32> succ1(int l, int r, int h) const {
        u32 l0 = bv[h].rank0(l);
        u32 r0 = bv[h].rank0(r);
        u32 zeros = bv[h].zeros;
        return make_pair(l + zeros - l0, r + zeros - r0);
    }

    // return A[k]
    T access(u32 k) const {
        T ret = 0;
        for (int h = lg - 1; h >= 0; h--) {
            u32 f = bv[h].get(k);
            ret |= f ? T(1) << h : 0;
            k = f ? bv[h].rank1(k) + bv[h].zeros : bv[h].rank0(k);
        }
        return ret;
    }

    // k-th (0-indexed) smallest number in A[l, r)
    T kth_smallest(u32 l, u32 r, u32 k) const {
        T res = 0;
        for (int h = lg - 1; h >= 0; h--) {
            u32 l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);
            if (k < r0 - l0)
                l = l0, r = r0;
            else {
                k -= r0 - l0;
                res |= (T)1 << h;
                l += bv[h].zeros - l0;
                r += bv[h].zeros - r0;
            }
        }
        return res;
    }

    // k-th (0-indexed) largest number in A[l, r)
    T kth_largest(int l, int r, int k) {
        return kth_smallest(l, r, r - l - k - 1);
    }

    // range_freq i s.t. (l <= i < r) && (v[i] < upper)
    int range_freq(int l, int r, T upper) {
        if (upper >= (T(1) << lg)) return r - l;
        int ret = 0;
        for (int h = lg - 1; h >= 0; h--) {
            bool f = (upper >> h) & 1;
            u32 l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);
            if (f) {
                ret += r0 - l0;
                l += bv[h].zeros - l0;
                r += bv[h].zeros - r0;
            } else {
                l = l0;
                r = r0;
            }
        }
        return ret;
    }

    int count(int l, int r, T x) {
        return range_freq(l, r, x + 1) - range_freq(l, r, x);
    }

    int count(int l, int r, T lower, T upper) {
        return range_freq(l, r, upper) - range_freq(l, r, lower);
    }

    // max v[i] s.t. (l <= i < r) && (v[i] < upper)
    T prev_value(int l, int r, T upper) {
        int cnt = range_freq(l, r, upper);
        return cnt == 0 ? T(-1) : kth_smallest(l, r, cnt - 1);
    }

    // min v[i] s.t. (l <= i < r) && (lower <= v[i])
    T next_value(int l, int r, T lower) {
        int cnt = range_freq(l, r, lower);
        return cnt == r - l ? T(-1) : kth_smallest(l, r, cnt);
    }
};

signed main() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];
    WaveletMatrix<int> WM(A);

    int Q;
    cin >> Q;
    for (int q = 0; q < Q; q++) {
        int l, r, x;
        cin >> l >> r >> x;
        l--; r--;
        int a = WM.prev_value(l, r + 1, x);
        int b = WM.next_value(l, r + 1, x);
        cout << min(abs(a - x), abs(b - x)) << endl;
    }
}
0