Submission #1121007

#TimeUsernameProblemLanguageResultExecution timeMemory
1121007steveonalexSjeckanje (COCI21_sjeckanje)C++17
110 / 110
549 ms55372 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return __lg(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n"){ for(auto item: container) cout << item << separator; cout << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const ll INF = 1e18 + 69; int normalise(ll x){ if (x == 0) return 0; if (x > 0) return 1; return -1; } struct SegmentTree{ struct Node{ ll f[2][2]; ll val_pref, val_suff; int len; Node(){ memset(f, 0, sizeof f); val_pref = val_suff = -INF; } void set_val(ll x){ f[0][0] = abs(x); val_pref = val_suff = x; } void add_val(ll x){ val_pref += x; val_suff += x; f[0][0] = abs(val_pref); } }; int n; vector<Node> a; SegmentTree(int _n){ n = _n; a.resize(n * 4 + 2); } Node combine(Node a, Node b){ Node c; c.len = a.len + b.len; c.val_pref = a.val_pref; c.val_suff = b.val_suff; for(int x = 0; x < min(2, c.len); ++x) for(int y = 0; y < min(2, c.len); ++y){ if (normalise(a.val_suff) * normalise(b.val_pref) != -1){ maximize(c.f[x][y], a.f[x][0] + b.f[0][y]); } else{ for(int i = 0; i <= 1; ++i) { int j = 1 - i; maximize(c.f[x][y], a.f[x][i] + b.f[j][y]); } } } for(int i = 1; i >= 1; --i) for(int j = 1; j >= 0; --j){ maximize(c.f[i-1][j], c.f[i][j]); } return c; } void build_tree(int l, int r, int id, vector<ll> &b){ if (l == r){ a[id].set_val(b[l]); a[id].len = 1; return; } int mid = (l + r) >> 1; build_tree(l, mid, id * 2, b); build_tree(mid + 1, r, id * 2 + 1, b); a[id] = combine(a[id * 2], a[id * 2 + 1]); } void update(int i, ll x, int l, int r, int id){ if (l == r){ a[id].add_val(x); return; } int mid = (l + r) >> 1; if (i <= mid) update(i, x, l, mid, id * 2); else update(i, x, mid + 1, r, id * 2 + 1); // cout << "Merge: " << l << " " << r << "\n"; a[id] = combine(a[id * 2], a[id * 2 + 1]); } void update(int i, ll x){ if (x == 0) return; // cout << "I am ICPC\n"; update(i, x, 1, n, 1); } ll get(){ return a[1].f[0][0]; } }; void solve(){ int n, q; cin >> n >> q; vector<ll> a(n+2); for(int i = 1; i<=n; ++i) cin >> a[i]; for(int i = n; i >= 1; --i) a[i] -= a[i-1]; a[1] = 0; SegmentTree st(n+1); st.build_tree(1, n+1, 1, a); while(q--){ int l, r, x; cin >> l >> r >> x; if (l > 1) st.update(l, x); if (r + 1 <= n) st.update(r + 1, -x); cout << st.get() << "\n"; } } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t start = clock(); solve(); cerr << "Time elapsed: " << clock() - start << "ms!\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...