結果

問題 No.876 Range Compress Query
ユーザー NOSSNOSS
提出日時 2019-09-06 22:17:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 389 ms / 2,000 ms
コード長 3,995 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 180,676 KB
実行使用メモリ 13,468 KB
最終ジャッジ日時 2023-09-07 00:50:20
合計ジャッジ時間 6,014 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 375 ms
12,892 KB
testcase_12 AC 309 ms
12,620 KB
testcase_13 AC 308 ms
13,036 KB
testcase_14 AC 372 ms
12,940 KB
testcase_15 AC 252 ms
13,148 KB
testcase_16 AC 361 ms
13,468 KB
testcase_17 AC 359 ms
13,424 KB
testcase_18 AC 389 ms
13,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(1e5) + 5;
constexpr double EPS = 1e-8;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()



template <typename Monoid, typename OperatorMonoid>
struct LazySegmentTree{
private:
    using F = function<Monoid(Monoid, Monoid)>;  // 要素と要素を結合させる関数
    using G = function<Monoid(Monoid, OperatorMonoid)>;  // 要素に作用素を作用させる関数
    using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;  // 作用素と作用素を結合させる関数
    using P = function<OperatorMonoid(OperatorMonoid, int)>;  // 作用素を作用させる区間の長さによって変化させる関数
    int N;
    vector<Monoid> node;
    vector<OperatorMonoid> lazy;
    F f;
    G g;
    H h;
    P p;
    Monoid e;  // identity element
    OperatorMonoid oe;  // identity element

public:
    LazySegmentTree(){}
    LazySegmentTree(F f, G g, H h, Monoid e, OperatorMonoid oe, P p=[](OperatorMonoid a, int b){return a;}):f(f), g(g), h(h), e(e), oe(oe), p(p){}
    void init(int sz){
        N = 1;
        while(N < sz) N <<= 1;
        node.assign(2*N-1, e);
        lazy.assign(2*N-1, oe);
    }
    void build(vector<Monoid>& v){
        int sz = int(v.size());
        init(sz);
        for(int i=0; i<sz; i++){
            node[i+N-1] = v[i];
        }
        for(int i=N-2; i>=0; i--){
            node[i] = f(node[i*2+1], node[i*2+2]);
        }
    }
    void eval(int k, int len){
        if(lazy[k] != oe){
            node[k] = g(node[k], p(lazy[k], len));
            if(k < N-1){
                lazy[2*k+1] = h(lazy[2*k+1], lazy[k]);
                lazy[2*k+2] = h(lazy[2*k+2], lazy[k]);
            }
            lazy[k] = oe;
        }
    }
    Monoid update(int a, int b, OperatorMonoid x){return update(a, b, x, 0, 0, N);}
    Monoid update(int a, int b, OperatorMonoid x, int k, int l, int r){
        eval(k, r-l);
        if(b <= l || r <= a) return node[k];
        if(a <= l && r <= b){
            lazy[k] = h(lazy[k], x);
            return g(node[k], p(lazy[k], r-l));
        }
        return node[k] = f(update(a,b,x,2*k+1,l,(l+r)/2), update(a,b,x,k*2+2,(l+r)/2,r));
    }
    // [a,b)
    Monoid query(int a, int b){return query(a, b, 0, 0, N);}
    Monoid query(int a, int b, int k, int l, int r){
        eval(k, r-l);
        if(b <= l || r <= a) return e;
        if(a <= l && r <= b) return node[k];
        Monoid vl, vr;
        vl = query(a, b, 2*k+1, l, (l+r)/2);
        vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main() {
    int n, q;
    cin >> n >> q;
    vector<P3> a(n);
    for(int i=0;i<n;i++){
        ll x;
        cin >> x;
        a[i] = P3(0,{x,x});
    }
    auto f = [](P3 x, P3 y){
        return P3(x.first+y.first+((x.second.second != y.second.first && x.second.second != -1 && y.second.first != -1) ? 1:0),{x.second.first, y.second.second});
    };
    auto g = [](P3 x, ll y){
        ll lhs = (x.second.first!=-1 ? x.second.first+y : -1);
        ll rhs = (x.second.second!=-1 ? x.second.second+y : -1);
        return P3(x.first,{lhs,rhs});
    };
    auto h = [](ll x, ll y){return x+y;};
    LazySegmentTree<P3,ll> seg(f,g,h,P3(0,{-1,-1}),0);
    seg.build(a);
    for(int i=0;i<q;i++){
        ll c, l ,r, x;
        cin >> c >> l >> r;
        l--;
        if(c == 1){
            cin >> x;
            seg.update(l,r,x);
        }
        else{
            cout << seg.query(l,r).first+1 << endl;
        }
    }
    return 0;
}
0