Submission #889680

# Submission time Handle Problem Language Result Execution time Memory
889680 2023-12-20T05:05:19 Z Cookie Sjeckanje (COCI21_sjeckanje) C++14
110 / 110
628 ms 50664 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 2e5 + 1, mxq = 1e5 + 5, sq = 300, mxv = 1e6 + 5, pr = 31;
//const int base = (1 << 18);
const int inf = 2e9 + 5, neg = -69420;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int x[9] = {0, 1, 1, -1, -1, 2, -2, 2, -2};
const int y[9] = {0, 2, -2, 2, -2, 1, 1, -1, -1};
struct Node{
    ll dp[2][2] = {}, vall, valr;
};
Node st[4 * mxn + 1];
int n, q;
ll a[mxn + 1], dif[mxn + 1];
Node comb(Node a, Node b){
    Node res; res.vall = a.vall; res.valr = b.valr;
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 2; k++){
                for(int l = 0; l < 2; l++){
                    if(k != l || k == 0)res.dp[i][j] = max(res.dp[i][j], a.dp[i][k] + b.dp[l][j]);
                    if((a.valr >= 0) == (b.vall >= 0)){
                        res.dp[i][j] = max(res.dp[i][j], a.dp[i][k] + b.dp[l][j]);
                    }
                }
            }
        }
    }
    return(res);
}
void build(int nd, int l, int r){
    if(l == r){
        st[nd].vall = st[nd].valr = dif[l]; st[nd].dp[1][1] = abs(dif[l]);
        return;
    }
    int mid = (l + r) >> 1;
    build(nd << 1, l, mid); build(nd << 1 | 1, mid + 1, r);
    st[nd] = comb(st[nd << 1], st[nd << 1 | 1]);
}

void upd(int nd, int l, int r, int id, ll v){
    if(l == r){
        st[nd].vall = st[nd].valr = v; st[nd].dp[1][1] = abs(v);
        return;
    }
    int mid = (l + r) >> 1;
    if(id <= mid)upd(nd << 1, l, mid, id, v);
    else upd(nd << 1 | 1, mid + 1, r, id, v);
    st[nd] = comb(st[nd << 1], st[nd << 1 | 1]);
}
Node get(int nd, int l, int r, int ql, int qr){
    if(ql <= l && qr >= r)return(st[nd]);
    int mid = (l + r ) >> 1;
    if(qr <= mid)return(get(nd << 1, l, mid, ql, qr));
    else if(ql > mid)return(get(nd << 1 | 1, mid + 1,r, ql, qr));
    else return(comb(get(nd << 1, l, mid, ql, qr), get(nd << 1 | 1, mid + 1, r, ql, qr)));
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("villa.inp", "r", stdin);
    //freopen("villa.out", "w", stdout);
    cin >> n >> q;
    for(int i = 1; i <= n; i++)cin >> a[i];
    for(int i = 1; i < n; i++){
        dif[i] = a[i + 1] - a[i];
    }
    build(1, 1, n - 1);
    while(q--){
        int l, r, x; cin >> l >> r >> x;
        if(l != 1){
            dif[l - 1] += x;
            upd(1, 1, n - 1, l - 1, dif[l - 1]);
        }if(r != n){
            dif[r] -= x;
            upd(1, 1, n - 1, r, dif[r]);
        }
        ll ans = 0;
        forr(i, 0, 2){
            forr(j, 0, 2){
                ans = max(ans, st[1].dp[i][j]);
            }
        }
        cout << ans << "\n";
    }

    return(0);
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 40792 KB Output is correct
2 Correct 5 ms 40796 KB Output is correct
3 Correct 5 ms 40796 KB Output is correct
4 Correct 6 ms 40792 KB Output is correct
5 Correct 5 ms 40796 KB Output is correct
6 Correct 5 ms 40792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 40792 KB Output is correct
2 Correct 5 ms 40796 KB Output is correct
3 Correct 5 ms 40796 KB Output is correct
4 Correct 6 ms 40792 KB Output is correct
5 Correct 5 ms 40796 KB Output is correct
6 Correct 5 ms 40792 KB Output is correct
7 Correct 11 ms 41052 KB Output is correct
8 Correct 11 ms 41052 KB Output is correct
9 Correct 11 ms 41052 KB Output is correct
10 Correct 11 ms 41052 KB Output is correct
11 Correct 11 ms 41052 KB Output is correct
12 Correct 10 ms 41048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 40792 KB Output is correct
2 Correct 5 ms 40796 KB Output is correct
3 Correct 5 ms 40796 KB Output is correct
4 Correct 6 ms 40792 KB Output is correct
5 Correct 5 ms 40796 KB Output is correct
6 Correct 5 ms 40792 KB Output is correct
7 Correct 11 ms 41052 KB Output is correct
8 Correct 11 ms 41052 KB Output is correct
9 Correct 11 ms 41052 KB Output is correct
10 Correct 11 ms 41052 KB Output is correct
11 Correct 11 ms 41052 KB Output is correct
12 Correct 10 ms 41048 KB Output is correct
13 Correct 628 ms 50480 KB Output is correct
14 Correct 596 ms 50088 KB Output is correct
15 Correct 595 ms 50104 KB Output is correct
16 Correct 591 ms 50012 KB Output is correct
17 Correct 601 ms 50148 KB Output is correct
18 Correct 535 ms 50664 KB Output is correct