답안 #513867

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
513867 2022-01-17T20:11:33 Z blue Sjeckanje (COCI21_sjeckanje) C++17
0 / 110
71 ms 70064 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
#define sz(x) int(x.size())

const int z = (1<<19);

int n, q;
vll a;

vi wt{0, +1, -1};

const ll INF = 1'000'000'000'000'000'000LL;

struct segtree
{
    // ll dp[z][3][3]; //0 = 0, 1 = +, 2 = -
    // ll lp[z];
    ll*** dp;
    ll* lp;

    void set_leaf(int i)
    {
        for(int u = 0; u <= 2; u++)
        {
            for(int v = 0; v <= 2; v++)
            {
                if(u != 0 && v != 0) dp[i][u][v] = -INF;
                else dp[i][u][v] = lp[i] * (wt[u] + wt[v]);
            }
        }
    }

    void recompute(int i)
    {
for(int u = 0; u <= 2; u++)
    for(int v = 0; v <= 2; v++)
        dp[i][u][v] = max(max({dp[2*i][u][0] + dp[2*i+1][0][v], dp[2*i][u][1] + dp[2*i+1][2][v], dp[2*i][u][2] + dp[2*i+1][1][v]})
                      + lp[i] * (wt[u] + wt[v]), -INF);

    }

    void build(int i, int l, int r)
    {

        if(l == r)
        {
            lp[i] = a[l];
            set_leaf(i);
        }
        else
        {
            build(2*i, l, (l+r)/2);
            build(2*i+1, (l+r)/2+1, r);
            recompute(i);
        }


        // cerr << "\n\n\n constructed node " << l << ' ' << r << " : \n";
        // for(int u = 0; u <= 2; u++)
        //     for(int v = 0; v <= 2; v++)
        //         cerr << u << ' ' << v << " : " << dp[i][u][v] << '\n';
    }

    void add(int i, int l, int r, int L, int R, ll V)
    {
        if(R < l || r < L) return;
        else if(L <= l && r <= R)
        {
            lp[i] += V;
            if(l == r) set_leaf(i);
            else recompute(i);
        }
        else
        {
            add(2*i, l, (l+r)/2, L, R, V);
            add(2*i+1, (l+r)/2+1, r, L, R, V);
            recompute(i);
        }
    }
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> q;
    a = vll(1+n);
    for(int i = 1; i <= n; i++) cin >> a[i];

    segtree S;

    S.lp = new ll[z];
    S.dp = new ll**[z];
    for(int u = 0; u < z; u++)
    {
        S.dp[u] = new ll*[3];
        for(int v = 0; v < 3; v++)
            S.dp[u][v] = new ll[3];
    }

    S.build(1, 1, n);

    // for(int u = 0; u <= 2; u++)
    //     for(int v = 0; v <= 2; v++)
    //         cerr << u << ' ' << v << " : " << S.dp[1][u][v] << '\n';



    for(int j = 1; j <= q; j++)
    {
        ll l, r, v;
        cin >> l >> r >> v;
        S.add(1, 1, n, l, r, v);


        // for(int u = 0; u <= 2; u++)
        //     for(int v = 0; v <= 2; v++)
        //         cerr << u << ' ' << v << " : " << S.dp[1][u][v] << '\n';

        cout << S.dp[1][0][0] << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 71 ms 70064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 71 ms 70064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 71 ms 70064 KB Output isn't correct
2 Halted 0 ms 0 KB -