类似小白逛公园的多内容线段树(例如,本题需要同时查询区间贡献与端点值)的 query / update 一定要特判,不能总是依赖 l > r 的边界特殊值,否则可能被卡!
例如,TLE:
void update(int root, int l, int r, int value) {
if (l > r) return;
if (lrange[root] >= l && rrange[root] <= r) {
modify(root, value);
return;
}
pushdown(root);
mids();
update(lcall, value); update(rcall, value);
pushup(root);
}
AC:
void update(int root, int l, int r, int value) {
if (l > r) return;
if (lrange[root] >= l && rrange[root] <= r) {
modify(root, value);
return;
}
pushdown(root);
mids();
if (r <= mid) {
update(lcall, value);
pushup(root);
return;
}
if (l > mid) {
update(rcall, value);
pushup(root);
return;
}
update(lcall, value); update(rcall, value);
pushup(root);
}