結果

問題 No.876 Range Compress Query
ユーザー veqccveqcc
提出日時 2019-09-08 19:58:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,181 bytes
コンパイル時間 1,070 ms
コンパイル使用メモリ 112,948 KB
実行使用メモリ 9,552 KB
最終ジャッジ日時 2023-09-09 22:37:03
合計ジャッジ時間 2,928 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 75 ms
8,984 KB
testcase_12 AC 62 ms
8,696 KB
testcase_13 AC 62 ms
9,100 KB
testcase_14 AC 75 ms
8,944 KB
testcase_15 AC 54 ms
9,072 KB
testcase_16 AC 74 ms
9,272 KB
testcase_17 AC 74 ms
9,552 KB
testcase_18 AC 78 ms
9,288 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;

template <typename T>
class SegmentTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T t;
    vector <T> dat;
public:
    SegmentTree() {};
    SegmentTree(F f, T t) : f(f), t(t) {}
    void init(int n_) {
        n = 1;
        while (n < n_) n <<= 1;
        dat.assign(n << 1, t);
    }
    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]);
    }
    void set_val(int k, T x) {
        dat[k += n] = x;
        while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }
    T query(int a, int b) {
        T vl = t, vr = t;
        for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};

typedef pair <ll, int> P;

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

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

    vector<ll> a(n);
    vector <P> b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (i) {
            b[i].first = a[i] - a[i - 1];
            b[i].second = b[i].first != 0;
        }
    }

    auto f = [](P a, P b) {
        return P(a.first, a.second + b.second);
    };

    SegmentTree<P> seg(f, P(-1, 0));
    seg.build(b);

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

        if (c == 1) {
            cin >> x;
            ll lv = seg.query(l - 1, l).first;
            ll rv = seg.query(r, r + 1).first;
            seg.set_val(l - 1, P(lv + x, lv + x != 0));
            seg.set_val(r, P(rv - x, rv - x != 0));
        } else {
            cout << seg.query(l, r).second + 1 << '\n';
        }
    }

    return 0;
}
0