Skip to content

Commit fa39eaf

Browse files
authored
Merge pull request #409 from hitonanode/fix-segtrees
Fix segtrees
2 parents 0b3f88b + 49a5c00 commit fa39eaf

File tree

7 files changed

+231
-185
lines changed

7 files changed

+231
-185
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()) {

segmenttree/test/beats_range-add-chmax-range-sum.test.cpp renamed to segmenttree/test/beats_range-chmax-add-range-sum.aoj0427.test.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
#include "../trees/acl_range-add-chmax-range-sum.hpp"
1+
#include "../trees/range-chmax-add-range-sum.hpp"
22
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0427"
33
#include <iostream>
44
using namespace std;
55

6+
using RCARS = RangeChmaxAddRangeSum<long long, (1LL << 60)>;
7+
68
int main() {
79
cin.tie(nullptr), ios::sync_with_stdio(false);
810

911
int N, tmp;
1012
cin >> N;
11-
vector<RangeAddChmaxRangeSum::S> A(N);
12-
for (auto &a : A) cin >> tmp, a = tmp;
13+
vector<RCARS::S> A(N);
14+
for (auto &a : A) cin >> tmp, a = RCARS::Gen(tmp);
1315

14-
RangeAddChmaxRangeSum::segtree segtree(A);
16+
RCARS::segtree segtree(A);
1517

1618
int Q, q, l, r, d;
1719
cin >> Q;
@@ -20,12 +22,12 @@ int main() {
2022
cin >> q >> l >> r >> d;
2123
l--;
2224
if (q == 1) {
23-
segtree.apply(l, r, RangeAddChmaxRangeSum::F::add(d));
25+
segtree.apply(l, r, RCARS::Add(d));
2426
} else {
2527
auto before = segtree.prod(l, r).sum;
26-
auto f1 = RangeAddChmaxRangeSum::F::add(-d);
27-
auto f2 = RangeAddChmaxRangeSum::F::chmax(0);
28-
segtree.apply(l, r, RangeAddChmaxRangeSum::composition(f2, f1));
28+
auto f1 = RCARS::Add(-d);
29+
auto f2 = RCARS::Chmax(0);
30+
segtree.apply(l, r, RCARS::composition(f2, f1));
2931
auto after = segtree.prod(l, r).sum;
3032
cout << before - after << '\n';
3133
}

segmenttree/trees/acl_range-add-chmax-range-sum.hpp

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
#include "../acl_beats.hpp"
3+
#include <algorithm>
4+
#include <type_traits>
5+
6+
// Verified: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0427
7+
template <typename Num, Num INF> struct RangeChmaxAddRangeSum {
8+
static_assert(std::is_signed<Num>::value, "Num must be signed");
9+
10+
static Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept { // a < a2, c < c2
11+
return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c);
12+
}
13+
14+
struct S {
15+
Num lo, lo2, sum;
16+
unsigned sz, nlo;
17+
bool fail;
18+
S() : lo(INF), lo2(INF), sum(0), sz(0), nlo(0), fail(false) {}
19+
S(Num x, unsigned sz_ = 1)
20+
: lo(x), lo2(INF), sum(Num(x) * sz_), sz(sz_), nlo(sz_), fail(false) {}
21+
};
22+
23+
private:
24+
static S e() { return S(); }
25+
26+
static S op(S l, S r) {
27+
S ret;
28+
ret.lo = std::min(l.lo, r.lo);
29+
ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2);
30+
ret.sum = l.sum + r.sum, ret.sz = l.sz + r.sz;
31+
ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo);
32+
return ret;
33+
}
34+
35+
struct F {
36+
Num lb, bias;
37+
F() : lb(-INF), bias(0) {}
38+
F(Num chmax_, Num add) : lb(chmax_), bias(add) {}
39+
};
40+
41+
static F id() { return F(); }
42+
43+
static S mapping(F f, S x) {
44+
if (x.sz == 0) return e();
45+
if (f.lb < x.lo2) {
46+
Num nxt_lo = std::max(x.lo, f.lb);
47+
x.sum += (nxt_lo - x.lo) * x.nlo + f.bias * x.sz;
48+
x.lo = nxt_lo + f.bias, x.lo2 += f.bias;
49+
return x;
50+
}
51+
x.fail = 1;
52+
return x;
53+
}
54+
55+
public:
56+
static F composition(F fnew, F fold) {
57+
F ret;
58+
ret.lb = std::max(fold.lb + fold.bias, fnew.lb) - fold.bias;
59+
ret.bias = fold.bias + fnew.bias;
60+
return ret;
61+
}
62+
63+
static F Chmax(Num x) noexcept { return F(x, Num(0)); }
64+
static F Add(Num x) noexcept { return F(-INF, x); };
65+
66+
static S Gen(Num x, unsigned sz = 1) { return S{x, sz}; }
67+
68+
using segtree = segtree_beats<S, op, e, F, mapping, composition, id>;
69+
};
70+
/* Usage:
71+
using RCARS = RangeChmaxAddRangeSum<long long, (1LL << 60)>;
72+
vector<RCARS::S> init;
73+
for (auto a : A) init.push_back(RCARS::Gen(a, 1));
74+
RCARS::segtree tree(init);
75+
76+
tree.apply(l, r, RCARS::Chmax(x));
77+
tree.apply(l, r, RCARS::Add(x));
78+
auto p = tree.prod(l, r);
79+
*/

0 commit comments

Comments
 (0)