結果

問題 No.876 Range Compress Query
ユーザー rniyarniya
提出日時 2020-03-03 10:54:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 2,950 bytes
コンパイル時間 1,917 ms
コンパイル使用メモリ 174,348 KB
実行使用メモリ 14,464 KB
最終ジャッジ日時 2024-04-21 23:27:21
合計ジャッジ時間 4,350 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 165 ms
13,824 KB
testcase_12 AC 136 ms
13,312 KB
testcase_13 AC 141 ms
13,824 KB
testcase_14 AC 164 ms
13,824 KB
testcase_15 AC 121 ms
14,080 KB
testcase_16 AC 161 ms
14,208 KB
testcase_17 AC 163 ms
14,464 KB
testcase_18 AC 169 ms
14,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

template<typename Monoid,typename OperatorMonoid>
struct LazySegmentTree{
    typedef function<Monoid(Monoid,Monoid)> F;
    typedef function<Monoid(Monoid,OperatorMonoid)> G;
    typedef function<OperatorMonoid(OperatorMonoid,OperatorMonoid)> H;
    int n,hi;
    F f;
    G g;
    H h;
    Monoid id0;
    OperatorMonoid id1;
    vector<Monoid> dat;
    vector<OperatorMonoid> laz;
    LazySegmentTree(int n_,F f,G g,H h,Monoid id0,OperatorMonoid id1):
                    f(f),g(g),h(h),id0(id0),id1(id1){init(n_);}
    void init(int n_){
        n=1,hi=0;
        while(n<n_) n<<=1,++hi;
        dat.assign(n<<1,id0);
        laz.assign(n<<1,id1);
    }
    void build(const vector<Monoid> &v){
        for (int i=0;i<v.size();++i) dat[i+n]=v[i];
        for (int i=n-1;i;--i) dat[i]=f(dat[i<<1|0],dat[i<<1|1]);
    }
    inline Monoid reflect(int k){
        return laz[k]==id1?dat[k]:g(dat[k],laz[k]);
    }
    inline void propagate(int k){
        if (laz[k]==id1) return;
        laz[k<<1|0]=h(laz[k<<1|0],laz[k]);
        laz[k<<1|1]=h(laz[k<<1|1],laz[k]);
        dat[k]=reflect(k);
        laz[k]=id1;
    }
    inline void thrust(int k){
        for (int i=hi;i;--i) propagate(k>>i);
    }
    inline void recalc(int k){
        while(k>>=1) dat[k]=f(reflect(k<<1|0),reflect(k<<1|1));
    }
    void update(int a,int b,OperatorMonoid x){
        if (a>=b) return;
        thrust(a+=n),thrust(b+=n-1);
        for (int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if (l&1) laz[l]=h(laz[l],x),++l;
            if (r&1) --r,laz[r]=h(laz[r],x);
        }
        recalc(a),recalc(b);
    }
    void set_val(int k,Monoid x){
        thrust(k+=n);
        dat[k]=x,laz[k]=id1;
        recalc(k);
    }
    Monoid query(int a,int b){
        if (a>=b) return id0;
        thrust(a+=n),thrust(b+=n-1);
        Monoid vl=id0,vr=id0;
        for (int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if (l&1) vl=f(vl,reflect(l++));
            if (r&1) vr=f(reflect(--r),vr);
        }
        return f(vl,vr);
    }
    Monoid operator[](int i){
        return query(i,i+1);
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,Q; cin >> N >> Q;
    vector<ll> a(N);
    for (int i=0;i<N;++i) cin >> a[i];
    struct node{ll left,right,F;};
    auto f=[](node a,node b){
        if (a.F==-1) return b;
        if (b.F==-1) return a;
        return node{a.left,b.right
        ,a.F+b.F-(a.right==b.left)};
    };
    auto g=[](node a,ll x){return node{a.left+x,a.right+x,a.F};};
    auto h=[](ll a,ll b){return a+b;};
    LazySegmentTree<node,ll> seg(N,f,g,h,node{-1,-1,-1},0);
    vector<node> v(N);
    for (int i=0;i<N;++i) v[i]=node{a[i],a[i],1};
    seg.build(v);
    for (;Q--;){
        int c,l,r; ll x; cin >> c >> l >> r; --l;
        if (c==1){
            cin >> x;
            seg.update(l,r,x);
        } else cout << seg.query(l,r).F << '\n';
    }
}
0