結果

問題 No.631 Noelちゃんと電車旅行
ユーザー SugarDragon5SugarDragon5
提出日時 2018-08-09 20:32:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 485 ms / 2,000 ms
コード長 3,918 bytes
コンパイル時間 1,862 ms
コンパイル使用メモリ 172,944 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2024-04-10 07:54:49
合計ジャッジ時間 8,511 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 313 ms
5,248 KB
testcase_01 AC 485 ms
7,296 KB
testcase_02 AC 419 ms
7,296 KB
testcase_03 AC 427 ms
7,296 KB
testcase_04 AC 432 ms
7,296 KB
testcase_05 AC 427 ms
7,296 KB
testcase_06 AC 166 ms
6,940 KB
testcase_07 AC 84 ms
6,944 KB
testcase_08 AC 317 ms
7,552 KB
testcase_09 AC 377 ms
6,940 KB
testcase_10 AC 259 ms
7,424 KB
testcase_11 AC 234 ms
6,940 KB
testcase_12 AC 288 ms
7,296 KB
testcase_13 AC 290 ms
6,940 KB
testcase_14 AC 124 ms
6,944 KB
testcase_15 AC 235 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
template<typename Monoid,typename OperatorMonoid=Monoid>
struct LazySegTree{
    //0-indexed 閉区間
    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 size;
    vector<Monoid> data;
    vector<OperatorMonoid> lazy;
    const F f;  //要素同士の演算
    const G g;  //要素と作用素のマージ
    const H h;  //作用素同士のマージ
    const P p;  //作用素を下に降ろす
    const Monoid M1;    //単位元
    const OperatorMonoid OM0;   //作用素の単位元

    LazySegTree(int n,const F f,const G g,const H h,const P p,const Monoid &M1,const OperatorMonoid OM0)
        :f(f),g(g),h(h),p(p),M1(M1),OM0(OM0){
        size=1;
        while(size<n)size<<=1;
        data.assign(size*2-1,M1);
        lazy.assign(size*2-1,OM0);
    }

    void set(int k,const Monoid &x){
        //kをxに初期化
        //buildを忘れずに行うこと
        data[k+size-1]=x;
    }
    void build(){
        //構築 O(N)
        for(int k=size-2;k>=0;k--){
            data[k]=f(data[2*k+1],data[2*k+2]);
        }
    }
    void propagate(int k,int len){
        //長さlenのkから下へ伝播
        if(lazy[k]!=OM0){
            if(k<size-1){
                lazy[2*k+1]=h(lazy[2*k+1],lazy[k]);
                lazy[2*k+2]=h(lazy[2*k+2],lazy[k]);
            }
            data[k]=g(data[k],p(lazy[k],len));
            lazy[k]=OM0;
        }
    }

    Monoid update(int a,int b,const OperatorMonoid &x,int k,int l,int r){
        propagate(k,r-l+1);
        if(r<a||b<l){ //範囲外
            return data[k];
        }else if(a<=l&&b>=r){   //内部
            lazy[k]=h(lazy[k],x);
            propagate(k,r-l+1);
            return data[k];
        }else{
            return data[k]=f(update(a,b,x,2*k+1,l,(l+r)/2),
                    update(a,b,x,2*k+2,(l+r)/2+1,r));
        }
    }
    Monoid update(int a,int b,const OperatorMonoid &x){
        //[a,b]をxに変更
        return update(a,b,x,0,0,size-1);
    }

    Monoid query(int a,int b,int k,int l,int r){
        propagate(k,r-l+1);
        if(r<a||b<l){
            return M1;
        }else if(a<=l&&b>=r){
            return data[k];
        }else{
            return f(query(a,b,2*k+1,l,(l+r)/2),
                    query(a,b,2*k+2,(l+r)/2+1,r));
        }
    }
    Monoid query(int a,int b){
        return query(a,b,0,0,size-1);
    }

    Monoid operator[](const int &k){
        return query(k,k);
    }
    void debug(){
        for(int i=0;i<size;i++){
            if(i)cerr<<" ";cerr<<(*this)[i];
        }cerr<<endl;
    }
};
#define ll long long
#define FOR(i,n,m) for(int i=(n);i<(m);i++)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) for(int i=(n);i>=0;i--)
#define all(vec) vec.begin(),vec.end()
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using P=pair<ll,ll>;
using PP=pair<ll,P>;
using vp=vector<P>;
using vpp=vector<PP>;
using vs=vector<string>;
#define fi first
#define se second
#define pb push_back
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
const ll MOD=1000000007LL;
const int INF=1<<30;
const ll LINF=1LL<<60;

int main(){
    int n;
    cin>>n;
    n--;
    auto f=[](ll a,ll b){return max(a,b);};
    auto g=[](ll a,ll b){return a+b;};
    auto p=[](ll a,ll b){return a;};
    LazySegTree<ll> tr(n,f,g,g,p,0,0);
    REP(i,n){
        ll x;
        cin>>x;
        tr.set(i,x+(n-i)*3);
    }
    tr.build();
    int m;
    cin>>m;
    REP(i,m){
        int l,r;ll d;
        cin>>l>>r>>d;
        l--;r--;
        tr.update(l,r,d);
        cout<<tr.query(0,n-1)<<endl;
    }
    return 0;
}
0