This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define __AVX__ 1
#define __AVX2__ 1
#define __SSE__ 1
#define __SSE2__ 1
#define __SSE2_MATH__ 1
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSE_MATH__ 1
#define __SSSE3__ 1
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
const int MAXN = 2e5 + 5;
template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }
int n, q;
ll s1, t;
ll a[MAXN], s[4 * MAXN], lazy[4 * MAXN];
void build(int id = 1, int l = 0, int r = n){
if((r - l) < 2){
s[id] = a[l];
return;
}
int mid = (l + r) / 2;
build(id << 1, l, mid);
build((id << 1) + 1, mid, r);
s[id] = (s[id << 1] + s[(id << 1) + 1]);
}
void down(int id, int l, int r){
if(lazy[id]){
s[id] += 1LL * lazy[id] * (r - l);
if((r - l) > 1){
lazy[id << 1] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
}
lazy[id] = 0;
}
void increase(int x, int y, ll u, int id = 1, int l = 0, int r = n){
//down(id, l, r);
if(x >= r || l >= y) return;
if(x <= l && r <= y){
lazy[id] += u;
down(id, l, r);
return;
}
int mid = (l + r) / 2;
increase(x, y, u, id << 1, l, mid);
increase(x, y, u, (id << 1) + 1, mid, r);
s[id] = (ll)(s[id << 1] + s[(id << 1) + 1]);
}
ll sum(int x, int y, int id = 1, int l = 0, int r = n){
down(id, l, r);
if(x >= r || l >= y) return 0;
if(x <= l && r <= y) return (ll)s[id];
int mid = (l + r) / 2;
return (ll)(sum(x, y, id * 2, l, mid) + sum(x, y, id * 2 + 1, mid, r));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
cin >> n >> q >> s1 >> t;
n++;
rep(i, n) cin >> a[i];
ll cur = 0;
repn(i, 1, n){
if(a[i] > a[i - 1]) cur -= (a[i] - a[i - 1]) * s1;
else cur += (a[i - 1] - a[i]) * t;
}
build();
rep(i, q){
int l, r;
ll x;
cin >> l >> r >> x;
//only need to take care of endpoints
if(l > 0){
//reverse the prev
if(sum(l, l + 1) > sum(l - 1, l)) cur += (sum(l, l + 1) - sum(l - 1, l)) * s1;
else cur -= (sum(l - 1, l) - sum(l, l + 1)) * t;
//increase(l, l + 1, x);
if((sum(l, l + 1)+x) > sum(l - 1, l)) cur -= ((sum(l, l + 1)+x) - sum(l - 1, l)) * s1;
else cur += (sum(l - 1, l) - (sum(l, l + 1)+x)) * t;
//increase(l, l + 1, -x);
}
if(r < (n - 1)){
if(sum(r + 1, r + 2) > sum(r, r + 1)) cur += (sum(r + 1, r + 2) - sum(r, r + 1)) * s1;
else cur -= (sum(r, r + 1) - sum(r + 1, r + 2)) * t;
//increase(r, r + 1, x);
if(sum(r + 1, r + 2) > (sum(r, r + 1)+x)) cur -= (sum(r + 1, r + 2) - (sum(r, r + 1)+x)) * s1;
else cur += ((sum(r, r + 1)+x) - sum(r + 1, r + 2)) * t;
//increase(r, r + 1, -x);
}
increase(l, r + 1, x);
cout << cur << endl;
}
return 0;
}
/*
Things to look out for:
- Integer overflows
- Array bounds
- Special cases
Be careful!
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |