正文
增加 38 行删除 38 行
65 行未改动66676869707172737421 行未改动9697989910010110210310447 行未改动15215315415515615715815916016116216316416516616716816917017117217317417517617717817914 行未改动1941951961971981992002012022032042052062072082092102116 行未改动2182192202212222232242252267 行未改动2342352362372382392402412422432442452462472482492502512522 行未改动25525625725825926026126226326426526626726826927027127227327427527627727827928036 行未改动3173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583596 行未改动36636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341465 行未改动int sum=0;int m=l+(r-l)/2;if(L<=m){sum+=query(L,R,l,m,k<<1);}if(R>m){sum+=query(L,R,m+1,r,k<<1|1);}return sum;21 行未改动cout<<query(x,y,1,n,1)<<endl;}}return 0;}```先大概讲一下整体的解法再分开来讲。47 行未改动### 懒标记下传```cppinline void push_down(int p, int l, int r) {if (tree[p].lazy == 0) return;int mid = (l + r) >> 1;if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].l, l, mid);}if (!tree[p].r) {tree[p].r = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].r, mid + 1, r);}tree[tree[p].l].sum += tree[p].lazy * (mid - l + 1);tree[tree[p].r].sum += tree[p].lazy * (r - mid);tree[tree[p].l].lazy += tree[p].lazy;tree[tree[p].r].lazy += tree[p].lazy;tree[p].lazy = 0;}```14 行未改动```cppvoid update(int p, int l, int r, int ql, int qr, ll k) {init_node(p, l, r);if (ql <= l && r <= qr) {tree[p].sum += k * (r - l + 1);tree[p].lazy += k;return;}push_down(p, l, r);int mid = (l + r) >> 1;if (ql <= mid) {if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);6 行未改动if (cnt >= tree.size()) tree.resize(cnt + 1);}update(tree[p].r, mid + 1, r, ql, qr, k);}tree[p].sum = (tree[p].l ? tree[tree[p].l].sum : calc_init_sum(l, mid)) +(tree[p].r ? tree[tree[p].r].sum : calc_init_sum(mid + 1, r));}7 行未改动```cppll query(int p, int l, int r, int ql, int qr) {if (!p) return calc_init_sum(max(l, ql), min(r, qr));init_node(p, l, r);if (ql <= l && r <= qr) return tree[p].sum;push_down(p, l, r);int mid = (l + r) >> 1;ll res = 0;if (ql <= mid) res += query(tree[p].l, l, mid, ql, qr);if (qr > mid) res += query(tree[p].r, mid + 1, r, ql, qr);return res;}```区间查询的代码和传统代码一样,只是多了一重剪枝,判断了未创建的节点就直接计算初始区间和。2 行未改动```cppint main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;tree.reserve(MAX_OP * 4); // 预分配内存while (m--) {int op, l, r, k;cin >> op >> l >> r;if (op == 1) {cin >> k;update(root, 1, n, l, r, k);} else {cout << query(root, 1, n, l, r) << "\n";}}return 0;}```36 行未改动}inline void push_down(int p, int l, int r) {if (tree[p].lazy == 0) return;int mid = (l + r) >> 1;if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].l, l, mid);}if (!tree[p].r) {tree[p].r = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].r, mid + 1, r);}tree[tree[p].l].sum += tree[p].lazy * (mid - l + 1);tree[tree[p].r].sum += tree[p].lazy * (r - mid);tree[tree[p].l].lazy += tree[p].lazy;tree[tree[p].r].lazy += tree[p].lazy;tree[p].lazy = 0;}void update(int p, int l, int r, int ql, int qr, ll k) {init_node(p, l, r);if (ql <= l && r <= qr) {tree[p].sum += k * (r - l + 1);tree[p].lazy += k;return;}push_down(p, l, r);int mid = (l + r) >> 1;if (ql <= mid) {if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);6 行未改动if (cnt >= tree.size()) tree.resize(cnt + 1);}update(tree[p].r, mid + 1, r, ql, qr, k);}tree[p].sum = (tree[p].l ? tree[tree[p].l].sum : calc_init_sum(l, mid)) +(tree[p].r ? tree[tree[p].r].sum : calc_init_sum(mid + 1, r));}ll query(int p, int l, int r, int ql, int qr) {if (!p) return calc_init_sum(max(l, ql), min(r, qr));init_node(p, l, r);if (ql <= l && r <= qr) return tree[p].sum;push_down(p, l, r);int mid = (l + r) >> 1;ll res = 0;if (ql <= mid) res += query(tree[p].l, l, mid, ql, qr);if (qr > mid) res += query(tree[p].r, mid + 1, r, ql, qr);return res;}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;tree.reserve(MAX_OP * 4); // 预分配内存while (m--) {int op, l, r, k;cin >> op >> l >> r;if (op == 1) {cin >> k;update(root, 1, n, l, r, k);} else {cout << query(root, 1, n, l, r) << "\n";}}return 0;}```65 行未改动66676869707172737421 行未改动9697989910010110210310447 行未改动15215315415515615715815916016116216316416516616716816917017117217317417517617717817914 行未改动1941951961971981992002012022032042052062072082092102116 行未改动2182192202212222232242252267 行未改动2342352362372382392402412422432442452462472482492502512522 行未改动25525625725825926026126226326426526626726826927027127227327427527627727827928036 行未改动3173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583596 行未改动36636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341465 行未改动int sum=0;int m=l+(r-l)/2;if(L<=m){sum+=query(L,R,l,m,k<<1);}if(R>m){sum+=query(L,R,m+1,r,k<<1|1);}return sum;21 行未改动cout<<query(x,y,1,n,1)<<endl;}}return 0;}```先大概讲一下整体的解法再分开来讲。47 行未改动### 懒标记下传```cppinline void push_down(int p, int l, int r) {if (tree[p].lazy == 0) return;int mid = (l + r) >> 1;if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].l, l, mid);}if (!tree[p].r) {tree[p].r = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].r, mid + 1, r);}tree[tree[p].l].sum += tree[p].lazy * (mid - l + 1);tree[tree[p].r].sum += tree[p].lazy * (r - mid);tree[tree[p].l].lazy += tree[p].lazy;tree[tree[p].r].lazy += tree[p].lazy;tree[p].lazy = 0;}```14 行未改动```cppvoid update(int p, int l, int r, int ql, int qr, ll k) {init_node(p, l, r);if (ql <= l && r <= qr) {tree[p].sum += k * (r - l + 1);tree[p].lazy += k;return;}push_down(p, l, r);int mid = (l + r) >> 1;if (ql <= mid) {if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);6 行未改动if (cnt >= tree.size()) tree.resize(cnt + 1);}update(tree[p].r, mid + 1, r, ql, qr, k);}tree[p].sum = (tree[p].l ? tree[tree[p].l].sum : calc_init_sum(l, mid)) +(tree[p].r ? tree[tree[p].r].sum : calc_init_sum(mid + 1, r));}7 行未改动```cppll query(int p, int l, int r, int ql, int qr) {if (!p) return calc_init_sum(max(l, ql), min(r, qr));init_node(p, l, r);if (ql <= l && r <= qr) return tree[p].sum;push_down(p, l, r);int mid = (l + r) >> 1;ll res = 0;if (ql <= mid) res += query(tree[p].l, l, mid, ql, qr);if (qr > mid) res += query(tree[p].r, mid + 1, r, ql, qr);return res;}```区间查询的代码和传统代码一样,只是多了一重剪枝,判断了未创建的节点就直接计算初始区间和。2 行未改动```cppint main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;tree.reserve(MAX_OP * 4); // 预分配内存while (m--) {int op, l, r, k;cin >> op >> l >> r;if (op == 1) {cin >> k;update(root, 1, n, l, r, k);} else {cout << query(root, 1, n, l, r) << "\n";}}return 0;}```36 行未改动}inline void push_down(int p, int l, int r) {if (tree[p].lazy == 0) return;int mid = (l + r) >> 1;if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].l, l, mid);}if (!tree[p].r) {tree[p].r = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);init_node(tree[p].r, mid + 1, r);}tree[tree[p].l].sum += tree[p].lazy * (mid - l + 1);tree[tree[p].r].sum += tree[p].lazy * (r - mid);tree[tree[p].l].lazy += tree[p].lazy;tree[tree[p].r].lazy += tree[p].lazy;tree[p].lazy = 0;}void update(int p, int l, int r, int ql, int qr, ll k) {init_node(p, l, r);if (ql <= l && r <= qr) {tree[p].sum += k * (r - l + 1);tree[p].lazy += k;return;}push_down(p, l, r);int mid = (l + r) >> 1;if (ql <= mid) {if (!tree[p].l) {tree[p].l = ++cnt;if (cnt >= tree.size()) tree.resize(cnt + 1);6 行未改动if (cnt >= tree.size()) tree.resize(cnt + 1);}update(tree[p].r, mid + 1, r, ql, qr, k);}tree[p].sum = (tree[p].l ? tree[tree[p].l].sum : calc_init_sum(l, mid)) +(tree[p].r ? tree[tree[p].r].sum : calc_init_sum(mid + 1, r));}ll query(int p, int l, int r, int ql, int qr) {if (!p) return calc_init_sum(max(l, ql), min(r, qr));init_node(p, l, r);if (ql <= l && r <= qr) return tree[p].sum;push_down(p, l, r);int mid = (l + r) >> 1;ll res = 0;if (ql <= mid) res += query(tree[p].l, l, mid, ql, qr);if (qr > mid) res += query(tree[p].r, mid + 1, r, ql, qr);return res;}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;tree.reserve(MAX_OP * 4); // 预分配内存while (m--) {int op, l, r, k;cin >> op >> l >> r;if (op == 1) {cin >> k;update(root, 1, n, l, r, k);} else {cout << query(root, 1, n, l, r) << "\n";}}return 0;}```