結果

問題 No.876 Range Compress Query
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-09-06 23:11:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,798 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 97,716 KB
実行使用メモリ 9,420 KB
最終ジャッジ日時 2023-09-07 02:26:05
合計ジャッジ時間 4,130 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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}
};

template<typename T> class LazyRSQ {
    public:
    int n;
    T inf = INT_MAX;
    vector<T> data, lazy, f, f_lazy;
    
    LazyRSQ(int m, T init_value=INT_MAX){
        // 2のべき乗にする
        n = 1;
        while(n < m) n <<= 1;
        data.assign(2*n-1, init_value);
        lazy.assign(2*n-1, 0);
        f.assign(2*n-1, 0);
        for(int i=0;i<m;++i) {
            f[n-1+i] = 1;
        }
        inf = init_value;
    }

    void eval(int k, int kl, int kr){
        if(data[k] == inf) data[k] = 0;
        if(lazy[k] == 0) return;
        data[k] += lazy[k];
        if(kr - kl > 1){
            lazy[2*k+1] += lazy[k];
            lazy[2*k+2] += lazy[k];
        }
        lazy[k] = 0;
    }

    // [s,t)
    void add(int s, int t, T x, int k, int kl, int kr){
        eval(k, kl, kr);
        if(kr <= s || t <= kl) return;
        if(s <= kl && kr <= t){
            lazy[k] += x;
            eval(k, kl, kr);
            return;
        }
        int kc = (kl+kr)/2;
        add(s, t, x, 2*k+1, kl, kc);
        add(s, t, x, 2*k+2, kc, kr);
        f[k] = f[2*k+1] + f[2*k+2];
        if(data[n-1+kc-1] == data[n-1+kc]) --f[k];
    }

    void add(int s, int t, T x) {
        add(s, t, x, 0, 0, n);
    }

    // [s,t)
    T find(int s, int t, int k, int kl, int kr){
        eval(k, kl, kr);
        if(kr <= s || t <= kl) return 0;
        if(s <= kl && kr <= t) return f[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);
        T ret = vl + vr;
        if(data[n-1+kc-1] == data[n-1+kc]) --ret;
        f[k] = ret;
        // cerr << "find(" << kl << ", " << kr << ") = " << ret << endl; 
        return ret;
    }

    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;
    LazyRSQ<ll> tree(n);
    for(int i=0;i<n;++i) {
        cin >> a;
        tree.add(i, i+1, a);
    }

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

}
0