This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
using namespace std;
const int N = 5 + 1e5;
const int inf = 7 + 1e9;
struct segment {
int n;
vector <ll> tree, a;
segment (int _n = 0) : n(_n) {
tree.assign(4 * n + 1, 0);
a.assign(4 * n + 1, 0);
}
void pull(int l, int r, int node, ll val) {
tree[node] += val * (r - l + 1);
a[node] += val;
}
void push(int l, int r, int node) {
if (l < r) {
int mid = (l + r) / 2;
pull(l, mid, 2 * node, a[node]);
pull(mid + 1, r, 2 * node + 1, a[node]);
}
a[node] = 0;
}
void update(int l, int r, int node, int u, int v, int val) {
if (r < u || v < l)
return;
if (u <= l && r <= v) {
pull(l, r, node, val);
return;
}
push(l, r, node);
int mid = (l + r) / 2;
update(l, mid, 2 * node, u, v, val);
update(mid + 1, r, 2 * node + 1, u, v, val);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
ll get(int l, int r, int node, int u) {
if (r < u || u < l)
return 0;
if (l == r)
return tree[node];
push(l, r, node);
int mid = (l + r) / 2;
return get(l, mid, 2 * node, u) + get(mid + 1, r, 2 * node + 1, u);
}
};
vector <int> a;
int main() {
#define TASKNAME ""
ios_base::sync_with_stdio (0);
cin.tie (0);
if ( fopen( TASKNAME".inp", "r" ) ) {
freopen (TASKNAME".inp", "r", stdin);
freopen (TASKNAME".out", "w", stdout);
}
int n, q;
ll s, t;
cin >> n >> q >> s >> t;
n ++;
a.assign(n + 1, 0);
segment f(n);
ll ans = 0;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
f.update(1, n, 1, i, i, a[i]);
int delta = abs(a[i - 1] - a[i]);
if (i > 1)
ans += (a[i - 1] < a[i] ? -s * delta : t * delta);
}
while (q --) {
int l, r;
ll x;
cin >> l >> r >> x;
l ++; r ++;
f.update(1, n, 1, l, r, x);
if (l > 1) {
ll u = f.get(1, n, 1, l - 1), v = f.get(1, n, 1, l), delta = abs(u - v);
if (u < v)
ans -= s * delta;
else
ans += t * delta;
delta = abs(u - v + x);
if (u < v - x)
ans += s * delta;
else
ans -= t * delta;
}
if (r < n) {
ll u = f.get(1, n, 1, r), v = f.get(1, n, 1, r + 1), delta = abs(u - v);
if (u < v)
ans -= s * delta;
else
ans += t * delta;
delta = abs(u - x - v);
if (u - x < v)
ans += s * delta;
else
ans -= t * delta;
}
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
foehn_phenomena.cpp: In function 'int main()':
foehn_phenomena.cpp:64:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen (TASKNAME".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
foehn_phenomena.cpp:65:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen (TASKNAME".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |