Astral-23 / competitive-programing

競プロ用のライブラリ。 Utility/template.hpp を include しないと動かない物が多数。
Creative Commons Zero v1.0 Universal
0 stars 0 forks source link

セグメント木シリーズ #38

Open Astral-23 opened 2 months ago

Astral-23 commented 2 months ago

区間代入/区間sum

struct S {
    ll x, sz;
};

S op(S l, S r) {
    l.x += r.x;
    l.sz += r.sz;
    return l;
}
S e() { return {0, 0}; }
using F = ll;
F id() { return inf; }
S mapping(F f, S x) {
    if (f == inf) return x;
    x.x = x.sz * f;
    return x;
}
F composition(F f, F g) {
    if (f == inf) return g;
    return f;
}

https://atcoder.jp/contests/abc371/submissions/57776675

区間代入/ min . max. sum

const ll inf = LLONG_MAX / 4;
struct S {
    ll x, sz, M, m;
};

S op(S l, S r) {
    l.x += r.x;
    l.sz += r.sz;
    chmax(l.M, r.M);
    chmin(l.m, r.m);
    return l;
}
S e() { return {0, 0, -inf, inf}; }
using F = ll;
F id() { return inf; }
S mapping(F f, S x) {
    if (f == inf) return x;
    x.x = x.sz * f;
    x.M = f;
    x.m = f;
    return x;
}
F composition(F f, F g) {
    if (f == inf) return g;
    return f;
}

https://atcoder.jp/contests/abc371/submissions/58548762

Astral-23 commented 2 days ago

range affine range sum

struct S {
    ll s;
    ll sz;
};
S op(S l, S r) { return S{l.s + r.s, l.sz + r.sz}; }
S e() { return S{0, 0}; }
struct F {
    ll a, b;
};
S mapping(F f, S s) {
    S res;
    res.s = f.a * s.s + f.b * s.sz;
    res.sz = s.sz;
    return res;
}
F composition(F l, F r) { return F{l.a * r.a, l.a * r.b + l.b}; }
F id() { return F{1, 0}; }
using segtree = lazysegtree<S, op, e, F, mapping, composition, id>;

https://judge.yosupo.jp/submission/226359