#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;
}
}
dp[0][0] = v[0][2];
dp[0][1] = v[0][2] - v[0][1];
for(int i = 1; i < v.size(); i++) {
dp[i][0] = max(dp[i - 1][0] + v[i][2] - v[i][0], dp[i - 1][1] + v[i][2]);
dp[i][1] = max(dp[i - 1][0] + max(0LL, v[i][2] - v[i][0] - v[i][1]), dp[i - 1][1] + v[i][2] - v[i][1]);
}
return dp[v.size() - 1][0];
}
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
Main.cpp: In function 'll calc()':
Main.cpp:39:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 1; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
62 ms |
2756 KB |
Output is correct |
8 |
Correct |
70 ms |
2740 KB |
Output is correct |
9 |
Correct |
72 ms |
2908 KB |
Output is correct |
10 |
Correct |
72 ms |
2760 KB |
Output is correct |
11 |
Correct |
62 ms |
2740 KB |
Output is correct |
12 |
Correct |
173 ms |
2844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
62 ms |
2756 KB |
Output is correct |
8 |
Correct |
70 ms |
2740 KB |
Output is correct |
9 |
Correct |
72 ms |
2908 KB |
Output is correct |
10 |
Correct |
72 ms |
2760 KB |
Output is correct |
11 |
Correct |
62 ms |
2740 KB |
Output is correct |
12 |
Correct |
173 ms |
2844 KB |
Output is correct |
13 |
Execution timed out |
2089 ms |
10240 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |