Skip to content

Commit 49a5c00

Browse files
committed
refactor beats
1 parent 6ca8e25 commit 49a5c00

File tree

3 files changed

+89
-72
lines changed

3 files changed

+89
-72
lines changed

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)