Submission #1106001

#TimeUsernameProblemLanguageResultExecution timeMemory
1106001faricaSjeckanje (COCI21_sjeckanje)C++14
110 / 110
835 ms49296 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int MAX_N = 2e5; const int MOD = 998244353; struct Node { int borders[2], dp[2][2]; Node() { borders[0] = borders[1] = 0; dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = 0; } }; Node segm[4*MAX_N]; int a[MAX_N]; void calc(int pos, int l, int r) { Node ret; ret.borders[0] = segm[2*pos+1].borders[0]; ret.borders[1] = segm[2*pos+2].borders[1]; for(int l=0; l<2; ++l) for(int m=0; m<2; ++m) for(int o=0; o<2; ++o) for(int r=0; r<2; ++r) { if(o && m) { if((segm[2*pos+1].borders[1] < 0) == (segm[2*pos+2].borders[0] < 0)) ret.dp[l][r] = max(ret.dp[l][r], segm[2*pos+1].dp[l][m] + segm[2*pos+2].dp[o][r]); } else ret.dp[l][r] = max(ret.dp[l][r], segm[2*pos+1].dp[l][m] + segm[2*pos+2].dp[o][r]); } segm[pos] = ret; } void update(int pos, int l, int r, int x, int val) { if(l==r) { segm[pos].borders[0] += val; segm[pos].borders[1] += val; segm[pos].dp[1][1] = abs(segm[pos].borders[0]); return; } int m = (l+r)/2; if(x <= m) update(2*pos+1, l, m, x, val); else update(2*pos+2, m+1, r, x, val); calc(pos, l, r); } void build(int pos, int l, int r) { if(l==r) { segm[pos].borders[0] += a[l+1] - a[l]; segm[pos].borders[1] += a[l+1] - a[l]; segm[pos].dp[1][1] = abs(segm[pos].borders[0]); return; } int m = (l+r)/2; build(2*pos+1, l, m); build(2*pos+2, m+1, r); calc(pos, l, r); } void solve() { for(int i=0; i<4*MAX_N; ++i) { segm[i].borders[0] = segm[i].borders[1] = 0; for(int j=0; j<2; ++j) for(int k=0; k<2; ++k) segm[i].dp[j][k] = 0; } int n, q; cin >> n >> q; for(int i=0; i<n; ++i) { cin >> a[i]; } build(1, 0, n-2); while(q--) { int l, r, x; cin >> l >> r >> x; --l, --r; if(l) update(1, 0, n-2, l-1, x); if(r != n-1) update(1, 0, n-2, r, -x); cout << segm[1].dp[1][1] << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while(t--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...