結果

問題 No.3446 Range Adjacent Differences
ユーザー K2
提出日時 2026-02-18 21:58:37
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
TLE  
実行時間 -
コード長 3,287 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,465 ms
コンパイル使用メモリ 390,376 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-02-18 21:58:50
合計ジャッジ時間 12,392 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

using ll = long long;
using mint = modint998244353;
// using mint = modint1000000007;

template <typename T> using vec = vector<T>;
template <typename T> using pa = pair<T, T>;

#define REP(_1, _2, _3, _4, name, ...) name
#define REP1(i, n) for (auto i = decay_t<decltype(n)>{}; (i) < (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) < (r); ++(i))
#define REP3(i, l, r, d) for (auto i = (l); (i) < (r); i += (d))
#define rep(...) REP(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)
#define rrep(i, r, l) for (int i = (r); i >= (l); --i)
template <typename T> bool chmax(T &a, const T &b) { return (a < b ? a = b, true : false); }
template <typename T> bool chmin(T &a, const T &b) { return (a > b ? a = b, true : false); }

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr int mov[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

void solve();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

void solve() {
    int N, Q;
    cin >> N >> Q;
    vec<int> A(N);
    rep(i, N) cin >> A[i];
    multiset<int> B, C;

    auto add = [&] (int i) {
        if (B.empty()) {
            B.insert(A[i]);
            return;
        }
        auto it = B.lower_bound(A[i]);
        if (it == B.end()) {
            C.insert(A[i] - *B.rbegin());
        } else if (it == B.begin()) {
            C.insert(*B.begin() - A[i]);
        } else {
            int r = *it, l = *--it;
            C.erase(C.find(r - l));
            C.insert(r - A[i]);
            C.insert(A[i] - l);
        }
        B.insert(A[i]);
    };
    auto del = [&] (int i) {
        if (B.size() == 1) {
            B.erase(A[i]);
            return;
        }
        B.erase(B.find(A[i]));
        auto it = B.lower_bound(A[i]);
        if (it == B.end()) {
            C.erase(C.find(A[i] - *B.rbegin()));
        } else if (it == B.begin()) {
            C.erase(C.find(*B.begin() - A[i]));
        } else {
            int r = *it, l = *--it;
            C.insert(r - l);
            C.erase(C.find(r - A[i]));
            C.erase(C.find(A[i] - l));
        }
    };

    vec<int> L(Q), R(Q), X(Q), T(Q);
    vec<int> ans(Q, -1);
    rep(i, Q) {
        int l, r, x;
        char c;
        cin >> l >> r >> x >> c;
        l--;
        L[i] = l;
        R[i] = r;
        X[i] = x;
        T[i] = (c == 'L');
    }

    auto query = [&] (int i) {
        if (T[i]) {
            auto it = C.upper_bound(X[i]);
            if (it != C.begin()) ans[i] = *--it;
        } else {
            auto it = C.lower_bound(X[i]);
            if (it != C.end()) ans[i] = *it;
        }
    };

    // Mo's
    // 初めて自分で書くけど、割と楽~
    int sq = max(1, (int)(N / sqrt(Q)));
    vec<int> idx(Q);
    ranges::iota(idx, 0);
    ranges::sort(idx, [&] (int i, int j) {
        if (L[i] / sq != L[j] / sq) return L[i] < L[j];
        return R[i] < R[j];
    });

    int l = 0, r = 0;
    for (auto i : idx) {
        while (l > L[i]) add(--l);
        while (r < R[i]) add(r++);
        while (l < L[i]) del(l++);
        while (r > R[i]) del(--r);
        query(i);
    }

    rep(i, Q) cout << ans[i] << endl;
}
0