Submission #674369

# Submission time Handle Problem Language Result Execution time Memory
674369 2022-12-23T20:48:59 Z badont Sjeckanje (COCI21_sjeckanje) C++17
0 / 110
1 ms 340 KB
#include<bits/stdc++.h>
using namespace std;

void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }

#ifdef LOCAL
#define debug(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define debug(...) "zzz"
#endif

using LL = long long;
using LD = long double;
using pii = pair<LL,LL>;

#define FOR(i, n) for(int i = 1; i<=n; i++)
#define F0R(i, n) for(int i = 0; i<n; i++)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; F0R(i, v.size()) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
template<typename A> ostream& operator<<(ostream &cout, array<A, 4> const &v) {cout << "["; F0R(i, v.size()) {if (i) cout << ", "; cout << v[i];} return cout << "]";}

struct Info {
	LL dp;
	//all the way, dp, max, min
	array<LL, 4> suffix;
	array<LL, 4> prefix;

	Info(LL g) : dp(0LL), suffix({true, 0, g, g}), prefix({true, 0, g, g}) {}
	void apply(LL g) {
		for (int i = 2; i < 4; i++) {
			this->suffix[i] += g;
			this->prefix[i] += g;
		}
	}

	Info() : Info(0) {}
};

struct Merge {
	Info operator()(const Info& a, const Info& b) {
		Info res(0);
		res.dp = a.dp + b.dp;
		//debug(b.suffix);
		res.suffix = b.suffix;
		res.prefix = a.prefix;
		res.suffix[0] = false;
		res.prefix[0] = false;

		//all the way, dp, max, min
		LL newVal = a.suffix[1] + b.prefix[1];
		newVal += max(a.suffix[2], b.prefix[2]);
		newVal -= min(a.suffix[3], b.prefix[3]);

		if (newVal > res.dp) {
			res.dp = newVal;
			if (b.prefix[0] == true) {
				res.suffix[0] = a.suffix[0];
				res.suffix[1] = a.suffix[1] + b.prefix[1];
				res.suffix[2] = max(a.suffix[2], b.prefix[2]);
				res.suffix[3] = min(a.suffix[3], b.prefix[3]);
			}
			if (a.suffix[0] == true) {
				res.prefix[0] = b.prefix[0];
				res.prefix[1] = b.prefix[1];
				res.prefix[2] = max(a.suffix[2], b.prefix[2]);
				res.prefix[3] = min(a.suffix[3], b.prefix[3]);
			}
		}


		return res;
	}
};

template<typename T, typename Merge = plus<T>>
struct lazy_seg {
	int n;
	vector<T> tr;
	vector<LL> lazy_inc;
	vector<bool> tag_inc;
	Merge merge;
	
	void pull(int z) {
		tr[z] = merge(tr[2 * z], tr[2 * z + 1]);
	};
	
	lazy_seg(int n) : n(n), tr(4 * n + 5), lazy_inc(4 * n + 5, 0LL), tag_inc(4 * n + 5), merge(Merge()) {}
	
	lazy_seg(vector<LL> a) : lazy_seg((int)a.size()) {
		function<void (int, int, int)> build = [&](int z, int l, int r) {
			if (l == r) {
				tr[z] = T(a[l]);
				return;
			}
			int m = (l + r) >> 1;
			build(2 * z, l, m);
			build(2 * z + 1, m + 1, r);
			pull(z);
		};
		build(1, 0, n - 1);
	}
	
	T f(int l, int r) {
		return r - l + 1; //change on operation
	}
	
	void push(int z, int l, int r) {
		if (tag_inc[z]) {
			tr[z].apply(lazy_inc[z]);
			if (l < r) F0R (i, 2) {					
				lazy_inc[2 * z + i] += lazy_inc[z];
				tag_inc[2 * z + i] = true;
			}
		}
		tag_inc[z] = false;
		lazy_inc[z] = 0LL;
	}
	
	void up_inc(int z, int l, int r, int ql, int qr, LL val) {
		push(z, l, r);
        if (ql > qr) return;     		
		if (ql == l && qr == r) {
			lazy_inc[z] += val;
			tag_inc[z] = true;
			push(z, l, r); 
			return;
		} 
		int m = (l + r) >> 1;
		up_inc(2 * z, l, m, ql, min(qr, m), val);
		up_inc(2 * z + 1, m + 1, r, max(ql, m + 1), qr, val);
		pull(z);
	};
	
	T query(int z, int l, int r, int ql, int qr) {
		push(z, l, r);
        if (ql > qr) return T(0);
		if (ql == l && qr == r) {
			return tr[z];
		}
		int m = (l + r) >> 1;
		return merge(
			query(2 * z, l, m, ql, min(qr, m)),
			query(2 * z + 1, m + 1, r, max(ql, m + 1), qr)
		);
	};
	
	void up_inc(int l, int r, LL val) { return up_inc(1, 0, n - 1, l, r, val); }
	T query(int l, int r) { return query(1, 0, n - 1, l, r); }
};


//var 
LL T;

void solve() {
	LL n, q;
	cin >> n >> q;
	vector<LL> a(n);
	for (auto& u : a)
		cin >> u;

	lazy_seg<Info, Merge> tree(a);
	while (q--) {
		LL l, r, x;
		cin >> l >> r >> x;
		l--; r--;
		tree.up_inc(l, r, x);
		cout << tree.tr[1].dp << "\n";
	}
}

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

	T = 1;
	//cin >> T;
	FOR(t, T)
		solve();

	cout.flush();
	return 0;
}


# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -