結果

問題 No.876 Range Compress Query
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-09-07 14:17:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,475 bytes
コンパイル時間 1,350 ms
コンパイル使用メモリ 93,972 KB
実行使用メモリ 8,980 KB
最終ジャッジ日時 2023-09-08 17:13:57
合計ジャッジ時間 3,688 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,416 KB
testcase_01 WA -
testcase_02 AC 3 ms
5,476 KB
testcase_03 WA -
testcase_04 AC 3 ms
5,496 KB
testcase_05 AC 3 ms
5,780 KB
testcase_06 AC 3 ms
5,644 KB
testcase_07 AC 3 ms
5,656 KB
testcase_08 AC 3 ms
5,660 KB
testcase_09 AC 3 ms
5,444 KB
testcase_10 AC 3 ms
5,652 KB
testcase_11 AC 140 ms
8,616 KB
testcase_12 AC 118 ms
8,980 KB
testcase_13 AC 119 ms
8,592 KB
testcase_14 AC 139 ms
8,840 KB
testcase_15 AC 98 ms
8,840 KB
testcase_16 AC 134 ms
8,616 KB
testcase_17 AC 135 ms
8,592 KB
testcase_18 AC 141 ms
8,628 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;
    T zero = 0;
    T data[4*MAX_N];
    T values[4*MAX_N];

    RSQ(int m, T init_value=0){
        // 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;
        if(t > n-1) values[t-1] -= x;
        data[s-1] = values[s-1]?1:0;
        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+1);
    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