結果

問題 No.876 Range Compress Query
ユーザー toyamatoyama
提出日時 2019-09-06 22:41:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 490 ms / 2,000 ms
コード長 4,850 bytes
コンパイル時間 1,178 ms
コンパイル使用メモリ 99,080 KB
実行使用メモリ 11,652 KB
最終ジャッジ日時 2023-09-07 01:35:10
合計ジャッジ時間 5,886 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 469 ms
11,304 KB
testcase_12 AC 389 ms
11,396 KB
testcase_13 AC 391 ms
11,420 KB
testcase_14 AC 469 ms
11,348 KB
testcase_15 AC 338 ms
11,304 KB
testcase_16 AC 471 ms
11,652 KB
testcase_17 AC 468 ms
11,468 KB
testcase_18 AC 490 ms
11,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
template<class T, class Compare = less<T> >
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template<class T, class Compare = greater<T> >
using MinHeap = priority_queue<T, vector<T>, Compare>;
using llong = long long;

//===
template<typename Monoid, typename Laz>
struct LazySegmentTree {

    const function<Monoid(Monoid, Monoid)> mergeMonoid;
    const function<Monoid(Monoid, Laz, int)> applyLaz;
    const function<Laz(Laz, Laz)> mergeLaz;
    
    const Monoid e; // neutral element
    
    vector<Monoid> seg;
    vector<Laz> lazy;
    vector<bool> isUpdated;
    
    int size;
                                           
    LazySegmentTree(int nmemb, const Monoid &e,
                    function<Monoid(Monoid, Monoid)> f,
                    function<Monoid(Monoid, Laz, int)> g,
                    function<Laz(Laz, Laz)> h):
        e(e), mergeMonoid(f), applyLaz(g), mergeLaz(h)
    {
        size = 1;
        while (size < nmemb) {
            size *= 2;
        }

        seg.assign(2 * size - 1, e);
        isUpdated.assign(2 * size - 1, true);
        lazy.resize(2 * size - 1); 
    }

    inline void propagation(int k, int len) {
        if (!isUpdated[k]) {
            seg[k] = applyLaz(seg[k], lazy[k], len);
            if (len > 1) {
                if (isUpdated[2 * k + 1])
                    lazy[2 * k + 1] = lazy[k], isUpdated[2 * k + 1] = false;
                else
                    lazy[2 * k + 1] = mergeLaz(lazy[2 * k + 1], lazy[k]);
                
                if (isUpdated[2 * k + 2])
                    lazy[2 * k + 2] = lazy[k], isUpdated[2 * k + 2] = false;
                else 
                    lazy[2 * k + 2] = mergeLaz(lazy[2 * k + 2], lazy[k]);
            }
            isUpdated[k] = true;
        }
    }

    Monoid update(int k, int nl, int nr, int ql, int qr, Laz dat) {
        propagation(k, nr - nl);

        if (nr <= ql || qr <= nl) return seg[k];

        if (ql <= nl && nr <= qr) {
            lazy[k] = dat;
            isUpdated[k] = false;
            propagation(k, nr - nl);
            return seg[k];
        }
        else {
            seg[k] = mergeMonoid(update(2 * k + 1, nl, (nl + nr) / 2, ql, qr, dat),
                                 update(2 * k + 2, (nl + nr) / 2, nr, ql, qr, dat));
            return seg[k];
        }
    }

    // [l, r) <= dat
    void update(int l, int r, Laz dat) {
        update(0, 0, size, l, r, dat);
    }

    Monoid query(int k, int nl, int nr, int ql, int qr) {

        propagation(k, nr - nl);
        
        if (nr <= ql || qr <= nl) return e;

        if (ql <= nl && nr <= qr) return seg[k];
        else return mergeMonoid(query(2 * k + 1, nl, (nl + nr) / 2, ql, qr),
                                query(2 * k + 2, (nl + nr) / 2, nr, ql, qr));
    }

    // [l, r)
    Monoid query(int l, int r) { return query(0, 0, size, l, r); }

    Monoid operator [](const int &k) { return query(k, k + 1); }
};
//===

struct P {
    llong l, r;
    llong length;
    
    P (){}
    P (const P &m) { *this = m; };
    P (llong l, llong r, llong len):
        l(l), r(r), length(len) {};

    P operator + (const P &r) const {
        const P &l = *this;

        if (l.r == -1) return r;
        if (r.r == -1) return l;

        P ret;
        ret.l = l.l;
        ret.r = r.r;
        ret.length = l.length + r.length;
        if (l.r == r.l) ret.length--;
                
        return ret;
    };
};

llong n, q;
llong com, l, r;

int main() {
    cin >> n >> q;

    LazySegmentTree<P, llong> seg(n, P(-1, -1, 1),
                              [](P l, P r){
                                  return l + r;
                              },
                              [](P m, llong v, int len) {
                                  P ret;
                                  ret.l = m.l + v;
                                  ret.r = m.r + v;
                                  ret.length = m.length;

                                  return ret;
                              },
                              [](llong l, llong r) {
                                  return l + r;
                              });


    for (int i = 0; i < n; i++) {
        llong v;
        cin >> v;

        seg.update(i, i + 1, v + 1);
    }

    for (int i = 0; i < q; i++) {
        cin >> com >> l >> r;
        l--; r--;

        if (com == 1) {
            llong v;
            cin >> v;
            seg.update(l, r + 1, v);
        }
        else if (com == 2) {
            P a = seg.query(l, r + 1);

            cout << a.length << endl;
        }
    }

    return 0;
}
0