Submission #46644

#TimeUsernameProblemLanguageResultExecution timeMemory
46644maksim_gaponovFoehn Phenomena (JOI17_foehn_phenomena)C++14
30 / 100
1050 ms11904 KiB
#define _CRT_SECURE_NO_WARNINGS
#ifdef _DEBUG
#define FILE_IN "input.txt"
#define FILE_OUT "output.txt"
#endif
#include <iostream>
#include <cstdlib>
#include <climits>
#include <set>
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll;

void openFiles() {
#ifdef _DEBUG
	assert(freopen(FILE_IN, "r", stdin));
	assert(freopen(FILE_OUT, "w", stdout));
#endif
}

vector<ll> a;
const int MAXN = 200000 + 10;
ll t[4 * MAXN];
ll add[4 * MAXN];
int n, q, s_up, s_down;

void push(int x, int l, int r) {
	t[x] += (r - l) * add[x];
	if (r - l > 1) {
		add[2 * x + 1] += add[x];
		add[2 * x + 2] += add[x];
	}
	add[x] = 0;
}

void update(int x, int l, int r, int ql, int qr, ll val) {
	push(x, l, r);
	if (l >= qr || r <= ql)
		return;
	if (ql <= l && r <= qr) {
		add[x] += val;
		push(x, l, r);
		return;
	}
	int c = (l + r) / 2;
	update(2 * x + 1, l, c, ql, qr, val);
	update(2 * x + 2, c, r, ql, qr, val);
	t[x] = t[2 * x + 1] + t[2 * x + 2];
}

ll query(int x, int l, int r, int ql, int qr) {
	push(x, l, r);
	if (l >= qr || r <= ql)
		return 0LL;
	if (ql <= l && r <= qr) {
		return t[x];
	}
	int c = (l + r) / 2;
	return (
		query(2 * x + 1, l, c, ql, qr) +
		query(2 * x + 2, c, r, ql, qr)
	);
}

ll get_a(int i) {
	return query(0, 0, n, i, i + 1);
}

ll sign(ll f) {
	if (f == 0)
		return 1;
	return (f) / abs(f);
}

int main() {
	openFiles();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> q >> s_up >> s_down;
	n++;
	a.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		update(0, 0, n, i, i + 1, a[i]);
	}
	ll up_cnt = 0, down_cnt = 0;
	for (int i = 1; i < n; i++) {
		if (a[i] > a[i - 1]) {
			up_cnt += a[i] - a[i - 1];
		}
		else {
			down_cnt += a[i - 1] - a[i];
		}
	}
	for (int j = 0; j < q; j++) {
		/*for (int i = 0; i < n; i++)
			cout << get_a(i) << " ";
		cout << "\n";*/
		int l, r;
		ll x;
		cin >> l >> r >> x;
		ll left_dist = get_a(l) - get_a(l - 1);
		ll right_dist;
		if (r != n - 1) {
			right_dist = get_a(r + 1) - get_a(r);
		}
		update(0, 0, n, l, r + 1, x);
		ll new_left_dist = get_a(l) - get_a(l - 1);
		ll new_right_dist;
		if (r != n - 1) {
			new_right_dist = get_a(r + 1) - get_a(r);
		}
		if (sign(new_left_dist) == sign(left_dist)) {
			if (sign(left_dist) == 1) {
				up_cnt += x;
			}
			else {
				down_cnt -= x;
			}
		}
		else {
			if (sign(left_dist) == 1) {
				up_cnt -= left_dist;
				down_cnt += -new_left_dist;
			}
			else {
				down_cnt -= -left_dist;
				up_cnt += new_left_dist;
			}
		}
		if (r != n - 1) {
			if (sign(new_right_dist) == sign(right_dist)) {
				if (sign(right_dist) == 1) {
					up_cnt -= x;
				}
				else {
					down_cnt += x;
				}
			}
			else {
				if (sign(right_dist) == 1) {
					up_cnt -= right_dist;
					down_cnt += -new_right_dist;
				}
				else {
					down_cnt -= -right_dist;
					up_cnt += new_right_dist;
				}
			}
		}
		ll res = down_cnt * s_down - up_cnt * s_up;
		cout << res << "\n";
	}
	return 0;
}

Compilation message (stderr)

foehn_phenomena.cpp: In function 'int main()':
foehn_phenomena.cpp:80:20: warning: 'new_right_dist' may be used uninitialized in this function [-Wmaybe-uninitialized]
  return (f) / abs(f);
                    ^
foehn_phenomena.cpp:118:6: note: 'new_right_dist' was declared here
   ll new_right_dist;
      ^~~~~~~~~~~~~~
foehn_phenomena.cpp:155:15: warning: 'right_dist' may be used uninitialized in this function [-Wmaybe-uninitialized]
      down_cnt -= -right_dist;
      ~~~~~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...