結果

問題 No.865 24時間降水量
ユーザー totori_nyaatotori_nyaa
提出日時 2019-08-16 22:15:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 480 ms / 2,000 ms
コード長 3,624 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 176,452 KB
実行使用メモリ 14,728 KB
最終ジャッジ日時 2023-10-24 00:29:42
合計ジャッジ時間 4,658 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

// Segment Tree
template<class Monoid, class Action> struct SegTree {
    using FuncMonoid = function< Monoid(Monoid, Monoid) >;
    using FuncAction = function< void(Monoid&, Action) >;
    using FuncLazy = function< void(Action&, Action) >;
    FuncMonoid FM;
    FuncAction FA;
    FuncLazy FL;
    Monoid UNITY_MONOID;
    Action UNITY_LAZY;
    int SIZE, HEIGHT;
    vector<Monoid> dat;
    vector<Action> lazy;

    SegTree() { }
    SegTree(int n, const FuncMonoid fm, const FuncAction fa, const FuncLazy fl,
            const Monoid &unity_monoid, const Action &unity_lazy)
    : FM(fm), FA(fa), FL(fl), UNITY_MONOID(unity_monoid), UNITY_LAZY(unity_lazy) {
        SIZE = 1; HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, UNITY_MONOID);
        lazy.assign(SIZE * 2, UNITY_LAZY);
    }
    void init(int n, const FuncMonoid fm, const FuncAction fa, const FuncLazy fl,
            const Monoid &unity_monoid, const Action &unity_lazy) {
        FM = fm; FA = fa; FL = fl;
        UNITY_MONOID = unity_monoid; UNITY_LAZY = unity_lazy;
        SIZE = 1; HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, UNITY_MONOID);
        lazy.assign(SIZE * 2, UNITY_LAZY);
    }

    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE] = v; }
    void build() {
        for (int k = SIZE - 1; k > 0; --k)
            dat[k] = FM(dat[k*2], dat[k*2+1]);
    }

    /* update [a, b) */
    inline void evaluate(int k) {
        if (lazy[k] == UNITY_LAZY) return;
        if (k < SIZE) FL(lazy[k*2], lazy[k]), FL(lazy[k*2+1], lazy[k]);
        FA(dat[k], lazy[k]);
        lazy[k] = UNITY_LAZY;
    }
    inline void update(int a, int b, const Action &v, int k, int l, int r) {
        evaluate(k);
        if (a <= l && r <= b)  FL(lazy[k], v), evaluate(k);
        else if (a < r && l < b) {
        update(a, b, v, k*2, l, (l+r)>>1), update(a, b, v, k*2+1, (l+r)>>1, r);
        dat[k] = FM(dat[k*2], dat[k*2+1]);
        }
    }
    inline void update(int a, int b, const Action &v) { update(a, b, v, 1, 0, SIZE); }

    /* get [a, b) */
    inline Monoid get(int a, int b, int k, int l, int r) {
        evaluate(k);
        if (a <= l && r <= b)
            return dat[k];
        else if (a < r && l < b)
            return FM(get(a, b, k*2, l, (l+r)>>1), get(a, b, k*2+1, (l+r)>>1, r));
        else
            return UNITY_MONOID;
    }
    inline Monoid get(int a, int b) { return get(a, b, 1, 0, SIZE); }
    inline Monoid operator [] (int a) { return get(a, a+1); }
    /* debug */
    void print() {
        for (int i = 0; i < SIZE; ++i) { cout << (*this)[i]; if (i != SIZE) cout << ","; }
        cout << endl;
    }
};


signed main(){
    //cout << setprecision(16) ;
    ios::sync_with_stdio(false);
	cin.tie(0);

    int n;
    cin>>n;
    ll a[n];
    ll s[n]={};
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(i<24) s[0]+=a[i];
        else s[i-23] = s[i-24] + a[i] - a[i-24];
    }

    auto fm = [](long long a, long long b) { return max(a, b); };
    auto fa = [](long long &a, long long d) { a += d; };
    auto fl = [](long long &d, long long e) { d += e; };
    SegTree<long long, long long> seg(n+1, fm, fa, fl, 0, 0);
    for(int i=0;i<n;i++){
        seg.set(i,s[i]);
    }
    seg.build();

    int q;
    cin>>q;
    while(q--){
        ll t,v;
        cin>>t>>v;
        ll dif = v - a[t-1];
        a[t-1] = v;
        seg.update(max(0LL,t-24),t,dif);
        ll ans = seg.get(0,n);
        cout<<ans<<endl;
    }

}
0