Submission #627121

# Submission time Handle Problem Language Result Execution time Memory
627121 2022-08-12T08:25:56 Z BackNoob Sjeckanje (COCI21_sjeckanje) C++14
110 / 110
724 ms 55392 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
 
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
 

int n;
int q;
ll a[mxN];
ll dp[mxN][3];

struct IT{
	struct Node{
		ll f[2][2];
		ll lef;
		ll rig;
	};

	int n;
	vector<Node> t;
	vector<ll> lz;
	IT(){}
	IT(int _n) {
		n = _n;
		t.resize(n * 4 + 7);
		lz.resize(n * 4 + 7, 0);
	}

	Node merge_node(Node &x, Node &y)
	{
		Node res;
		res.lef = x.lef;
		res.rig = y.rig;
		for(int i = 0; i < 2; i++)
		for(int j = 0; j < 2; j++) res.f[i][j] = -infll;

		for(int a = 0; a < 2; a++)
		for(int b = 0; b < 2; b++) {
			if(x.f[a][b] == -infll) continue;
			for(int c = 0; c < 2; c++)
			for(int d = 0; d < 2; d++) {
				if(y.f[c][d] == -infll) continue;
				ll val = x.f[a][b] + y.f[c][d];
				if(b == c) {
					if(b == 0) {
						if(x.rig < y.lef) val += y.lef - x.rig;
					}
					else {
						if(x.rig > y.lef) val += x.rig - y.lef;
					}

				}
				maximize(res.f[a][d], val);
			}
		}
		return res;
	}

	void build(int v, int tl, int tr)
	{
		if(tl == tr) {
			t[v].lef = t[v].rig = a[tl];
			for(int i = 0; i < 2; i++)
			for(int j = 0; j < 2; j++) 
				if(i != j) t[v].f[i][j] = -infll;
				else t[v].f[i][j] = 0;
		}
		else {
			int tm = (tl + tr) / 2;
			build(v * 2, tl, tm);
			build(v * 2 + 1, tm + 1, tr);
			t[v] = merge_node(t[v * 2], t[v * 2 + 1]);
		}
	}

	void push_down(int v)
	{
		t[v * 2].lef += lz[v];
		t[v * 2].rig += lz[v];
		t[v * 2 + 1].lef += lz[v];
		t[v * 2 + 1].rig += lz[v];
		lz[v * 2] += lz[v];
		lz[v * 2 + 1] += lz[v];
		lz[v] = 0;
	}
	void update(int v, int tl, int tr, int l, int r, int val)
	{
		if(l > r) return;
		if(tl == l && tr == r) {
			t[v].lef += val;
			t[v].rig += val;
			lz[v] += val;
		}
		else {
			push_down(v);
			int tm = (tl + tr) / 2;
			update(v * 2, tl, tm, l, min(r, tm), val);
			update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
			t[v] = merge_node(t[v * 2], t[v * 2 + 1]);
		}
	}

	void update(int l, int r, int val)
	{
		update(1, 1, n, l, r, val);
	}
} seg;



 
void solve()
{
	cin >> n >> q;
	for(int i = 1; i <= n; i++) cin >> a[i];

	seg = IT(n);
	seg.build(1, 1, n);
	while(q--) {
		int l, r, x;
		cin >> l >> r >> x;

		seg.update(l, r, x);
		ll res = -infll;
		for(int i = 0; i < 2; i++)
		for(int j = 0; j < 2; j++) maximize(res, seg.t[1].f[i][j]);
		cout << res << endl;
	}
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

 
    int tc = 1;
    //cin >> tc;
    while(tc--) {
    	solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 7 ms 1104 KB Output is correct
8 Correct 7 ms 1108 KB Output is correct
9 Correct 10 ms 1108 KB Output is correct
10 Correct 6 ms 1104 KB Output is correct
11 Correct 7 ms 1068 KB Output is correct
12 Correct 6 ms 1160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 7 ms 1104 KB Output is correct
8 Correct 7 ms 1108 KB Output is correct
9 Correct 10 ms 1108 KB Output is correct
10 Correct 6 ms 1104 KB Output is correct
11 Correct 7 ms 1068 KB Output is correct
12 Correct 6 ms 1160 KB Output is correct
13 Correct 724 ms 54892 KB Output is correct
14 Correct 658 ms 54912 KB Output is correct
15 Correct 655 ms 54868 KB Output is correct
16 Correct 715 ms 54848 KB Output is correct
17 Correct 695 ms 54960 KB Output is correct
18 Correct 653 ms 55392 KB Output is correct