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 <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
struct SegmentTree{
int n, N;
vector<ll> T, lz;
SegmentTree(int n) : n(n){
N = 1;
while(N < n) N*=2;
T.resize(N*2+1, 0);
lz.resize(N*2+1, 0);
};
void down(int node){
for(int child = node*2; child <= node*2+1; child++){
if(child >= 2*N) continue;
lz[child] += lz[node];
}
T[node] += lz[node];
lz[node] = 0;
}
void upd(int l, int r, ll x){
auto upd = [&](auto upd, int node, int low, int high, int l, int r, ll x) -> void{
down(node);
if(low > r || l > high) return;
if(low >= l && r >= high){
lz[node] += x;
down(node);
return;
}
int mid = (low+high)/2;
upd(upd, node*2, low, mid, l, r, x);
upd(upd, node*2+1, mid+1, high, l, r, x);
};
upd(upd, 1, 1, N, l, r, x);
};
};
void solve(){
ll n, q, s, t;
cin >> n >> q >> s >> t;
vector<ll> a(n+1);
for(int i = 0; i <= n; i++){
cin >> a[i];
}
SegmentTree T(n+1);
for(int i = 1; i <= n; i++){
T.upd(i, i, a[i]);
}
ll cur = 0;
for(int i = 1; i <= n; i++){
if(a[i] > a[i-1]){
cur += -s*(a[i]-a[i-1]);
}else{
cur += t*(a[i-1]-a[i]);
}
}
for(int i = 1; i <= q; i++){
ll l, r, x;
cin >> l >> r >> x;
//get the correct value
T.upd(l-1, l-1, 0);
T.upd(l, l, 0);
T.upd(r+1, r+1, 0);
T.upd(r, r, 0);
int N = T.N;
ll l1, l2 = T.T[l+N-1], r1 = T.T[r+N-1], r2;
if(l == 1){
l1 = 0;
}else{
l1 = T.T[l-1+N-1];
}
if(r == n){
r2 = r1;
}else{
r2 = T.T[r+1+N-1];
}
ll d1 = 0, d2 = 0;
if(l2 > l1){
d1 += (l2-l1)*-s;
}else{
d1 += (l1-l2)*t;
}
if(r2 > r1){
d1 += (r2-r1)*-s;
}else{
d1 += (r1-r2)*t;
}
T.upd(l, r, x);
T.upd(l-1, l-1, 0);
T.upd(l, l, 0);
T.upd(r+1, r+1, 0);
T.upd(r, r, 0);
l2 = T.T[l+N-1], r1 = T.T[r+N-1];
if(l == 1){
l1 = 0;
}else{
l1 = T.T[l-1+N-1];
}
if(r == n){
r2 = r1;
}else{
r2 = T.T[r+1+N-1];
}
if(l2 > l1){
d2 += (l2-l1)*-s;
}else{
d2 += (l1-l2)*t;
}
if(r2 > r1){
d2 += (r2-r1)*-s;
}else{
d2 += (r1-r2)*t;
}
cur += d2-d1;
cout << cur << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |