Skip to content

Commit db820bc

Browse files
authored
Merge pull request #332 from hitonanode/hotfix/fix-beats
Hotfix/fix beats
2 parents a058736 + b42e508 commit db820bc

File tree

3 files changed

+122
-14
lines changed

3 files changed

+122
-14
lines changed

segmenttree/acl_beats.hpp

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

4-
// CUT begin
54
template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F),
65
F (*id)()>
76
class segtree_beats : public atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> {
@@ -19,12 +18,14 @@ class segtree_beats : public atcoder::lazy_segtree<S, op, e, F, mapping, composi
1918
namespace RangeChMinMaxAddSum {
2019
#include <algorithm>
2120

22-
template <typename Num>
23-
inline Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept { // a < a2, c < c2
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
2424
return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c);
2525
}
26-
template <typename Num>
27-
inline Num second_highest(Num a, Num a2, Num b, Num b2) noexcept { // a > a2, b > b2
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
2829
return a == b ? std::max(a2, b2) : a2 >= b ? a2 : b2 >= a ? b2 : std::min(a, b);
2930
}
3031

@@ -36,7 +37,7 @@ struct S {
3637
unsigned sz, nlo, nhi;
3738
bool fail;
3839
S() : lo(BINF), hi(-BINF), lo2(BINF), hi2(-BINF), sum(0), sz(0), nlo(0), nhi(0), fail(0) {}
39-
S(BNum x, unsigned sz_ = 1)
40+
S(BNum x, unsigned sz_)
4041
: lo(x), hi(x), lo2(BINF), hi2(-BINF), sum(x * sz_), sz(sz_), nlo(sz_), nhi(sz_), fail(0) {}
4142
friend std::ostream &operator<<(std::ostream &os, const S s) {
4243
return os << "[(" << s.lo << "x" << s.nlo << ", " << s.lo2 << ", " << s.hi2 << ", " << s.hi
@@ -49,10 +50,12 @@ S op(S l, S r) {
4950
if (l.lo > l.hi) return r;
5051
if (r.lo > r.hi) return l;
5152
S ret;
52-
ret.lo = std::min(l.lo, r.lo), ret.hi = std::max(l.hi, r.hi);
53+
ret.lo = std::min(l.lo, r.lo);
54+
ret.hi = std::max(l.hi, r.hi);
5355
ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2),
5456
ret.hi2 = second_highest(l.hi, l.hi2, r.hi, r.hi2);
55-
ret.sum = l.sum + r.sum, ret.sz = l.sz + r.sz;
57+
ret.sum = l.sum + r.sum;
58+
ret.sz = l.sz + r.sz;
5659
ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo);
5760
ret.nhi = l.nhi * (l.hi >= r.hi) + r.nhi * (r.hi >= l.hi);
5861
return ret;
@@ -76,21 +79,32 @@ F composition(F fnew, F fold) {
7679
F id() { return F(); }
7780
S mapping(F f, S x) {
7881
if (x.sz == 0) return e();
79-
if (x.lo == x.hi or f.lb == f.ub or f.lb >= x.hi or f.ub < x.lo)
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) {
8085
return S(std::min(std::max(x.lo, f.lb), f.ub) + f.bias, x.sz);
86+
}
87+
88+
// 2 種類 -> 1 種類
8189
if (x.lo2 == x.hi) {
82-
x.lo = x.hi2 = std::max(x.lo, f.lb) + f.bias, x.hi = x.lo2 = std::min(x.hi, f.ub) + f.bias;
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;
8392
x.sum = x.lo * x.nlo + x.hi * x.nhi;
8493
return x;
8594
}
95+
96+
// lo と lo2, hi と hi2 が潰れないケース
8697
if (f.lb < x.lo2 and f.ub > x.hi2) {
8798
BNum nxt_lo = std::max(x.lo, f.lb), nxt_hi = std::min(x.hi, f.ub);
8899
x.sum += (nxt_lo - x.lo) * x.nlo - (x.hi - nxt_hi) * x.nhi + f.bias * x.sz;
89100
x.lo = nxt_lo + f.bias, x.hi = nxt_hi + f.bias, x.lo2 += f.bias, x.hi2 += f.bias;
90101
return x;
91102
}
103+
92104
x.fail = 1;
93105
return x;
94106
}
107+
95108
using segtree = segtree_beats<S, op, e, F, mapping, composition, id>;
109+
96110
} // namespace RangeChMinMaxAddSum
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY
2+
3+
#include "../../random/xorshift.hpp"
4+
#include "../acl_beats.hpp"
5+
6+
#include <algorithm>
7+
#include <cstdio>
8+
#include <numeric>
9+
#include <vector>
10+
11+
using RangeChMinMaxAddSum::S, RangeChMinMaxAddSum::F;
12+
13+
int main() {
14+
for (int trial = 0; trial < 1 << 20; ++trial) {
15+
const int N = rand_int() % 32 + 1;
16+
17+
const int maxA = rand_int() % 50 + 1;
18+
const int Q = rand_int() % 10 + 1;
19+
20+
std::vector<S> A(N);
21+
std::vector<int> simulate(N);
22+
for (int i = 0; i < N; ++i) {
23+
simulate.at(i) = rand_int() % (maxA + 1);
24+
A.at(i) = S(simulate.at(i), 1);
25+
}
26+
27+
segtree_beats<S, RangeChMinMaxAddSum::op, RangeChMinMaxAddSum::e, F, RangeChMinMaxAddSum::mapping,
28+
RangeChMinMaxAddSum::composition, RangeChMinMaxAddSum::id>
29+
segtree(A);
30+
31+
for (int q = 0; q < Q; ++q) {
32+
int tp = rand_int() % 4;
33+
if (q == Q - 1) tp = 3;
34+
35+
int l = 0, r = 0;
36+
while (l == r) {
37+
l = rand_int() % (N + 1);
38+
r = rand_int() % (N + 1);
39+
if (l > r) std::swap(l, r);
40+
}
41+
42+
if (tp < 3) {
43+
int b = rand_int() % (maxA + 1);
44+
if (tp == 0) {
45+
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));
47+
}
48+
49+
if (tp == 1) {
50+
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));
52+
}
53+
54+
if (tp == 2) {
55+
for (int i = l; i < r; ++i) simulate.at(i) += b;
56+
segtree.apply(l, r, RangeChMinMaxAddSum::F::add(b));
57+
}
58+
}
59+
60+
if (tp == 3) {
61+
auto prod = segtree.prod(l, r);
62+
63+
std::vector<int> values;
64+
for (int i = l; i < r; ++i) values.push_back(simulate.at(i));
65+
std::sort(values.begin(), values.end());
66+
67+
assert(prod.lo == values.front());
68+
assert(prod.nlo == std::count(values.begin(), values.end(), prod.lo));
69+
70+
assert(prod.hi == values.back());
71+
assert(prod.nhi == std::count(values.begin(), values.end(), prod.hi));
72+
73+
assert(prod.sum == std::accumulate(values.begin(), values.end(), 0LL));
74+
75+
assert(prod.sz == r - l);
76+
assert(!prod.fail);
77+
78+
if (values.front() != values.back()) {
79+
int i = 0;
80+
while (values.at(i) == values.front()) ++i;
81+
assert(prod.lo2 == values.at(i));
82+
83+
i = (int)values.size() - 1;
84+
while (values.at(i) == values.back()) --i;
85+
assert(prod.hi2 == values.at(i));
86+
}
87+
}
88+
}
89+
}
90+
91+
puts("Hello World");
92+
}

segmenttree/trees/acl_range-update-gcd-range-max-sum.hpp

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

4-
// CUT begin
4+
#include <cstdint>
5+
#include <numeric>
6+
57
// Verified: https://yukicoder.me/submissions/611774
68
namespace yuki880 {
79
constexpr uint32_t BINF = 1 << 30;
@@ -28,7 +30,7 @@ S op(S l, S r) {
2830
ret.sum = l.sum + r.sum;
2931
ret.lcm = (l.lcm >= BINF or r.lcm >= BINF)
3032
? BINF
31-
: std::min<uint64_t>(BINF, (uint64_t)l.lcm * r.lcm / std::__gcd(l.lcm, r.lcm));
33+
: std::min<uint64_t>(BINF, (uint64_t)l.lcm * r.lcm / std::gcd(l.lcm, r.lcm));
3234
ret.sz = l.sz + r.sz;
3335
if (l.all_same and r.all_same and l.max == r.max) ret.all_same = true;
3436
return ret;
@@ -43,7 +45,7 @@ struct F {
4345
};
4446

4547
F composition(F fnew, F fold) {
46-
return fnew.reset ? fnew : F(std::__gcd(fnew.dogcd, fold.dogcd), fold.reset);
48+
return fnew.reset ? fnew : F(std::gcd(fnew.dogcd, fold.dogcd), fold.reset);
4749
}
4850

4951
F id() { return F(); }
@@ -53,7 +55,7 @@ S mapping(F f, S x) {
5355
if (f.reset) x = S(f.reset, x.sz);
5456
if (f.dogcd) {
5557
if (x.all_same)
56-
x = S(std::__gcd(f.dogcd, x.max), x.sz);
58+
x = S(std::gcd(f.dogcd, x.max), x.sz);
5759
else if (f.dogcd and (x.lcm == BINF or f.dogcd % x.lcm))
5860
x.fail = true;
5961
}

0 commit comments

Comments
 (0)