Skip to content

Commit 6ca8e25

Browse files
committed
refactor beats
1 parent 0b3f88b commit 6ca8e25

File tree

4 files changed

+142
-113
lines changed

4 files changed

+142
-113
lines changed

segmenttree/acl_beats.hpp

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#pragma once
22
#include "acl_lazysegtree.hpp"
33

4-
#include <algorithm>
5-
#include <iostream>
6-
74
template <class S, auto op, auto e, class F, auto mapping, auto composition, auto id>
85
class segtree_beats : public atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> {
96
using Base = atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>;
@@ -16,95 +13,3 @@ class segtree_beats : public atcoder::lazy_segtree<S, op, e, F, mapping, composi
1613
}
1714
}
1815
};
19-
20-
namespace RangeChMinMaxAddSum {
21-
template <typename Num> inline Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept {
22-
assert(a <= a2); // a < a2 or a == a2 == INF
23-
assert(c <= c2); // c < c2 or c == c2 == -INF
24-
return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c);
25-
}
26-
template <typename Num> inline Num second_highest(Num a, Num a2, Num b, Num b2) noexcept {
27-
assert(a >= a2); // a > a2 or a == a2 == -INF
28-
assert(b >= b2); // b > b2 or b == b2 == INF
29-
return a == b ? std::max(a2, b2) : a2 >= b ? a2 : b2 >= a ? b2 : std::min(a, b);
30-
}
31-
32-
using BNum = long long;
33-
constexpr BNum BINF = 1LL << 61;
34-
35-
struct S {
36-
BNum lo, hi, lo2, hi2, sum;
37-
unsigned sz, nlo, nhi;
38-
bool fail;
39-
S() : lo(BINF), hi(-BINF), lo2(BINF), hi2(-BINF), sum(0), sz(0), nlo(0), nhi(0), fail(0) {}
40-
S(BNum x, unsigned sz_)
41-
: lo(x), hi(x), lo2(BINF), hi2(-BINF), sum(x * sz_), sz(sz_), nlo(sz_), nhi(sz_), fail(0) {}
42-
friend std::ostream &operator<<(std::ostream &os, const S s) {
43-
return os << "[(" << s.lo << "x" << s.nlo << ", " << s.lo2 << ", " << s.hi2 << ", " << s.hi
44-
<< "x" << s.nhi << "), sz=" << s.sz << ", sum=" << s.sum << "]";
45-
}
46-
};
47-
48-
S e() { return S(); }
49-
S op(S l, S r) {
50-
if (l.lo > l.hi) return r;
51-
if (r.lo > r.hi) return l;
52-
S ret;
53-
ret.lo = std::min(l.lo, r.lo);
54-
ret.hi = std::max(l.hi, r.hi);
55-
ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2),
56-
ret.hi2 = second_highest(l.hi, l.hi2, r.hi, r.hi2);
57-
ret.sum = l.sum + r.sum;
58-
ret.sz = l.sz + r.sz;
59-
ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo);
60-
ret.nhi = l.nhi * (l.hi >= r.hi) + r.nhi * (r.hi >= l.hi);
61-
return ret;
62-
}
63-
struct F {
64-
BNum lb, ub, bias;
65-
F() : lb(-BINF), ub(BINF), bias(0) {}
66-
F(BNum chmax_, BNum chmin_, BNum add) : lb(chmax_), ub(chmin_), bias(add) {}
67-
static F chmin(BNum x) noexcept { return F(-BINF, x, BNum(0)); }
68-
static F chmax(BNum x) noexcept { return F(x, BINF, BNum(0)); }
69-
static F add(BNum x) noexcept { return F(-BINF, BINF, x); };
70-
};
71-
72-
F composition(F fnew, F fold) {
73-
F ret;
74-
ret.lb = std::max(std::min(fold.lb + fold.bias, fnew.ub), fnew.lb) - fold.bias;
75-
ret.ub = std::min(std::max(fold.ub + fold.bias, fnew.lb), fnew.ub) - fold.bias;
76-
ret.bias = fold.bias + fnew.bias;
77-
return ret;
78-
}
79-
F id() { return F(); }
80-
S mapping(F f, S x) {
81-
if (x.sz == 0) return e();
82-
83-
// f の作用後 x の要素が 1 種類だけになるケース
84-
if (x.lo == x.hi or f.lb == f.ub or f.lb >= x.hi or f.ub <= x.lo) {
85-
return S(std::min(std::max(x.lo, f.lb), f.ub) + f.bias, x.sz);
86-
}
87-
88-
// 2 種類 -> 1 種類
89-
if (x.lo2 == x.hi) {
90-
x.lo = x.hi2 = std::max(x.lo, f.lb) + f.bias;
91-
x.hi = x.lo2 = std::min(x.hi, f.ub) + f.bias;
92-
x.sum = x.lo * x.nlo + x.hi * x.nhi;
93-
return x;
94-
}
95-
96-
// lo と lo2, hi と hi2 が潰れないケース
97-
if (f.lb < x.lo2 and f.ub > x.hi2) {
98-
BNum nxt_lo = std::max(x.lo, f.lb), nxt_hi = std::min(x.hi, f.ub);
99-
x.sum += (nxt_lo - x.lo) * x.nlo - (x.hi - nxt_hi) * x.nhi + f.bias * x.sz;
100-
x.lo = nxt_lo + f.bias, x.hi = nxt_hi + f.bias, x.lo2 += f.bias, x.hi2 += f.bias;
101-
return x;
102-
}
103-
104-
x.fail = 1;
105-
return x;
106-
}
107-
108-
using segtree = segtree_beats<S, op, e, F, mapping, composition, id>;
109-
110-
} // namespace RangeChMinMaxAddSum

segmenttree/test/beats.test.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
#define PROBLEM "https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum"
2-
#include "../acl_beats.hpp"
2+
#include "../trees/range-chmin-chmax-add-range-sum.hpp"
33
#include <iostream>
44
#include <vector>
55
using namespace std;
66

7+
using RCCARS = RangeChminChmaxAddRangeSum<long long, (1LL << 60)>;
8+
79
int main() {
810
cin.tie(nullptr), ios::sync_with_stdio(false);
911
int N, Q;
1012
cin >> N >> Q;
11-
vector<RangeChMinMaxAddSum::S> A(N);
13+
vector<RCCARS::S> A(N);
1214
for (auto &a : A) {
1315
long long tmp;
14-
cin >> tmp, a = {tmp, 1};
16+
cin >> tmp, a = RCCARS::Gen(tmp);
1517
}
16-
RangeChMinMaxAddSum::segtree segtree(A);
18+
19+
RCCARS::segtree segtree(A);
20+
1721
while (Q--) {
1822
int q, l, r;
1923
long long b;
2024
cin >> q >> l >> r;
2125
if (q < 3) {
2226
cin >> b;
23-
if (q == 0) segtree.apply(l, r, RangeChMinMaxAddSum::F::chmin(b));
24-
if (q == 1) segtree.apply(l, r, RangeChMinMaxAddSum::F::chmax(b));
25-
if (q == 2) segtree.apply(l, r, RangeChMinMaxAddSum::F::add(b));
27+
if (q == 0) segtree.apply(l, r, RCCARS::Chmin(b));
28+
if (q == 1) segtree.apply(l, r, RCCARS::Chmax(b));
29+
if (q == 2) segtree.apply(l, r, RCCARS::Add(b));
2630
} else {
2731
long long ret = segtree.prod(l, r).sum;
2832
cout << ret << '\n';

segmenttree/test/beats_random_test.test.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY
22

33
#include "../../random/xorshift.hpp"
4-
#include "../acl_beats.hpp"
4+
#include "../trees/range-chmin-chmax-add-range-sum.hpp"
55

66
#include <algorithm>
77
#include <cstdio>
88
#include <numeric>
99
#include <vector>
1010

11-
using RangeChMinMaxAddSum::S, RangeChMinMaxAddSum::F;
11+
using RCCARS = RangeChminChmaxAddRangeSum<long long, (1LL << 60)>;
1212

1313
int main() {
1414
for (int trial = 0; trial < 1 << 20; ++trial) {
@@ -17,16 +17,14 @@ int main() {
1717
const int maxA = rand_int() % 50 + 1;
1818
const int Q = rand_int() % 10 + 1;
1919

20-
std::vector<S> A(N);
20+
std::vector<RCCARS::S> A(N);
2121
std::vector<int> simulate(N);
2222
for (int i = 0; i < N; ++i) {
2323
simulate.at(i) = rand_int() % (maxA + 1);
24-
A.at(i) = S(simulate.at(i), 1);
24+
A.at(i) = RCCARS::Gen(simulate.at(i));
2525
}
2626

27-
segtree_beats<S, RangeChMinMaxAddSum::op, RangeChMinMaxAddSum::e, F, RangeChMinMaxAddSum::mapping,
28-
RangeChMinMaxAddSum::composition, RangeChMinMaxAddSum::id>
29-
segtree(A);
27+
RCCARS::segtree segtree(A);
3028

3129
for (int q = 0; q < Q; ++q) {
3230
int tp = rand_int() % 4;
@@ -43,17 +41,17 @@ int main() {
4341
int b = rand_int() % (maxA + 1);
4442
if (tp == 0) {
4543
for (int i = l; i < r; ++i) simulate.at(i) = std::min(simulate.at(i), b);
46-
segtree.apply(l, r, RangeChMinMaxAddSum::F::chmin(b));
44+
segtree.apply(l, r, RCCARS::Chmin(b));
4745
}
4846

4947
if (tp == 1) {
5048
for (int i = l; i < r; ++i) simulate.at(i) = std::max(simulate.at(i), b);
51-
segtree.apply(l, r, RangeChMinMaxAddSum::F::chmax(b));
49+
segtree.apply(l, r, RCCARS::Chmax(b));
5250
}
5351

5452
if (tp == 2) {
5553
for (int i = l; i < r; ++i) simulate.at(i) += b;
56-
segtree.apply(l, r, RangeChMinMaxAddSum::F::add(b));
54+
segtree.apply(l, r, RCCARS::Add(b));
5755
}
5856
}
5957

@@ -72,7 +70,7 @@ int main() {
7270

7371
assert(prod.sum == std::accumulate(values.begin(), values.end(), 0LL));
7472

75-
assert(prod.sz == r - l);
73+
assert((int)prod.sz == r - l);
7674
assert(!prod.fail);
7775

7876
if (values.front() != values.back()) {
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#pragma once
2+
#include "../acl_beats.hpp"
3+
4+
#include <algorithm>
5+
#include <cassert>
6+
#include <type_traits>
7+
8+
template <typename Num, Num INF> struct RangeChminChmaxAddRangeSum {
9+
static_assert(std::is_signed<Num>::value, "Num must be signed");
10+
11+
struct S {
12+
Num lo, hi, lo2, hi2, sum;
13+
unsigned sz, nlo, nhi;
14+
bool fail;
15+
S() : lo(INF), hi(-INF), lo2(INF), hi2(-INF), sum(0), sz(0), nlo(0), nhi(0), fail(0) {}
16+
S(Num x, unsigned sz_)
17+
: lo(x), hi(x), lo2(INF), hi2(-INF), sum(x * sz_), sz(sz_), nlo(sz_), nhi(sz_),
18+
fail(0) {}
19+
20+
template <class OStream> friend OStream &operator<<(OStream &os, const S s) {
21+
return os << "[(" << s.lo << "x" << s.nlo << ", " << s.lo2 << ", " << s.hi2 << ", "
22+
<< s.hi << "x" << s.nhi << "), sz=" << s.sz << ", sum=" << s.sum << "]";
23+
}
24+
};
25+
26+
private:
27+
static Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept {
28+
assert(a <= a2); // a < a2 or a == a2 == INF
29+
assert(c <= c2); // c < c2 or c == c2 == -INF
30+
return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c);
31+
}
32+
33+
static Num second_highest(Num a, Num a2, Num b, Num b2) noexcept {
34+
assert(a >= a2); // a > a2 or a == a2 == -INF
35+
assert(b >= b2); // b > b2 or b == b2 == INF
36+
return a == b ? std::max(a2, b2) : a2 >= b ? a2 : b2 >= a ? b2 : std::min(a, b);
37+
}
38+
39+
static S e() { return S(); }
40+
41+
static S op(S l, S r) {
42+
if (l.lo > l.hi) return r;
43+
if (r.lo > r.hi) return l;
44+
S ret;
45+
ret.lo = std::min(l.lo, r.lo);
46+
ret.hi = std::max(l.hi, r.hi);
47+
ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2),
48+
ret.hi2 = second_highest(l.hi, l.hi2, r.hi, r.hi2);
49+
ret.sum = l.sum + r.sum;
50+
ret.sz = l.sz + r.sz;
51+
ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo);
52+
ret.nhi = l.nhi * (l.hi >= r.hi) + r.nhi * (r.hi >= l.hi);
53+
return ret;
54+
}
55+
56+
struct F {
57+
Num lb, ub, bias;
58+
F() : lb(-INF), ub(INF), bias(0) {}
59+
F(Num chmax_, Num chmin_, Num add) : lb(chmax_), ub(chmin_), bias(add) {}
60+
};
61+
62+
static F composition(F fnew, F fold) {
63+
F ret;
64+
ret.lb = std::max(std::min(fold.lb + fold.bias, fnew.ub), fnew.lb) - fold.bias;
65+
ret.ub = std::min(std::max(fold.ub + fold.bias, fnew.lb), fnew.ub) - fold.bias;
66+
ret.bias = fold.bias + fnew.bias;
67+
return ret;
68+
}
69+
70+
static F id() { return F(); }
71+
72+
static S mapping(F f, S x) {
73+
if (x.sz == 0) return e();
74+
75+
// f の作用後 x の要素が 1 種類だけになるケース
76+
if (x.lo == x.hi or f.lb == f.ub or f.lb >= x.hi or f.ub <= x.lo) {
77+
return S(std::min(std::max(x.lo, f.lb), f.ub) + f.bias, x.sz);
78+
}
79+
80+
// 2 種類 -> 1 種類
81+
if (x.lo2 == x.hi) {
82+
x.lo = x.hi2 = std::max(x.lo, f.lb) + f.bias;
83+
x.hi = x.lo2 = std::min(x.hi, f.ub) + f.bias;
84+
x.sum = x.lo * x.nlo + x.hi * x.nhi;
85+
return x;
86+
}
87+
88+
// lo と lo2, hi と hi2 が潰れないケース
89+
if (f.lb < x.lo2 and f.ub > x.hi2) {
90+
Num nxt_lo = std::max(x.lo, f.lb), nxt_hi = std::min(x.hi, f.ub);
91+
x.sum += (nxt_lo - x.lo) * x.nlo - (x.hi - nxt_hi) * x.nhi + f.bias * x.sz;
92+
x.lo = nxt_lo + f.bias, x.hi = nxt_hi + f.bias, x.lo2 += f.bias, x.hi2 += f.bias;
93+
return x;
94+
}
95+
96+
x.fail = 1;
97+
return x;
98+
}
99+
100+
public:
101+
static F Chmin(Num x) noexcept { return F(-INF, x, Num(0)); }
102+
static F Chmax(Num x) noexcept { return F(x, INF, Num(0)); }
103+
static F Add(Num x) noexcept { return F(-INF, INF, x); };
104+
105+
static S Gen(Num x, unsigned sz = 1) noexcept { return S(x, sz); }
106+
107+
using segtree = segtree_beats<S, op, e, F, mapping, composition, id>;
108+
};
109+
/* Usage:
110+
using RCCARS = RangeChminChmaxAddRangeSum<long long, (1LL << 60)>;
111+
112+
vector<RCCARS::S> init;
113+
for (long long a : A) init.push_back(RCCARS::Gen(a));
114+
115+
RCCARS::segtree segtree(init);
116+
117+
segtree.apply(l, r, RCCARS::Chmin(b));
118+
segtree.apply(l, r, RCCARS::Chmax(b));
119+
segtree.apply(l, r, RCCARS::Add(b));
120+
121+
long long ret = segtree.prod(l, r).sum;
122+
*/

0 commit comments

Comments
 (0)