結果

問題 No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに
ユーザー ruthenruthen
提出日時 2022-10-15 00:38:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 690 ms / 6,000 ms
コード長 7,395 bytes
コンパイル時間 2,718 ms
コンパイル使用メモリ 219,272 KB
実行使用メモリ 36,412 KB
最終ジャッジ日時 2023-09-09 01:21:02
合計ジャッジ時間 27,943 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 269 ms
23,360 KB
testcase_04 AC 501 ms
30,640 KB
testcase_05 AC 244 ms
18,552 KB
testcase_06 AC 443 ms
30,236 KB
testcase_07 AC 507 ms
30,644 KB
testcase_08 AC 496 ms
30,652 KB
testcase_09 AC 205 ms
16,856 KB
testcase_10 AC 199 ms
16,836 KB
testcase_11 AC 347 ms
19,544 KB
testcase_12 AC 353 ms
19,436 KB
testcase_13 AC 232 ms
16,920 KB
testcase_14 AC 255 ms
14,540 KB
testcase_15 AC 535 ms
32,892 KB
testcase_16 AC 403 ms
27,028 KB
testcase_17 AC 177 ms
10,376 KB
testcase_18 AC 667 ms
35,096 KB
testcase_19 AC 670 ms
34,636 KB
testcase_20 AC 668 ms
35,960 KB
testcase_21 AC 672 ms
34,628 KB
testcase_22 AC 674 ms
34,632 KB
testcase_23 AC 669 ms
35,300 KB
testcase_24 AC 676 ms
34,780 KB
testcase_25 AC 668 ms
34,760 KB
testcase_26 AC 678 ms
35,660 KB
testcase_27 AC 679 ms
36,412 KB
testcase_28 AC 684 ms
34,776 KB
testcase_29 AC 690 ms
35,936 KB
testcase_30 AC 672 ms
35,360 KB
testcase_31 AC 647 ms
35,044 KB
testcase_32 AC 662 ms
35,132 KB
testcase_33 AC 558 ms
35,096 KB
testcase_34 AC 559 ms
34,776 KB
testcase_35 AC 551 ms
35,056 KB
testcase_36 AC 557 ms
35,496 KB
testcase_37 AC 568 ms
35,236 KB
testcase_38 AC 75 ms
15,824 KB
testcase_39 AC 261 ms
16,056 KB
testcase_40 AC 428 ms
35,820 KB
testcase_41 AC 2 ms
4,384 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