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;
const int maxn = 200005;
int n, q;
int a[maxn];
long long dp[maxn];
long long pref[maxn];
long long delta[maxn];
bool is_diff(long long x, long long y)
{
if (x > 0 && y < 0)
{
return true;
}
if (x < 0 && y > 0)
{
return true;
}
return false;
}
void solve()
{
for (int i = 1; i <= n - 1; ++i)
{
delta[i] = a[i + 1] - a[i];
}
dp[1] = abs(delta[1]);
for (int i = 2; i <= n - 1; ++i)
{
pref[i] = max(dp[i - 1], pref[i - 1]);
if (is_diff(delta[i], delta[i - 1]) == false)
{
dp[i] = pref[i - 1] + abs(delta[i]);
}
else
{
dp[i] = pref[i - 1] + abs(delta[i]);
}
}
cout << max(dp[n - 1], pref[n - 1]) << "\n";
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> q;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
for (int i = 1; i <= q; ++i)
{
int l, r, x;
cin >> l >> r >> x;
for (int j = l; j <= r; ++j)
{
a[j] += x;
}
solve();
}
return 0;
}
/*
4 3
1 2 3 4
1 2 1
1 1 2
2 3 1
4 1
1 2 3 4
1 2 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |