結果

問題 No.876 Range Compress Query
ユーザー TAISA_TAISA_
提出日時 2019-09-06 22:24:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 2,703 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 171,048 KB
実行使用メモリ 4,528 KB
最終ジャッジ日時 2023-09-07 01:02:18
合計ジャッジ時間 4,200 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 132 ms
4,384 KB
testcase_12 AC 110 ms
4,528 KB
testcase_13 AC 108 ms
4,380 KB
testcase_14 AC 130 ms
4,508 KB
testcase_15 AC 89 ms
4,508 KB
testcase_16 AC 124 ms
4,384 KB
testcase_17 AC 125 ms
4,384 KB
testcase_18 AC 134 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T> bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
    for (int i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "\n" : " ");
    }
    return os;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
    for (auto &e : t) {
        fill_v(e, v);
    }
};

struct Segtree {
    int n;
    vector<int> dat;
    Segtree(int n_) {
        n = 1;
        while (n < n_) {
            n <<= 1;
        }
        dat.resize(2 * n, 0);
    }
    void upd(int k, int x) {
        k += n;
        dat[k] = x;
        k >>= 1;
        while (k > 0) {
            dat[k] = dat[k << 1] + dat[k << 1 | 1];
            k >>= 1;
        }
    }
    int get(int a, int b, int k, int l, int r) {
        if (b <= l || r <= a) {
            return 0;
        }
        if (a <= l && r <= b) {
            return dat[k];
        }
        return get(a, b, 2 * k, l, (l + r) / 2) +
               get(a, b, 2 * k + 1, (l + r) / 2, r);
    }
    int get(int a, int b) {
        if (a >= b) {
            return 0;
        }
        return get(a, b, 1, 0, n);
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    Segtree seg(n + 1);
    vector<int> a(n + 1);
    int bb = 0;
    for (int i = 0; i < n; i++) {
        int aa;
        cin >> aa;
        a[i] = aa - bb;
        seg.upd(i, (a[i] != 0));
        bb = aa;
    }
    while (q--) {
        int t, l, r;
        cin >> t >> l >> r;
        --l;
        --r;
        if (t == 1) {
            int x;
            cin >> x;
            a[l] += x;
            seg.upd(l, (a[l] != 0));
            r++;
            a[r] -= x;
            seg.upd(r, (a[r] != 0));
        } else {
            cout << seg.get(l + 1, r + 1) + 1 << endl;
        }
    }
}
0