Submission #889680

#TimeUsernameProblemLanguageResultExecution timeMemory
889680CookieSjeckanje (COCI21_sjeckanje)C++14
110 / 110
628 ms50664 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...