結果

問題 No.876 Range Compress Query
ユーザー onigawara_kypronigawara_kypr
提出日時 2019-09-07 06:32:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 2,618 bytes
コンパイル時間 951 ms
コンパイル使用メモリ 106,400 KB
実行使用メモリ 8,068 KB
最終ジャッジ日時 2023-09-07 19:41:45
合計ジャッジ時間 3,420 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 60 ms
7,536 KB
testcase_12 AC 51 ms
7,208 KB
testcase_13 AC 51 ms
7,540 KB
testcase_14 AC 59 ms
7,720 KB
testcase_15 AC 44 ms
7,780 KB
testcase_16 AC 60 ms
8,068 KB
testcase_17 AC 60 ms
7,996 KB
testcase_18 AC 63 ms
8,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

template < typename T >
struct SegmentTree {
    vector<T> data;
    long long n;

    SegmentTree(vector<T> vec) {
        long long sz = vec.size();
        n=1;
        while(n<sz) n*=2;
        data.resize(2*n-1, 0);

        for (long long i=0; i<sz; i++) data[i+n-1] = vec[i];
        for (long long i=n-2; i>=0; i--) data[i] = data[2*i+1] + data[2*i+2];
    }

    void update(long long x, T val) {
        x += (n-1);
        data[x] = val;
        while(x > 0) {
            x = (x-1) / 2;
            data[x] = data[2*x+1] + data[2*x+2];
        }
    }

    T query(long long l, long long r) {
        T ret = 0;
        l += (n-1), r += (n-1);
        while (l < r) {
            if ((l & 1LL) == 0LL) ret += data[l];
            if ((r & 1LL) == 0LL) ret += data[r-1];
            l /= 2;
            r = (r-1) / 2;
        }
        return ret;
    }    
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, q; cin >> n >> q;
    vll a(n); REP(i, n) cin >> a[i];
    vll aa(n-1, 0);
    REP(i, n-1) aa[i] = a[i] - a[i+1];
    vll aaa(n-1, 0);
    REP(i, n-1) aaa[i] = aa[i]==0?0:1;
    SegmentTree<ll> seg(aaa);
    REP(_, q) {
        ll qi; cin >> qi;
        if (qi == 1) {
            ll l, r, x; cin >> l >> r >> x;
            l--;
            if (l > 0) {
                aa[l-1] -= x;
                aaa[l-1] = aa[l-1]==0?0:1;
                seg.update(l-1, aaa[l-1]);
            }
            if (r < n) {
                aa[r-1] += x;
                aaa[r-1] = aa[r-1]==0?0:1;
                seg.update(r-1, aaa[r-1]);
            }
        } else {
            ll l, r; cin >> l >> r;
            cout << seg.query(l-1, r-1)+1 << '\n';
        }
    }
    return 0;
}
0