結果

問題 No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに
ユーザー ruthen71ruthen71
提出日時 2022-10-15 00:38:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 719 ms / 6,000 ms
コード長 7,395 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 220,332 KB
実行使用メモリ 34,776 KB
最終ジャッジ日時 2024-06-26 18:37:07
合計ジャッジ時間 23,836 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 292 ms
23,516 KB
testcase_04 AC 559 ms
29,980 KB
testcase_05 AC 252 ms
17,952 KB
testcase_06 AC 469 ms
29,052 KB
testcase_07 AC 526 ms
30,592 KB
testcase_08 AC 511 ms
29,800 KB
testcase_09 AC 216 ms
16,924 KB
testcase_10 AC 206 ms
16,412 KB
testcase_11 AC 357 ms
19,708 KB
testcase_12 AC 357 ms
19,772 KB
testcase_13 AC 243 ms
16,936 KB
testcase_14 AC 253 ms
14,204 KB
testcase_15 AC 559 ms
31,956 KB
testcase_16 AC 441 ms
27,188 KB
testcase_17 AC 182 ms
10,612 KB
testcase_18 AC 697 ms
34,600 KB
testcase_19 AC 701 ms
34,596 KB
testcase_20 AC 704 ms
34,600 KB
testcase_21 AC 690 ms
34,600 KB
testcase_22 AC 696 ms
34,600 KB
testcase_23 AC 705 ms
34,600 KB
testcase_24 AC 713 ms
34,732 KB
testcase_25 AC 712 ms
34,728 KB
testcase_26 AC 707 ms
34,728 KB
testcase_27 AC 709 ms
34,732 KB
testcase_28 AC 716 ms
34,728 KB
testcase_29 AC 719 ms
34,600 KB
testcase_30 AC 702 ms
34,724 KB
testcase_31 AC 703 ms
34,732 KB
testcase_32 AC 705 ms
34,600 KB
testcase_33 AC 582 ms
34,728 KB
testcase_34 AC 581 ms
34,776 KB
testcase_35 AC 587 ms
34,728 KB
testcase_36 AC 581 ms
34,732 KB
testcase_37 AC 589 ms
34,728 KB
testcase_38 AC 75 ms
15,296 KB
testcase_39 AC 276 ms
16,144 KB
testcase_40 AC 450 ms
34,596 KB
testcase_41 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;


template <class T> struct lazy_sum_add {
    using S = std::pair<T, int>;
    using F = T;
    using value_type_S = S;
    using value_type_F = F;
    static constexpr S op(S a, S b) { return {a.first + b.first, a.second + b.second}; }
    static constexpr S e() { return {0, 0}; }
    static constexpr S mapping(F f, S x) { return {x.first + f * x.second, x.second}; }
    static constexpr F composition(F f, F g) { return f + g; }
    static constexpr F id() { return 0; }
};

template <class Lazy> struct lazy_segment_tree {
   public:
    using S = typename Lazy::value_type_S;
    using F = typename Lazy::value_type_F;
    lazy_segment_tree(int n) : lazy_segment_tree(std::vector<S>(n, Lazy::e())) {}
    lazy_segment_tree(const std::vector<S>& v) : _n((int)v.size()) {
        log = 0;
        while ((1U << log) < (unsigned int)(_n)) log++;
        size = 1 << log;
        d = std::vector<S>(size << 1, Lazy::e());
        lz = std::vector<F>(size, Lazy::id());
        for (int i = 0; i < _n; i++) d[i + size] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, const S& x) {
        assert(0 <= p and p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);  // 上から下へ伝搬
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);  // 下から上に更新
    }

    void chset(int p, const S& x) {
        assert(0 <= p and p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);  // 上から下へ伝搬
        d[p] = Lazy::op(d[p], x);
        for (int i = 1; i <= log; i++) update(p >> i);  // 下から上に更新
    }

    S operator[](int p) {
        assert(0 <= p and p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);  // 上から下へ伝搬
        return d[p];
    }

    S get(int p) {
        assert(0 <= p and p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);  // 上から下へ伝搬
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l and l <= r and r <= _n);
        if (l == r) return Lazy::e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = Lazy::e(), smr = Lazy::e();
        while (l < r) {
            if (l & 1) sml = Lazy::op(sml, d[l++]);
            if (r & 1) smr = Lazy::op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return Lazy::op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, const F& f) {
        assert(0 <= p and p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = Lazy::mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void apply(int l, int r, const F& f) {
        assert(0 <= l and l <= r and r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <class G> int max_right(int l, G& g) {
        assert(0 <= l && l <= _n);
        assert(g(Lazy::e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = Lazy::e();
        do {
            while ((l & 1) == 0) l >>= 1;
            if (!g(Lazy::op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l <<= 1;
                    if (g(Lazy::op(sm, d[l]))) {
                        sm = Lazy::op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = Lazy::op(sm, d[l]);
            l++;
        } while ((l & -l) != l);  // 2べきまたは0のときfalse
        return _n;
    }

    template <class G> int min_left(int r, G& g) {
        assert(0 <= r && r <= _n);
        assert(g(Lazy::e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = Lazy::e();
        do {
            r--;
            while (r > 1 && (r & 1)) r >>= 1;
            if (!g(Lazy::op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (r << 1) | 1;
                    if (g(Lazy::op(d[r], sm))) {
                        sm = Lazy::op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = Lazy::op(d[r], sm);
        } while ((r & -r) != r);  // 2べきまたは0のときfalse
        return 0;
    }

   private:
    int _n, log, size;
    std::vector<S> d;
    std::vector<F> lz;
    inline void update(int k) { d[k] = Lazy::op(d[k << 1], d[(k << 1) | 1]); }
    void all_apply(int k, const F& f) {
        d[k] = Lazy::mapping(f, d[k]);
        if (k < size) lz[k] = Lazy::composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(k << 1, lz[k]);
        all_apply((k << 1) | 1, lz[k]);
        lz[k] = Lazy::id();
    }
};

/**
 * @brief Lazy Segment Tree (遅延セグメント木)
 * @docs docs/data_structure/lazy_segment_tree.md
 */

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    V<ll> A(N), T(N);
    rep(i, N) cin >> A[i] >> T[i];
    int Q;
    cin >> Q;
    V<ll> D(Q);
    V<int> L(Q), R(Q);
    rep(i, Q) {
        cin >> D[i] >> L[i] >> R[i];
        L[i]--;
    }
    rep(i, N) {
        A[i] = A[i] + T[i] - 1;
        T[i]--;
    }
    auto calc = [](V<ll>& A, V<ll>& D, V<int>& L, V<int>& R) -> V<ll> {
        int Q = (int)D.size(), N = (int)A.size();
        V<tuple<ll, int, int>> event;
        rep(i, N) event.push_back({A[i], 1, i});
        rep(i, Q) event.push_back({D[i], 2, i});
        sort(event.begin(), event.end());
        V<pair<ll, int>> seginit(N);
        rep(i, N) seginit[i] = {A[i], 1};
        lazy_segment_tree<lazy_sum_add<ll>> seg(seginit);
        V<ll> ans(Q);
        ll las = 0;
        for (auto [t, qt, ind] : event) {
            seg.apply(0, N, -(t - las));
            las = t;
            if (qt == 1) {
                seg.set(ind, {seg[ind].first, 0});
            } else {
                ans[ind] = seg.prod(L[ind], R[ind]).first;
            }
        }
        return ans;
    };
    show(A, T);
    auto res1 = calc(A, D, L, R);
    auto res2 = calc(T, D, L, R);
    show(res1, res2);
    rep(i, Q) { cout << res1[i] - res2[i] << '\n'; }
    return 0;
}
0