C++template <typename Tp> struct operAdd {
auto operator()(const Tp &a, const Tp &b) -> Tp { return a + b; }
};
template <typename Tp, typename SizeT> struct operRAdd {
auto operator()(const Tp &a, const SizeT &l, const SizeT &r) -> Tp {
return a * (r - l + 1);
}
};
/* 汎用高性能セグメントツリーテンプレート!
*
* 模板参数 (template arguments):
* - `Tp = int // 节点值类型`
* - `SizeT = int // 区间值类型`
* - `OperPushup = operAdd<Tp> // 节点合并运算`
* - `OperModify = operAdd<Tp> // 节点修改运算`
* - `OperRange = operRAdd<Tp, SizeT> // 区间修改运算`
* - `defaultVal = 0 // 节点默认值`
* - `defaultTag = 0 // 标记默认值`
*
* 静态方法 (static method):
* - `NewNode() -> segtree<Tp, SizeT, OperPushup, OperModify, OperRange,
* defaultVal, defaultTag> * // 返回一个指向新节点的指针`
*
* 实例方法 (instance method):
* - `build(SizeT, SizeT, [optional]Tp*) // 传入区间大小,建立线段树`
* - `modify(SizeT, Tp) // 传入位置和值,修改节点`
* - `query(SizeT) -> Tp // 传入位置,获取节点的值`
*/
template <typename Tp = int, typename SizeT = int,
class OperPushup = operAdd<Tp>, class OperModify = operAdd<Tp>,
class OperRange = operRAdd<Tp, SizeT>, int defaultVal = 0,
int defaultTag = 0>
struct segtree {
private:
SizeT l, r;
Tp val;
Tp tag;
segtree<Tp, SizeT, OperPushup, OperModify, OperRange, defaultVal,
defaultTag> *son[2];
#define ls son[0]
#define rs son[1]
#define mid (((l) + (r)) >> 1)
/* 注意:这个函数不会进行边界检查 */
void pushdown() {
if (this->tag == defaultTag) {
return;
}
ls->val = OperModify()(ls->val, OperRange()(this->tag, ls->l, ls->r));
rs->val = OperModify()(rs->val, OperRange()(this->tag, rs->l, rs->r));
ls->tag = ls->tag == defaultTag ? this->tag
: OperModify()(ls->tag, this->tag);
rs->tag = rs->tag == defaultTag ? this->tag
: OperModify()(rs->tag, this->tag);
this->tag = defaultTag;
}
/* 注意:这个函数不会进行边界检查 */
void pushup() { this->val = OperPushup()(ls->val, rs->val); }
public:
segtree()
: l(0), r(0), val(defaultVal), tag(defaultTag), son{nullptr, nullptr} {
}
static auto newNode() -> segtree<Tp, SizeT, OperPushup, OperModify,
OperRange, defaultVal, defaultTag> * {
constexpr SizeT bufSize = 1e5;
static segtree<Tp, SizeT, OperPushup, OperModify, OperRange,
defaultVal, defaultTag> *now =
new segtree<Tp, SizeT, OperPushup, OperModify, OperRange,
defaultVal, defaultTag>[bufSize];
static segtree<Tp, SizeT, OperPushup, OperModify, OperRange,
defaultVal, defaultTag> *end = now + bufSize;
return now == end
? (now = new segtree<Tp, SizeT, OperPushup, OperModify,
OperRange, defaultVal,
defaultTag>[bufSize],
end = now + bufSize, now)
: ++now;
}
/* 递归建树,传入的 `l` 应当小于等于 `r` */
void build(const SizeT &l, const SizeT &r, const Tp *a = nullptr) {
this->l = l;
this->r = r;
this->tag = defaultTag;
if (l >= r) {
this->val = a != nullptr ? a[l] : defaultVal;
return;
}
ls = newNode();
rs = newNode();
ls->build(l, mid, a);
rs->build(mid + 1, r, a);
this->pushup();
}
/* 单点修改,传入的 `pos` 应在该节点的区间范围内 */
void modify(const SizeT &pos, const Tp &val) {
if (this->l == this->r) {
this->val = OperModify()(this->val, val);
return;
}
this->pushdown();
if (ls->r >= pos) {
ls->modify(pos, val);
}
else {
rs->modify(pos, val);
}
this->pushup();
}
/* 单点查询,传入的 `pos` 应在该节点的区间范围内 */
[[nodiscard]] auto query(const SizeT &pos) -> const Tp & {
if (this->l == this->r) {
return this->val;
}
this->pushdown();
if (ls->r >= pos) {
return ls->query(pos);
}
return rs->query(pos);
}
/* 区间修改,传入的 `l`, `r` 应在该节点的区间范围内 */
void modify(const SizeT &l, const SizeT &r, const Tp &val) {
if (l <= this->l && this->r <= r) {
this->tag =
this->tag == defaultTag ? val : OperModify()(this->tag, val);
this->val =
OperModify()(this->val, OperRange()(val, this->l, this->r));
return;
}
this->pushdown();
if (ls->r >= l) {
ls->modify(l, r, val);
}
if (rs->l <= r) {
rs->modify(l, r, val);
}
this->pushup();
}
/* 区间查询,传入的 `l`, `r` 应在该节点的区间范围内 */
[[nodiscard]] auto query(const SizeT &l, const SizeT &r) -> Tp {
if (l <= this->l && this->r <= r) {
return this->val;
}
this->pushdown();
Tp res = defaultVal;
if (ls->r >= l) {
res = OperPushup()(res, ls->query(l, r));
}
if (rs->l <= r) {
res = OperPushup()(res, rs->query(l, r));
}
return res;
}
#undef mid
#undef ls
#undef rs
};