跳到正文

剪贴板

剪贴板 lsfbgyik

原文
剪贴板信息

剪贴板信息

收录
更新
C++
namespace psd {
struct T {
    sint lc, rc;
    sint l, r;
    int v;
    sint m() { return (l + r) >> 1; }
    bool lowest() { return l == r; }
};
T t[N * LG];
sint ts;
sint load(sint l, sint r) {
    t[++ts] = {0, 0, l, r, 0};
    return ts;
}
sint clone(sint o) {
    assert(o);
    sint p = load(0, 0);
    t[p] = t[o];
    return p;
}
void up(sint o) { t[o].v = t[t[o].lc].v + t[t[o].rc].v; }
void build(sint &o, sint l, sint r) {
    o = load(l, r);
    if (t[o].lowest()) {
        return;
    }
    int mid = t[o].m();
    build(t[o].lc, l, mid);
    build(t[o].rc, mid + 1, r);
}
void update(sint &o, sint x, sint y) {
    o = clone(o);
    if (t[o].lowest()) {
        t[o].v += y;
        return;
    }
    int mid = t[o].m();
    if (x <= mid) {
        update(t[o].lc, x, y);
    } else {
        update(t[o].rc, x, y);
    }
    up(o);
}
int query(sint &o, sint x, sint y) {
    if (x <= t[o].l && t[o].r <= y) {
        return t[o].v;
    }
    int mid = t[o].m();
    int ret = 0;
    if (x <= mid) {
        ret = query(t[o].lc, x, y);
    }
    if (y > mid) {
        ret += query(t[o].rc, x, y);
    }
    return ret;
}
} // namespace psd