結果

問題 No.1375 Divide and Update
ユーザー milanis48663220milanis48663220
提出日時 2021-02-05 22:17:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 396 ms / 2,000 ms
コード長 3,024 bytes
コンパイル時間 1,029 ms
コンパイル使用メモリ 101,252 KB
実行使用メモリ 17,536 KB
最終ジャッジ日時 2023-09-15 08:33:16
合計ジャッジ時間 7,021 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 349 ms
16,668 KB
testcase_11 AC 314 ms
16,384 KB
testcase_12 AC 172 ms
9,828 KB
testcase_13 AC 145 ms
9,416 KB
testcase_14 AC 392 ms
17,456 KB
testcase_15 AC 393 ms
17,344 KB
testcase_16 AC 393 ms
17,536 KB
testcase_17 AC 396 ms
17,440 KB
testcase_18 AC 394 ms
17,420 KB
testcase_19 AC 395 ms
17,360 KB
testcase_20 AC 382 ms
17,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;


template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

const ll INF = 1e15;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    ll x, y; cin >> x >> y;
    vector<ll> a(n);
    vector<ll> cumsum_a(n+1);
    auto calc_min = [](ll a, ll b){ return min(a, b); };
    auto calc_max = [](ll a, ll b){ return max(a, b); };
    segtree<ll> sgtl(n+1, INF, calc_min);
    segtree<ll> sgtr(n+1, -INF, calc_max);
    for(int i = 0; i < n; i++) cin >> a[i];
    ll sum_all = accumulate(a.begin(), a.end(), (ll)0);
    for(int i = 0; i < n; i++){
        cumsum_a[i+1] = cumsum_a[i]+x-a[i];
    }
    for(int i = 0; i < n+1; i++) sgtl.update(i, cumsum_a[i]);
    vector<ll> max_l(n+1), max_r(n+1);
    max_l[0] = x-a[0];
    for(int i = 1; i < n; i++){
        max_l[i] = max(max_l[i-1], cumsum_a[i+1]-sgtl.query(0, i+1));
    }
     for(int i = 0; i < n; i++) {
        cumsum_a[i+1] = cumsum_a[i]+y-a[i];
    }
    for(int i = 0; i < n+1; i++) sgtr.update(i, cumsum_a[i]);
    max_r[n-1] = y-a[n-1];
    for(int i = n-2; i >= 0; i--){
        max_r[i] = max(max_r[i+1], sgtr.query(i+1, n+1)-cumsum_a[i]);
    }
    for(int i = 1; i <= n-2; i++){
        // cout << max_l[i-1] << ' ' << max_r[i+1] << endl;
        cout << sum_all+max_l[i-1]+max_r[i+1] << endl;
    }
} 
0