# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
850175 | gun_gan | Sjeckanje (COCI21_sjeckanje) | C++17 | 1 ms | 2392 KiB |
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;
typedef long long ll;
const int MX = 2e5 + 5;
int N, Q;
ll A[MX], dp[MX][2];
ll calc() {
for(int i = 0; i <= N; i++) dp[i][0] = dp[i][1] = 0;
vector<array<ll,3>> v;
for(int i = 1; i + 1 <= N; i++) {
if(A[i + 1] >= A[i]) {
int j = i;
ll sum = 0;
while(j + 1 <= N && A[j + 1] >= A[j]) {
sum += A[j + 1] - A[j];
j++;
}
v.push_back({A[i + 1] - A[i], A[j] - A[j - 1], sum});
i = j - 1;
} else {
int j = i;
ll sum = 0;
while(j + 1 <= N && A[j + 1] < A[j]) {
sum += A[j] - A[j + 1];
j++;
}
v.push_back({A[i] - A[i + 1], A[j - 1] - A[j], sum});
i = j - 1;
}
}
if(v.size() == 1) return v[0][2];
ll res = v.back()[2];
for(int i = 0; i + 1 < v.size(); i++) {
res += v[i][2];
res -= min(v[i][1], v[i + 1][0]);
}
return res;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> N >> Q;
for(int i = 1; i <= N; i++) cin >> A[i];
for(int i = 1; i <= Q; i++) {
ll l, r, x;
cin >> l >> r >> x;
for(int j = l; j <= r; j++) A[j] += x;
cout << calc() << '\n';
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |