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>
using namespace std;
#define ll long long
#define ii pair<int, int>
#define fi first
#define se second
#define pb push_back
const int N = 2009;
const int oo = 1e9;
const int mod = 1e9 + 7;
int n, q, de, inc, sum;
int node[N], la[4*N];
void true_val(int i, int l, int r) {
if(l > r || !la[i]) return;
if(l == r) node[l] += la[i];
else {
la[2*i] += la[i];
la[2*i + 1] += la[i];
}
la[i] = 0;
}
void update(int i, int l, int r, int a, int b, int val) {
true_val(i, l, r);
if(l > r || r < a || l > b) return;
if(a <= l && r <= b) {
la[i] += val;
return;
}
int mid = (l + r) / 2;
update(2*i, l, mid, a, b, val); update(2*i + 1, mid + 1, r, a, b, val);
}
void getm(int i, int l, int r, int pos) {
true_val(i, l, r);
if(l > pos || pos > r) return;
if(l == r) return;
int mid = (l + r) / 2;
getm(2*i, l, mid, pos); getm(2*i + 1, mid + 1, r, pos);
}
signed main()
{
//freopen("test.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q >> de >> inc;
for(int i = 0; i <= n; ++i) cin >> node[i];
for(int i = 0; i < n; ++i){
if(node[i + 1] > node[i]) sum -= de*(node[i + 1] - node[i]);
else sum += inc*(node[i] - node[i + 1]);
}
//cout << sum << '\n';
while(q --) {
int l, r, val; cin >> l >> r >> val;
if(l > 0) {
getm(1, 0, n, l - 1);
getm(1, 0, n, l);
if(node[l] > node[l - 1]) sum += de*(node[l] - node[l - 1]);
else sum -= inc*(node[l - 1] - node[l]);
}
if(r < n) {
getm(1, 0, n, r + 1);
getm(1, 0, n, r);
if(node[r] < node[r + 1]) sum += de*(node[r + 1] - node[r]);
else sum -= inc*( -node[r + 1] + node[r]);
}
update(1, 0, n, l, r, val);
if(l > 0) {
getm(1, 0, n, l);
if(l > 0){
if(node[l] > node[l - 1]) sum -= de*(node[l] - node[l - 1]);
else sum += inc*(node[l - 1] - node[l]);
}
}
if(r < n) {
getm(1, 0, n, r);
if(r < n){
if(node[r] < node[r + 1]) sum -= de*(node[r + 1] - node[r]);
else sum += inc*(node[r] - node[r + 1]);
}
}
cout << sum << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |