#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 |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
464 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
4 ms |
588 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
5 ms |
748 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
5 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
5 ms |
580 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
612 KB |
Output is correct |
17 |
Correct |
3 ms |
600 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
456 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
618 ms |
16940 KB |
Output is correct |
2 |
Correct |
601 ms |
17488 KB |
Output is correct |
3 |
Correct |
604 ms |
18276 KB |
Output is correct |
4 |
Correct |
614 ms |
17648 KB |
Output is correct |
5 |
Correct |
573 ms |
18780 KB |
Output is correct |
6 |
Correct |
441 ms |
17752 KB |
Output is correct |
7 |
Correct |
467 ms |
17744 KB |
Output is correct |
8 |
Correct |
608 ms |
18504 KB |
Output is correct |
9 |
Correct |
532 ms |
18772 KB |
Output is correct |
10 |
Correct |
620 ms |
17688 KB |
Output is correct |
11 |
Correct |
590 ms |
17492 KB |
Output is correct |
12 |
Correct |
452 ms |
18428 KB |
Output is correct |
13 |
Correct |
606 ms |
18512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
464 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
4 ms |
588 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
5 ms |
748 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
5 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
5 ms |
580 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
612 KB |
Output is correct |
17 |
Correct |
3 ms |
600 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
456 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
618 ms |
16940 KB |
Output is correct |
23 |
Correct |
601 ms |
17488 KB |
Output is correct |
24 |
Correct |
604 ms |
18276 KB |
Output is correct |
25 |
Correct |
614 ms |
17648 KB |
Output is correct |
26 |
Correct |
573 ms |
18780 KB |
Output is correct |
27 |
Correct |
441 ms |
17752 KB |
Output is correct |
28 |
Correct |
467 ms |
17744 KB |
Output is correct |
29 |
Correct |
608 ms |
18504 KB |
Output is correct |
30 |
Correct |
532 ms |
18772 KB |
Output is correct |
31 |
Correct |
620 ms |
17688 KB |
Output is correct |
32 |
Correct |
590 ms |
17492 KB |
Output is correct |
33 |
Correct |
452 ms |
18428 KB |
Output is correct |
34 |
Correct |
606 ms |
18512 KB |
Output is correct |
35 |
Correct |
633 ms |
16980 KB |
Output is correct |
36 |
Correct |
606 ms |
18548 KB |
Output is correct |
37 |
Correct |
580 ms |
19224 KB |
Output is correct |
38 |
Correct |
599 ms |
19028 KB |
Output is correct |
39 |
Correct |
575 ms |
19024 KB |
Output is correct |
40 |
Correct |
612 ms |
19188 KB |
Output is correct |
41 |
Correct |
597 ms |
19024 KB |
Output is correct |
42 |
Correct |
620 ms |
19148 KB |
Output is correct |
43 |
Correct |
617 ms |
18256 KB |
Output is correct |
44 |
Correct |
596 ms |
18684 KB |
Output is correct |
45 |
Correct |
608 ms |
18660 KB |
Output is correct |
46 |
Correct |
606 ms |
19928 KB |
Output is correct |
47 |
Correct |
437 ms |
18256 KB |
Output is correct |
48 |
Correct |
547 ms |
18272 KB |
Output is correct |
49 |
Correct |
586 ms |
17380 KB |
Output is correct |
50 |
Correct |
549 ms |
18264 KB |
Output is correct |
51 |
Correct |
556 ms |
18512 KB |
Output is correct |
52 |
Correct |
619 ms |
18388 KB |
Output is correct |