結果

問題 No.876 Range Compress Query
ユーザー 👑 hitonanodehitonanode
提出日時 2019-10-23 22:59:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 2,109 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 171,076 KB
実行使用メモリ 5,032 KB
最終ジャッジ日時 2023-09-21 12:21:36
合計ジャッジ時間 3,667 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 121 ms
4,548 KB
testcase_12 AC 101 ms
4,380 KB
testcase_13 AC 100 ms
4,632 KB
testcase_14 AC 120 ms
4,580 KB
testcase_15 AC 83 ms
4,684 KB
testcase_16 AC 115 ms
4,820 KB
testcase_17 AC 115 ms
4,892 KB
testcase_18 AC 123 ms
5,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

// 1-indexed BIT (i : [1, len])
template<typename T>
struct BIT
{
    int len;
    vector<T> val;
    BIT(int num) : len(num), val(num + 1) {}
    BIT() : BIT(0) {}
    void reset() { fill(val.begin(), val.end(), 0); }
    void add(int pos, T v) { while (pos > 0 and pos <= len) val[pos] += v, pos += pos & -pos; }
    T sum(int pos) const // (0, pos]
    {
        T res = 0;
        while (pos > 0) res += val[pos], pos -= pos & -pos;
        return res;
    }
    T get(int pos) const
    {
        return sum(pos) - sum(pos - 1);
    }
};


int main()
{
    int N, Q;
    cin >> N >> Q;
    vector<lint> A(N);
    cin >> A;
    BIT<lint> bit_d(N + 1);
    bit_d.add(1, A[0]);
    REP(i, N - 1) bit_d.add(i + 2, A[i + 1] - A[i]);
    BIT<int> bit_n(N + 1);
    REP(i, N) if (bit_d.get(i + 1)) bit_n.add(i + 1, 1);
    REP(_, Q)
    {
        int q;
        cin >> q;
        if (q == 1)
        {
            int l, r, x;
            cin >> l >> r >> x;
            r++;

            if (bit_d.get(l)) bit_n.add(l, -1);
            bit_d.add(l, x);
            if (bit_d.get(l)) bit_n.add(l, 1);

            if (bit_d.get(r)) bit_n.add(r, -1);
            bit_d.add(r, -x);
            if (bit_d.get(r)) bit_n.add(r, 1);
        }
        else
        {
            int l, r;
            cin >> l >> r;
            int ret = bit_n.sum(r) - bit_n.sum(l - 1);
            if (bit_n.get(l) == 0) ret++;
            cout << ret << endl;
        }
    }
}
0