Description
给定序列 a=(a1,a2,⋯ ,an)a=(a_1,a_2,\cdots,a_n)a=(a1,a2,⋯,an),另有序列 hhh,初始时 h=ah=ah=a.
有 mmm 个操作分两种:
- add(l,r,k)\operatorname{add}(l,r,k)add(l,r,k):对每个 i∈[l,r]i\in[l,r]i∈[l,r] 执行 ai←ai+ka_i\gets a_i+kai←ai+k.
- query(l,r)\operatorname{query}(l,r)query(l,r):求 ∑i=lrhi\sum_{i=l}^r h_i∑i=lrhi.
每次 操作 后,对每个 i∈[1,n]i\in[1,n]i∈[1,n] 执行 hi←hi+aih_i\gets h_i+a_ihi←hi+ai.
Limitations
1≤n,m≤1051\le n,m\le 10^51≤n,m≤105
1≤l≤r≤n1\le l \le r \le n1≤l≤r≤n
∣ai∣,∣k∣≤1000|a_i|,|k|\le 1000∣ai∣,∣k∣≤1000
1s,512MB1\text{s},512\text{MB}1s,512MB
Solution
显然使用线段树,每个节点维护信息 D=(sum,hsum,len)D=(\textit{sum},\textit{hsum},\textit{len})D=(sum,hsum,len).
当然还需要标记 T=(tag,htag,upd)T=(\textit{tag},\textit{htag},\textit{upd})T=(tag,htag,upd):
- tag\textit{tag}tag:aia_iai 的加标记.
- htag\textit{htag}htag:hih_ihi 的加标记.
- upd\textit{upd}upd:hih_ihi 的更新次数.
D+DD+DD+D 显然,D+TD+TD+T 和 T+TT+TT+T 可以用矩阵推,当然也可以直接靠理解:
- hsum←hsum+sum×upd ′+htag ′×len\textit{hsum}\gets\textit{hsum}+\textit{sum}\times \textit{upd\;}^\prime+\textit{htag\;}^\prime\times \textit{len}hsum←hsum+sum×upd′+htag′×len(总共加了 upd\textit{upd}upd 次原序列,再加上自己的标记)
- sum←sum+tag ′×len\textit{sum}\gets\textit{sum}+\textit{tag\;}^{\prime}\times\textit{len}sum←sum+tag′×len(加上自己标记)
- htag←htag+tag×upd ′+htag ′\textit{htag}\gets\textit{htag}+\textit{tag}\times \textit{upd\;}^{\prime}+\textit{htag\;}^{\prime}htag←htag+tag×upd′+htag′(和 hsum\textit{hsum}hsum 同理)
- tag←tag+tag ′\textit{tag}\gets\textit{tag}+\textit{tag\;}^{\prime}tag←tag+tag′
- upd←upd+upd ′\textit{upd}\gets\textit{upd}+\textit{upd\;}^{\prime}upd←upd+upd′
然后套模板即可,时间复杂度 O(mlogn)O(m\log n)O(mlogn).
Code
2.8KB,0.47s,10.5MB (in total, C++20 with O2)2.8\text{KB},0.47\text{s},10.5\text{MB}\;\texttt{(in total, C++20 with O2)}2.8KB,0.47s,10.5MB(in total, C++20 with O2)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;
template<class T>
bool chmax(T &a, const T &b){
if(a < b){ a = b; return true; }
return false;
}
template<class T>
bool chmin(T &a, const T &b){
if(a > b){ a = b; return true; }
return false;
}
namespace seg_tree {
struct Node {
int l, r, len;
i64 sum, hissum, tag, histag, upd;
};
inline int ls(int u) { return 2 * u + 1; }
inline int rs(int u) { return 2 * u + 2; }
struct SegTree {
vector<Node> tr;
inline SegTree() {}
inline SegTree(const vector<i64>& a) {
const int n = a.size();
tr.resize(n << 1);
build(0, 0, n - 1, a);
}
inline void pushup(int u, int mid) {
tr[u].sum = tr[ls(mid)].sum + tr[rs(mid)].sum;
tr[u].hissum = tr[ls(mid)].hissum + tr[rs(mid)].hissum;
}
inline void apply(int u, i64 tag, i64 histag, i64 upd) {
tr[u].hissum += tr[u].sum * upd + histag * tr[u].len;
tr[u].sum += tag * tr[u].len;
tr[u].histag += tr[u].tag * upd + histag;
tr[u].tag += tag;
tr[u].upd += upd;
}
inline void pushdown(int u, int mid) {
apply(ls(mid), tr[u].tag, tr[u].histag, tr[u].upd);
apply(rs(mid), tr[u].tag, tr[u].histag, tr[u].upd);
tr[u].tag = tr[u].histag = tr[u].upd = 0;
}
inline void build(int u, int l, int r, const vector<i64>& a) {
tr[u].l = l, tr[u].r = r, tr[u].len = r - l + 1;
if (l == r) return (void)(tr[u].sum = tr[u].hissum = a[l]);
const int mid = (l + r) >> 1;
build(ls(mid), l, mid, a);
build(rs(mid), mid + 1, r, a);
pushup(u, mid);
}
inline void add(int u, int l, int r, i64 k) {
if (l <= tr[u].l && tr[u].r <= r) return apply(u, k, 0, 0);
const int mid = (tr[u].l + tr[u].r) >> 1;
pushdown(u, mid);
if (l <= mid) add(ls(mid), l, r, k);
if (r > mid) add(rs(mid), l, r, k);
pushup(u, mid);
}
inline i64 query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) return tr[u].hissum;
const int mid = (tr[u].l + tr[u].r) >> 1;
i64 res = 0;
pushdown(u, mid);
if (l <= mid) res += query(ls(mid), l, r);
if (r > mid) res += query(rs(mid), l, r);
return res;
}
inline void range_add(int l, int r, i64 k) { add(0, l, r, k); }
inline i64 range_hsum(int l, int r) { return query(0, l, r); }
};
}
using seg_tree::SegTree;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
scanf("%d %d", &n, &m);
vector<i64> a(n);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
SegTree sgt(a);
for (int i = 0, op, l, r, v; i < m; i++) {
scanf("%d %d %d", &op, &l, &r), l--, r--;
if (op == 1) sgt.range_add(l, r, (scanf("%d", &v), v));
else printf("%lld\n", sgt.range_hsum(l, r));
sgt.apply(0, 0, 0, 1);
}
return 0;
}
343

被折叠的 条评论
为什么被折叠?



