結果

問題 No.876 Range Compress Query
ユーザー rokahikou1rokahikou1
提出日時 2021-02-16 21:31:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,463 bytes
コンパイル時間 1,784 ms
コンパイル使用メモリ 172,696 KB
実行使用メモリ 5,716 KB
最終ジャッジ日時 2023-10-11 12:08:52
合計ジャッジ時間 5,514 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 126 ms
5,716 KB
testcase_12 AC 104 ms
5,088 KB
testcase_13 AC 105 ms
5,520 KB
testcase_14 AC 125 ms
5,412 KB
testcase_15 AC 87 ms
5,500 KB
testcase_16 AC 122 ms
5,564 KB
testcase_17 AC 120 ms
5,584 KB
testcase_18 AC 129 ms
5,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define ALL(v) (v).begin(), (v).end()
#define LLA(v) (v).rbegin(), (v).rend()
#define SZ(v) (v).size()
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    read(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    read(__VA_ARGS__)
#define DOUBLE(...)                                                            \
    double __VA_ARGS__;                                                        \
    read(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    read(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    read(__VA_ARGS__)
#define VEC(type, name, size)                                                  \
    vector<type> name(size);                                                   \
    read(name)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
    int to;
    T cost;
    edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;
template <class T> inline vector<T> make_vec(size_t a, T val) {
    return vector<T>(a, val);
}
template <class... Ts> inline auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}

void read() {}
template <class T> inline void read(T &a) { cin >> a; }
template <class T, class S> inline void read(pair<T, S> &p) {
    read(p.first), read(p.second);
}
template <class T> inline void read(vector<T> &v) {
    for(auto &a : v)
        read(a);
}
template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
    read(head), read(tail...);
}
template <class T> inline void chmax(T &a, T b) { (a < b ? a = b : a); }
template <class T> inline void chmin(T &a, T b) { (a > b ? a = b : a); }

/* SegmentTree(0-indexed) */
template <class Monoid> struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;
    const F f;
    const Monoid e;
    int n;

    vector<Monoid> dat;
    SegmentTree(int _n, const F _f, const Monoid &_e) : f(_f), e(_e) {
        n = 1;
        while(n < _n)
            n *= 2;
        dat.assign(2 * n, e);
    }

    SegmentTree(vector<Monoid> v, const F _f, const Monoid &_e) : f(_f), e(_e) {
        int sz = v.size();
        n = 1;
        while(n < sz)
            n *= 2;
        dat.resize(2 * n);
        for(int i = 0; i < sz; i++)
            set(i, v[i]);
        build();
    }

    void set(int k, Monoid a) { dat[k + n] = a; }

    void build() {
        for(int i = n - 1; i > 0; i--) {
            dat[i] = f(dat[2 * i], dat[2 * i + 1]);
        }
    }

    void update(int k, const Monoid &a) {
        k += n;
        dat[k] = a;
        while(k >>= 1) {
            dat[k] = f(dat[k * 2], dat[k * 2 + 1]);
        }
    }

    Monoid at(int a) { return query(a, a + 1); }

    // query for [a,b)
    Monoid query(int a, int b) {
        Monoid vl = e, vr = e;
        a += n, b += n;
        for(; a < b; a >>= 1, b >>= 1) {
            if(a & 1)
                vl = f(vl, dat[a++]);
            if(b & 1)
                vr = f(dat[--b], vr);
        }
        return f(vl, vr);
    }
};

// Range min Query
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=4865222
// template <class Monoid> struct RminQ : SegmentTree<Monoid> {
//     static auto f(Monoid a, Monoid b) { return min(a, b); }
//     RminQ(int n, const Monoid &e = numeric_limits<Monoid>::max())
//         : SegmentTree<Monoid>(n, f, e) {}
//     RminQ(vector<Monoid> v, const Monoid &e = numeric_limits<Monoid>::max())
//         : SegmentTree<Monoid>(v, f, e) {}
// };

// // Range sum Query
// // https://judge.yosupo.jp/submission/24445
// template <class Monoid> struct RsumQ : SegmentTree<Monoid> {
//     static auto f(Monoid a, Monoid b) { return a + b; }
//     RsumQ(int n, const Monoid &e = 0) : SegmentTree<Monoid>(n, f, e) {}
//     RsumQ(vector<Monoid> v, const Monoid &e = 0)
//         : SegmentTree<Monoid>(v, f, e) {}
// };

using S = int;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    INT(n, q);
    VEC(ll, a, n);
    vector<ll> b;
    b.push_back(INF);
    for(int i = 0; i < n - 1; i++) {
        b.push_back(a[i + 1] - a[i]);
    }
    b.push_back(INF);
    auto op = [](S a, S b) { return a + b; };
    S e = 0;
    SegmentTree<S> segtree(n + 1, op, e);
    rep(i, n + 1) {
        if(b[i] != 0)
            segtree.set(i, 1);
    }
    segtree.build();

    rep(i, q) {
        INT(qq);
        if(qq == 1) {
            INT(l, r, x);
            l--;
            b[l] += x;
            b[r] -= x;
            segtree.update(l, b[l] != 0);
            segtree.update(r, b[r] != 0);

        } else {
            INT(l, r);
            l--;
            cout << segtree.query(l, r) << endl;
        }
    }
}
0