思路
首先考虑操作 1 一个点 \(i\) 能被操作到的条件。注意到此时 \(x \sim i - 1\) 这些位置都是被更新过的,再仔细观察此时 \(\forall j \in [x,i),a_j = a_x + \sum_{p = x}^{j - 1}k_p\)。
那么对于 \(a_i\) 如果会被修改将会变为 \(a_x + \sum_{p = x}^{i - 1}k_p\),那么 \(a_i\) 被修改,当且仅当,其中 \(s\) 为 \(k\) 的前缀和:
\[a_i < a_x + \sum_{p = x}^{i - 1}k_p\\ \Rightarrow a_i < a_x + s_{i - 1} - s_{x - 1}\\ \Rightarrow a_i - s_{i - 1} < a_x - s_{x - 1} \]不妨维护 \(a_i - s_{i - 1}\) 的值,这样我们可以二分找到最大的 \(i\)。此时的修改操作变为了区间推平为 \(a_x - s_{x - 1}\),直接丢到线段树上即可。
最后考虑操作 2 就简单了,在线段树上查询 \([l,r]\) 维护的 \(a_i - s_{i - 1}\) 的和,加上 \(s_{l - 1 \sim r - 1}\) 即可。
Code
#include <bits/stdc++.h>
#define re register
#define fst first
#define snd second
#define int long long
using namespace std;
const int N = 1e5 + 10;
const int inf = (int)(1e18) + 10;
int n,q;
int arr[N],k[N],s[N],ss[N];
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
struct{
#define ls(u) (u << 1)
#define rs(u) (u << 1 | 1)
struct node{
int l,r;
int Max,sum,tag;
}tr[N << 2];
inline void calc(int u,int k){
tr[u].Max = tr[u].tag = k;
tr[u].sum = (tr[u].r - tr[u].l + 1) * k;
}
inline void pushup(int u){
tr[u].sum = tr[ls(u)].sum + tr[rs(u)].sum;
tr[u].Max = max(tr[ls(u)].Max,tr[rs(u)].Max);
}
inline void pushdown(int u){
if (tr[u].tag != inf){
calc(ls(u),tr[u].tag); calc(rs(u),tr[u].tag);
tr[u].tag = inf;
}
}
inline void build(int u,int l,int r){
tr[u] = {l,r,inf,0,inf};
if (l == r) return (tr[u].Max = tr[u].sum = arr[l] - s[l - 1]),void();
int mid = l + r >> 1;
build(ls(u),l,mid); build(rs(u),mid + 1,r);
pushup(u);
}
inline void modify(int u,int x,int k){
if (tr[u].l == tr[u].r){
tr[u].Max += k; tr[u].sum += k;
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(ls(u),x,k);
else modify(rs(u),x,k);
pushup(u);
}
inline void update(int u,int l,int r,int k){
if (l <= tr[u].l && tr[u].r <= r) return calc(u,k),void();
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) update(ls(u),l,r,k);
if (r > mid) update(rs(u),l,r,k);
pushup(u);
}
inline int querymax(int u,int l,int r){
if (l <= tr[u].l && tr[u].r <= r) return tr[u].Max;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid && r > mid) return max(querymax(ls(u),l,r),querymax(rs(u),l,r));
else if (l <= mid) return querymax(ls(u),l,r);
else return querymax(rs(u),l,r);
}
inline int querysum(int u,int l,int r){
if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid && r > mid) return querysum(ls(u),l,r) + querysum(rs(u),l,r);
else if (l <= mid) return querysum(ls(u),l,r);
else return querysum(rs(u),l,r);
}
#undef ls
#undef rs
}T;
signed main(){
n = read();
for (re int i = 1;i <= n;i++) arr[i] = read();
for (re int i = 1;i < n;i++) s[i] = s[i - 1] + (k[i] = read());
for (re int i = 1;i < n;i++) ss[i] = ss[i - 1] + s[i];
T.build(1,1,n);
q = read();
while (q--){
char op[10]; scanf("%s",op);
if (op[0] == '+'){
int x,k;
x = read(),k = read();
T.modify(1,x,k);
int val = T.querysum(1,x,x);
int l = x + 1,r = n;
while (l < r){
int mid = l + r + 1 >> 1;
if (T.querymax(1,x + 1,mid) < val) l = mid;
else r = mid - 1;
}
if (x < n && T.querymax(1,x + 1,l) < val) T.update(1,x + 1,l,val);
}
else{
int l,r;
l = read(),r = read();
int sum = T.querysum(1,l,r) + ss[r - 1];
if (l - 2 >= 0) sum -= ss[l - 2];
printf("%lld\n",sum);
}
}
return 0;
}
标签:Hasn,CF1136E,int,题解,sum,tr,mid,querymax,return
From: https://www.cnblogs.com/WaterSun/p/18468592