线段树模板
xuan_never · · 算法·理论
#include <bits/stdc++.h>
using namespace std;
#define null ?
#define mid (l + r >> 1)
typedef long long ll;
struct Tree {
int l, r;
ll v, ta;
Tree *ls, *rs;
void mt(ll x) {
v = ,
ta = ;
}
void pushup() {
v = ;
}
void pushdown() {
if (ta != null) {
ls->mt(ta),
rs->mt(ta);
ta = null;
}
}
void upd(int x, int y, ll v) {
if (x <= l && y >= r)
return mt(v);
pushdown();
if (mid >= x) ls->upd(x, y, v);
if (mid < y) rs->upd(x, y, v);
pushup();
}
ll qry(int x, int y) {
if (x <= l && y >= r)
return v;
pushdown();
ll an = ;
if (mid >= x) an ? ls->qry(x, y);
if (mid < y) an ? rs->qry(x, y);
pushup();
return an;
}
} T[int(maxn * 4.19)], *sg = T, *rt;
Tree* build(int l, int r) {
Tree* u = ++sg;
u->l = l, u->r = r;
u->ta = null;
if (l == r) {
u->v = a[l];
return u;
} u->ls = build(l, mid), u->rs = build(mid + 1, r);
u->pushup();
return u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
rt = build(1, n);
return 0;
}
例
// P2357
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mid (l + r >> 1)
int n, m, op, x, y;
ll a[200005], v;
struct Tree {
int l, r;
ll v, ta;
Tree *ls, *rs;
void mt(ll x) {
v += x * (r - l + 1), ta += x;
}
void pushup() {
v = ls->v + rs->v;
}
void pushdown() {
if (ta) {
ls->mt(ta),
rs->mt(ta);
ta = 0;
}
}
void upd(int x, int y, ll v) {
if (x <= l && y >= r) {
mt(v);
return;
} pushdown();
if (mid >= x) ls->upd(x, y, v);
if (mid < y) rs->upd(x, y, v);
pushup();
}
ll qry(int x, int y) {
if (x <= l && y >= r)
return v;
pushdown();
ll an = 0;
if (mid >= x) an += ls->qry(x, y);
if (mid < y) an += rs->qry(x, y);
pushup();
return an;
}
} T[800005], *sg = T, *rt;
Tree* build(int l, int r) {
Tree *u = ++sg;
u->l = l, u->r = r, u->ta = 0;
if (l == r) {
u->v = a[l];
return u;
} u->ls = build(l, mid),
u->rs = build(mid + 1, r);
u->pushup();
return u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> a[i];
rt = build(1, n);
while (m--) {
cin >> op;
if (op == 1) {
cin >> x >> y >> v;
rt->upd(x, y, v);
} else if (op == 2) {
cin >> v;
rt->upd(1, 1, v);
} else if (op == 3) {
cin >> v;
rt->upd(1, 1, -v);
} else if (op == 4) {
cin >> x >> y;
cout << rt->qry(x, y) << '\n';
} else cout << rt->qry(1, 1) << '\n';
}
return 0;
}
// P1253
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mid (l + r >> 1)
#define null -2e18
int n, m, a[1000006], op, x, k, l, r;
struct Tree {
int l, r;
ll v, ta1, ta2;
// ta1标记为覆盖,2标记为增加;下mt upd同
Tree *ls, *rs;
void mt1(ll x) {
ta1 = v = x,
ta2 = 0;
}
void mt2(ll x) {
v += x;
(ta1 != null ? ta1 : ta2) += x;
}
void pushup() {
v = max(ls->v, rs->v);
}
void pushdown() {
if (ta1 != null) {
ls->mt1(ta1),
rs->mt1(ta1);
ta1 = null;
} else if (ta2) {
ls->mt2(ta2),
rs->mt2(ta2);
ta2 = 0;
}
}
void upd1(int x, int y, int w) {
if (x <= l && y >= r) {
mt1(w);
return;
} pushdown();
if (mid >= x) ls->upd1(x, y, w);
if (mid < y) rs->upd1(x, y, w);
pushup();
}
void upd2(int x, int y, int w) {
if (x <= l && y >= r) {
mt2(w);
return;
} pushdown();
if (mid >= x) ls->upd2(x, y, w);
if (mid < y) rs->upd2(x, y, w);
pushup();
}
ll qry(int x, int y) {
if (x <= l && y >= r)
return v;
pushdown();
ll an = null;
if (mid >= x) an = ls->qry(x, y);
if (mid < y) an = max(an, rs->qry(x, y));
pushup();
return an;
}
} T[4000006], *sg = T, *rot;
Tree* build(int l, int r) {
Tree *u = ++sg;
u->l = l, u->r = r;
u->ta1 = null, u->ta2 = 0;
if (l == r) {
u->v = a[l];
return u;
} u->ls = build(l, mid),
u->rs = build(mid + 1, r);
u->pushup();
return u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> a[i];
rot = build(1, n);
while (m--) {
cin >> op >> l >> r;
if (op < 3) {
cin >> x;
if (op < 2)
rot->upd1(l, r, x);
else rot->upd2(l, r, x);
} else cout << rot->qry(l, r) << '\n';
}
return 0;
}