#include<bits/stdc++.h>
#define taskname "E"
using namespace std;
typedef long long ll;
const int lim = 2e5 + 5;
const ll INF = 1e18;
struct Data{
ll pos, neg, bal;
Data(){}
Data(ll pos, ll neg, ll bal){
this->pos = pos;
this->neg = neg;
this->bal = bal;
}
};
Data st[lim << 2];
int n, q, a[lim];
ll laz[lim << 2];
Data merge(Data a, Data b){
Data ans;
ans.pos = max({a.pos, b.pos, a.bal + b.pos});
ans.neg = max({a.neg, b.neg, a.bal + b.neg});
ans.bal = max({a.bal, b.bal, a.bal + b.bal, a.pos + b.neg, a.neg + b.pos});
return ans;
}
void build(int id, int l, int r){
laz[id] = 0;
if(l == r){
st[id] = Data(a[l], -a[l], 0LL);
return;
}
int m = (l + r) >> 1;
build(id << 1, l, m);
build(id << 1 | 1, m + 1, r);
st[id] = merge(st[id << 1], st[id << 1 | 1]);
}
void push_down(int id){
st[id << 1].pos += laz[id];
st[id << 1].neg -= laz[id];
laz[id << 1] += laz[id];
st[id << 1 | 1].pos += laz[id];
st[id << 1 | 1].neg -= laz[id];
laz[id << 1 | 1] += laz[id];
laz[id] = 0;
}
void update(int id, int l, int r, int u, int v, int x){
if(l > v || r < u){
return;
}
if(u <= l && v >= r){
st[id].pos += x;
st[id].neg -= x;
laz[id] += x;
return;
}
int m = (l + r) >> 1;
push_down(id);
update(id << 1, l, m, u, v, x);
update(id << 1 | 1, m + 1, r, u, v, x);
st[id] = merge(st[id << 1], st[id << 1 | 1]);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
build(1, 1, n);
for(int _ = 0; _ < q; _++){
int l, r, x;
cin >> l >> r >> x;
update(1, 1, n, l, r, x);
cout << st[1].bal << "\n";
}
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:65:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |