結果

問題 No.876 Range Compress Query
ユーザー kyunakyuna
提出日時 2019-10-06 16:34:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 2,054 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 79,248 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 12:15:11
合計ジャッジ時間 3,375 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 207 ms
5,376 KB
testcase_12 AC 172 ms
5,376 KB
testcase_13 AC 173 ms
5,376 KB
testcase_14 AC 216 ms
5,376 KB
testcase_15 AC 152 ms
5,376 KB
testcase_16 AC 203 ms
5,376 KB
testcase_17 AC 205 ms
5,376 KB
testcase_18 AC 226 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;

template<typename Monoid>
struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;
    const F f;
    const Monoid M1;
    int sz;
    vector<Monoid> dat;
    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1), sz(1) {
        while (sz < n) sz <<= 1;
        dat.assign(sz * 2, M1);
    }
    void set(int k, const Monoid &x) { dat[k + sz] = x; }
    void build() {
        for (int k = sz - 1; k > 0; k--) {
            dat[k] = f(dat[2 * k], dat[2 * k + 1]);
        }
    }
    void update(int k, const Monoid &x) {
        dat[k += sz] = x;
        while (k >>= 1) dat[k] = f(dat[2 * k], dat[2 * k + 1]);
    }
    Monoid get(int a, int b) {  // [a, b)
        Monoid L = M1, R = M1;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = f(L, dat[a++]);
            if (b & 1) R = f(dat[--b], R);
        }
        return f(L, R);
    }
    Monoid operator[](const int &k) const { return dat[k + sz]; }
    friend ostream& operator<<(ostream& os, SegmentTree<Monoid> &seg) {
        for (int i = 0; i < seg.sz; i++) os << seg[i] << " ";
        return os;
    }
};

int main() {
    int n, q; cin >> n >> q;
    vector<int> a(n);
    for (int &ai: a) cin >> ai;
    vector<int> diff(n - 1);
    for (int i = 0; i < n - 1; i++) diff[i] = a[i] - a[i + 1];
    SegmentTree<int> seg(n - 1, [](int a, int b) { return a + b; }, 0);
    for (int i = 0; i < n - 1; i++) seg.set(i, diff[i] != 0);
    seg.build();
    while (q--) {
        int com; cin >> com;
        if (com == 1) {
            int l, r, x; cin >> l >> r >> x; l--, r--;
            l--;
            if (l >= 0) {
                diff[l] -= x; seg.update(l, diff[l] != 0);
            }
            if (r < n - 1) {
                diff[r] += x; seg.update(r, diff[r] != 0);
            }
        } else {
            int l, r; cin >> l >> r; l--; r--;
            cout << seg.get(l, r) + 1 << endl;
        }
    }
    return 0;
}
0