結果

問題 No.876 Range Compress Query
ユーザー merom686merom686
提出日時 2019-09-06 22:05:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 2,736 bytes
コンパイル時間 726 ms
コンパイル使用メモリ 79,916 KB
実行使用メモリ 9,576 KB
最終ジャッジ日時 2023-09-06 23:59:02
合計ジャッジ時間 2,867 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 120 ms
8,924 KB
testcase_12 AC 100 ms
8,728 KB
testcase_13 AC 99 ms
9,052 KB
testcase_14 AC 118 ms
9,040 KB
testcase_15 AC 85 ms
9,064 KB
testcase_16 AC 120 ms
9,324 KB
testcase_17 AC 118 ms
9,576 KB
testcase_18 AC 123 ms
9,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct SegmentTree {
    struct Node {
        Node operator*(const Node &o) const {
            if (l == 0) return o;
            return { l, o.r, f + o.f - (r == o.l) };
        }
        ll l, r;
        int f;
    };
    using Lazy = ll;

    SegmentTree(int n) : e({ 0, 0, 0 }), n(n) {
        for (m = 2; m < n; m *= 2);
        t = vector<Node>(m + n + n % 2, e);
        z = vector<Lazy>(m, 0);
    }
    Node &operator[](int i) {
        return t[m + i];
    }
    const Node &root() const {
        return t[1];
    }
    void build() {
        for (int i = (n - 1 + m) / 2; i > 0; i--) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    void update(int i, const Node &o) {
        i += m;
        for (int j = m; j >= 2; j /= 2) propagate(i / j, j);
        for (t[i] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    Node query(int l, int r, int i = 1, int il = 0, int ir = -1) {
        if (ir < 0) ir = m;
        if (l <= il && ir <= r) return t[i];
        propagate(i, ir - il);
        Node o = e;
        int ic = (il + ir) / 2;
        if (l < ic) o = o * query(l, r, i * 2 + 0, il, ic);
        if (ic < r) o = o * query(l, r, i * 2 + 1, ic, ir);
        return o;
    }
    void range_act(int l, int r, Lazy x, int i = 1, int il = 0, int ir = -1) {
        if (ir < 0) ir = m;
        if (l <= il && ir <= r) { act(i, ir - il, x); return; }
        propagate(i, ir - il);
        int ic = (il + ir) / 2;
        if (l < ic) range_act(l, r, x, i * 2 + 0, il, ic);
        if (ic < r) range_act(l, r, x, i * 2 + 1, ic, ir);
        t[i] = t[i * 2] * t[i * 2 + 1];
    }
    void propagate(int i, int w) {
        if (z[i] == 0) return;
        for (int j = 0; j < 2; j++) act(i * 2 + j, w / 2, z[i]);
        z[i] = 0;
    }
    void act(int i, int w, Lazy x) {
        t[i].l += x;
        t[i].r += x;
        if (i < m) z[i] += x;
    }
    vector<Node> t;
    vector<Lazy> z;
    const Node e;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    SegmentTree st(n);
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        st[i] = SegmentTree::Node({ a, a, 1 });
    }
    st.build();

    for (int h = 0; h < q; h++) {
        int t;
        cin >> t;

        if (t == 1) {
            int l, r, x;
            cin >> l >> r >> x;
            l--;

            st.range_act(l, r, x);

        } else {
            int l, r;
            cin >> l >> r;
            l--;

            cout << st.query(l, r).f << '\n';
        }
    }

    return 0;
}
0