結果

問題 No.876 Range Compress Query
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-09-07 14:25:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 2,485 bytes
コンパイル時間 3,143 ms
コンパイル使用メモリ 94,444 KB
実行使用メモリ 9,200 KB
最終ジャッジ日時 2023-09-08 17:25:28
合計ジャッジ時間 3,877 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,432 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,572 KB
testcase_03 AC 3 ms
5,656 KB
testcase_04 AC 3 ms
5,416 KB
testcase_05 AC 2 ms
5,392 KB
testcase_06 AC 3 ms
5,380 KB
testcase_07 AC 3 ms
5,428 KB
testcase_08 AC 3 ms
5,524 KB
testcase_09 AC 3 ms
5,452 KB
testcase_10 AC 3 ms
5,432 KB
testcase_11 AC 142 ms
8,548 KB
testcase_12 AC 115 ms
8,504 KB
testcase_13 AC 117 ms
9,200 KB
testcase_14 AC 140 ms
8,844 KB
testcase_15 AC 99 ms
9,068 KB
testcase_16 AC 136 ms
8,716 KB
testcase_17 AC 135 ms
8,880 KB
testcase_18 AC 146 ms
8,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

const int MAX_N = 100000;

template<typename T> class RSQ {
    public:
    int n, m;
    T zero = 0;
    T data[4*MAX_N];
    T values[4*MAX_N];

    RSQ(int m, T init_value=0): m(m) {
        // 2のべき乗にする
        n = 1;
        while(n < m) n <<= 1;
        zero = init_value;
        for(int i=0;i<2*n-1;++i){
            data[i] = init_value;
            values[i] = init_value;
        }
    }

    void bottom_up_update(int i) {
        while(i){
            i = (i-1)/2;
            data[i] = data[2*i+1] + data[2*i+2];
        }
    }

    void update(int s, int t, T x){
        s += n-1;
        t += n-1;
        if(s > n-1) values[s-1] += x;
        data[s-1] = values[s-1]?1:0;
        if(t > n-1) values[t-1] -= x;
        data[t-1] = values[t-1]?1:0;
        
        bottom_up_update(s-1);
        bottom_up_update(t-1);

        // for(int i=0;i<2*n-1;++i) {
        //     cerr << data[i] << " ";
        // }
        // cerr << endl;
        // for(int i=n-1;i<2*n-1;++i) {
        //     cerr << values[i] << " ";
        // }
        // cerr << endl;
    }

    // [s, t) 
    T find(int s, int t, int k, int kl, int kr){
        if(kr <= s || t <= kl) return zero;
        if(s <= kl && kr <= t) return data[k];
        int kc = (kl+kr)/2;
        T vl = find(s, t, 2*k+1, kl, kc);
        T vr = find(s, t, 2*k+2, kc, kr);
        return vl + vr;
    }
    
    T find(int s, int t) {
        return find(s, t, 0, 0, n);
    }
};

int main() {
    std::ios::sync_with_stdio(false); cin.tie(0);
    int n, q, query, l, r;
    ll a;
    cin >> n >> q;
    RSQ<ll> tree(n);
    for(int i=0;i<n;++i) {
        cin >> a;
        tree.update(i, i+1, a);
    }
    

    for(int i=0;i<q;++i) {
        cin >> query >> l >> r;
        --l; --r;
        if(query == 1) {
            cin >> a;
            tree.update(l, r+1, a);
        } else {
            cout << tree.find(l, r) + 1 << endl;
        }
    }

}
0