#include <bits/stdc++.h>
#define pii pair<int,int>
#define pli pair<long long,pii>
#define fi first
#define se second
#define vi vector<int>
#define vii vector<pii>
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 1e9;
const ll linf = 1e16;
const double pi = acos(-1);
const int N = 2e5 + 5;
ll a[N];
ll d[N];
ll dp[N][2];
int n,q;
ll brute(){
dp[0][0] = dp[0][1] = 0;
dp[1][0] = dp[1][1] = 0;
if (d[1] > 0) dp[1][0] = d[1];
else dp[1][1] = -d[1];
for (int i = 2; i < n; i++){
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1];
if (d[i] > 0){
dp[i][0] = max(dp[i][0], dp[i - 1][0] + d[i]);
dp[i][0] = max(dp[i][0], max(dp[i - 2][0], dp[i - 2][1]) + d[i]);
}
else{
dp[i][1] = max(dp[i][1], dp[i - 1][1] - d[i]);
dp[i][1] = max(dp[i][1], max(dp[i - 2][0], dp[i - 2][1]) - d[i]);
}
}
return max(dp[n - 1][0], dp[n - 1][1]);
}
struct node{
ll left = 0, right = 0;
ll val[2][2];
node(ll x){
left = right = x;
val[0][0] = val[0][1] = val[1][0] = val[1][1] = 0;
val[1][1] = abs(x);
}
};
node combine(node& a,node& b){
node n(0);
n.left = a.left;
n.right = b.right;
///[i - j][k - l]
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
for (int k = 0; k < 2; k++){
for (int l = 0; l < 2; l++){
if (j && k){
if ((a.right >= 0 && b.left >= 0) || (a.right <= 0 && b.left <= 0)){
n.val[i][l] = max(n.val[i][l], a.val[i][j] + b.val[k][l]);
}
}
else n.val[i][l] = max(n.val[i][l], a.val[i][j] + b.val[k][l]);
}
}
}
}
return n;
}
class IT{
private:
vector<node> t;
ll getAns(node& f){
return max({f.val[0][0],f.val[0][1],f.val[1][0],f.val[1][1]});
}
public:
IT (int n){
t.resize(4 * n,node(0));
}
void build(int id,int l,int r){
if (l == r){
t[id] = node(d[l]);
return;
}
int mid = (l + r) / 2;
build(id * 2,l,mid);
build(id * 2 + 1,mid + 1,r);
t[id] = combine(t[id * 2],t[id * 2 + 1]);
// cout << l << " " << r << " " << getAns(t[id]) << "\n";
}
void update(int id,int l,int r,int i,int val){
if (l > i || r < i) return;
if (l == r){
t[id] = node(val);
return;
}
int mid = (l + r) / 2;
update(id * 2,l,mid,i,val);
update(id * 2 + 1,mid + 1,r,i,val);
t[id] = combine(t[id * 2],t[id * 2 + 1]);
}
ll query(int id,int l,int r,int u,int v){
if (l > v || r < u) return 0;
if (l >= u && r <= v){
return getAns(t[id]);
}
return -1;
}
};
void solve(){
IT t(n);
t.build(1,1,n - 1);
while(q--){
int l,r,x;
cin >> l >> r >> x;
d[l - 1] += x;
d[r] -= x;
if (l - 1 >= 1) t.update(1,1,n - 1,l - 1,d[l - 1]);
if (r <= n - 1) t.update(1,1,n - 1,r,d[r]);
cout << t.query(1,1,n - 1,1,n - 1) << "\n";
// cout << brute() << "\n";
}
}
int main(){-
#define TEXT "a"
cin.tie(0) -> sync_with_stdio(0);
if (fopen(TEXT".inp","r")){
freopen(TEXT".inp","r",stdin);
freopen(TEXT".out","w",stdout);
}
cin >> n >> q;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
for (int i = 1; i < n; i++) d[i] = a[i + 1] - a[i];
solve();
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:135:12: warning: value computed is not used [-Wunused-value]
135 | int main(){-
| ^
136 | #define TEXT "a"
| ~~~~~~~~~~~~~~~~
137 | cin.tie(0) -> sync_with_stdio(0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(TEXT".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(TEXT".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
2 ms |
2384 KB |
Output is correct |
3 |
Correct |
2 ms |
2572 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
2 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
2 ms |
2384 KB |
Output is correct |
3 |
Correct |
2 ms |
2572 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
2 ms |
2384 KB |
Output is correct |
7 |
Correct |
6 ms |
3152 KB |
Output is correct |
8 |
Correct |
6 ms |
3152 KB |
Output is correct |
9 |
Correct |
6 ms |
3152 KB |
Output is correct |
10 |
Correct |
9 ms |
3216 KB |
Output is correct |
11 |
Correct |
6 ms |
3152 KB |
Output is correct |
12 |
Correct |
6 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
2 ms |
2384 KB |
Output is correct |
3 |
Correct |
2 ms |
2572 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
2 ms |
2384 KB |
Output is correct |
7 |
Correct |
6 ms |
3152 KB |
Output is correct |
8 |
Correct |
6 ms |
3152 KB |
Output is correct |
9 |
Correct |
6 ms |
3152 KB |
Output is correct |
10 |
Correct |
9 ms |
3216 KB |
Output is correct |
11 |
Correct |
6 ms |
3152 KB |
Output is correct |
12 |
Correct |
6 ms |
3152 KB |
Output is correct |
13 |
Correct |
548 ms |
51028 KB |
Output is correct |
14 |
Correct |
582 ms |
51048 KB |
Output is correct |
15 |
Correct |
595 ms |
51284 KB |
Output is correct |
16 |
Correct |
599 ms |
51000 KB |
Output is correct |
17 |
Correct |
522 ms |
51016 KB |
Output is correct |
18 |
Correct |
453 ms |
51784 KB |
Output is correct |