結果

問題 No.876 Range Compress Query
ユーザー veqccveqcc
提出日時 2019-09-06 22:14:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 3,412 bytes
コンパイル時間 1,158 ms
コンパイル使用メモリ 115,604 KB
実行使用メモリ 13,536 KB
最終ジャッジ日時 2023-09-07 00:40:03
合計ジャッジ時間 3,876 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 158 ms
13,276 KB
testcase_12 AC 127 ms
13,036 KB
testcase_13 AC 130 ms
13,008 KB
testcase_14 AC 155 ms
12,892 KB
testcase_15 AC 106 ms
13,140 KB
testcase_16 AC 147 ms
13,504 KB
testcase_17 AC 152 ms
13,456 KB
testcase_18 AC 164 ms
13,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
typedef pair <ll, ll> P;
typedef pair <ll, P> PP;

template <typename T, typename E>
class LazySegmentTree {
    using F = function<T(T, T)>;
    using G = function<T(T, E)>;
    using H = function<E(E, E)>;
    int n, height;
    F f;
    G g;
    H h;
    T t;
    E e;
    vector<T> dat;
    vector<E> laz;

public:
    LazySegmentTree(F f, G g, H h, T t, E e): f(f), g(g), h(h), t(t), e(e) {}

    void init(int n_) {
        n = 1;
        height = 0;
        while (n < n_) { n <<= 1; height++; }
        dat.assign(2 * n, t);
        laz.assign(2 * n, e);
    }

    void build(const vector<T> &v) {
        auto n_ = (int)v.size();
        init(n_);
        for (int i = 0; i < n_; i++) dat[n + i] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
    }

    inline T reflect(int k) {
        return laz[k] == e ? dat[k] : g(dat[k], laz[k]);
    }

    inline void eval(int k) {
        if (laz[k] == e) return;
        laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
        laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
        dat[k] = reflect(k);
        laz[k] = e;
    }

    inline void thrust(int k) {
        for (int i = height; i > 0; i--) {
            eval(k >> i);
        }
    }

    inline void recalc(int k) {
        while (k >>= 1) {
            dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
        }
    }

    void update(int a, int b, E x) {
        thrust(a += n);
        thrust(b += n - 1);
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) laz[l] = h(laz[l], x), l++;
            if (r & 1) --r, laz[r] = h(laz[r], x);
        }
        recalc(a);
        recalc(b);
    }

    void set_val(int a, T x) {
        thrust(a += n);
        dat[a] = x;
        laz[a] = e;
        recalc(a);
    }

    T query(int a, int b) {
        thrust(a += n);
        thrust(b += n - 1);
        T vl = t, vr = t;
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = f(vl, reflect(l++));
            if (r & 1) vr = f(reflect(--r), vr);
        }
        return f(vl, vr);
    }
};

struct Node {
    ll val, left, right;
};

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

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

    vector <Node> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].left;
        a[i].right = a[i].left;
        a[i].val = 1;
    }

    auto f = [](Node a, Node b) {
        ll v = a.val + b.val - 1;
        if (a.right && b.left && a.right != b.left) v++;
        return (Node){v, a.left, b.right};
    };
    auto g = [](Node a, ll b) { return (Node){a.val, a.left + b, a.right + b}; };
    auto h = [](ll a, ll b) { return a + b; };

    LazySegmentTree<Node, ll> seg(f, g, h, (Node){1, 0, 0}, 0);
    seg.build(a);

    while (q--) {
        ll c, l, r, x;
        cin >> c >> l >> r;

        if (c == 1) {
            cin >> x;
            seg.update(l - 1, r, x);
        } else {
            cout << seg.query(l - 1, r).val << '\n';
        }
    }

    return 0;
}
0