题目
思路:
假设有两个水塔1和2,分类讨论:
1、当a1 > b1时,2中剩下的水是a2 - b2 + a1 - b1
2、当a1 < b1时,1中的水不会流到2中,2中剩下的水是a2 - b2
即最大(a - b) 的后缀和
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define lson p << 1
#define rson p << 1 | 1
const int maxn = 5e5 + 5, maxm = 2e3 + 5;
int a[maxn], b[maxn], c[maxn], d[maxn], t[maxn << 2], suf[maxn];
int n;
int lazy[maxn << 2];
void push_up(int p){
t[p] = max(t[lson], t[rson]);
}
void push_down(int p, int l, int r){
lazy[lson] += lazy[p];
lazy[rson] += lazy[p];
int mid = (l + r) >> 1;
t[lson] += lazy[p];
t[rson] += lazy[p];
lazy[p] = 0;
}
void build(int p, int l, int r){
if(l == r){
t[p] = suf[l];
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(p);
}
void update(int p, int l, int r, int L, int R, int val){
if(L <= l && R >= r){
t[p] += val;
lazy[p] += val;
return;
}
int mid = (l + r) >> 1;
push_down(p, l, r);
if(L <= mid) update(lson, l, mid, L, R, val);
if(R >= mid + 1) update(rson, mid + 1, r, L, R, val);
push_up(p);
}
void solve()
{
cin >> n;
int q;
cin >> q;
int suma = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
suma += a[i];
}
for(int i = 1; i <= n; i++){
cin >> b[i];
d[i] = a[i] - b[i];
}
for(int i = 1; i < n; i++){
cin >> c[i];
}
suf[n + 1] = 0;
for(int i = n; i >= 1; i--){
suf[i] = suf[i + 1] + d[i];
}
build(1, 1, n);
while(q--){
int p, x, y, z;
cin >> p >> x >> y >> z;
suma += x - a[p];
int dif = (x - y) - d[p];
a[p] = x;
b[p] = y;
d[p] = x - y;
update(1, 1, n, 1, p, dif);
int res = suma - max(0LL, t[1]);
cout << res << '\n';
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
}