Skip to content

Commit 9195e8d

Browse files
committed
delete _prod
1 parent 9c7e65e commit 9195e8d

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed

cpp/merge-sort-tree.hpp

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,8 @@ class MergeSortTree {
5656
int j0 = (h - 1) * sz + i;
5757
int last1 = j2;
5858
int last2 = h * sz + std::min(n, i + t * 2);
59-
while (j1 != last1 or j2 != last2) {
60-
if (j1 == last1) {
61-
key_data[j0] = key_data[j2];
62-
value_data[j0] = value_data[j2];
63-
j0++;
64-
j2++;
65-
} else if (j2 == last2) {
66-
key_data[j0] = key_data[j1];
67-
value_data[j0] = value_data[j1];
68-
j0++;
69-
j1++;
70-
} else if (comp(key_data[j2], key_data[j1])) {
59+
while (j1 != last1 || j2 != last2) {
60+
if (j1 == last1 || (j2 < last2 && comp(key_data[j2], key_data[j1]))) {
7161
key_data[j0] = key_data[j2];
7262
value_data[j0] = value_data[j2];
7363
j0++;
@@ -79,10 +69,7 @@ class MergeSortTree {
7969
j1++;
8070
}
8171
}
82-
83-
if (i < n) {
84-
cumulative_value[(h - 1) * sz + i] = value_data[(h - 1) * sz + i];
85-
}
72+
cumulative_value[(h - 1) * sz + i] = value_data[(h - 1) * sz + i];
8673
for (int j = i + 1; j < std::min(n, i + t * 2); j++) {
8774
cumulative_value[(h - 1) * sz + j] = op(cumulative_value[(h - 1) * sz + j - 1], value_data[(h - 1) * sz + j]);
8875
}
@@ -92,14 +79,14 @@ class MergeSortTree {
9279
}
9380

9481
value_type _prod_section(int l, int r, std::optional<key_type> a, std::optional<key_type> b) const {
95-
value_type ret = e();
82+
value_type ret = cumulative_value[r - 1];
9683
if (b.has_value()) {
9784
int i = std::lower_bound(key_data.begin() + l, key_data.begin() + r, b.value(), comp) - key_data.begin();
9885
if (i != l) {
9986
ret = cumulative_value[i - 1];
87+
} else {
88+
ret = e();
10089
}
101-
} else {
102-
ret = cumulative_value[r - 1];
10390
}
10491
if (a.has_value()) {
10592
int i = std::lower_bound(key_data.begin() + l, key_data.begin() + r, a.value(), comp) - key_data.begin();
@@ -109,24 +96,6 @@ class MergeSortTree {
10996
}
11097
return ret;
11198
}
112-
value_type _prod(int l, int r, std::optional<key_type> a, std::optional<key_type> b) const {
113-
value_type ret = e();
114-
int h = height - 1;
115-
int t = 1;
116-
while (l < r) {
117-
if (l & t) {
118-
ret = op(ret, _prod_section(h * sz + l, h * sz + l + t, a, b));
119-
l += t;
120-
}
121-
if (r & t) {
122-
r -= t;
123-
ret = op(ret, _prod_section(h * sz + r, h * sz + r + t, a, b));
124-
}
125-
h--;
126-
t <<= 1;
127-
}
128-
return ret;
129-
}
13099

131100
public:
132101
MergeSortTree() = default;
@@ -155,11 +124,31 @@ class MergeSortTree {
155124

156125
/**
157126
* @brief product value[i] s.t. a <= key[i] < b , i in [l, r)
127+
*
128+
* @param l 半開区間の開始
129+
* @param r 半開区間の終端 0<=l<=r<=n
130+
* @param a nulloptの場合は負の無限大
131+
* @param b nulloptの場合は正の無限大
158132
*/
159-
value_type prod(std::optional<int> l = std::nullopt, std::optional<int> r = std::nullopt, std::optional<key_type> a = std::nullopt, std::optional<key_type> b = std::nullopt) const {
160-
if (a.has_value() and b.has_value() and not comp(a.value(), b.value())) return e();
161-
if (l.has_value() and r.has_value() and l >= r) return e();
162-
return _prod(l.value_or(0), r.value_or(n), a, b);
133+
value_type prod(int l, int r, std::optional<key_type> a = std::nullopt, std::optional<key_type> b = std::nullopt) const {
134+
assert(0 <= l && l <= r && r <= n);
135+
if (a.has_value() && b.has_value() && !comp(a.value(), b.value())) return e();
136+
value_type ret = e();
137+
int h = height - 1;
138+
int t = 1;
139+
while (l < r) {
140+
if (l & t) {
141+
ret = op(ret, _prod_section(h * sz + l, h * sz + l + t, a, b));
142+
l += t;
143+
}
144+
if (r & t) {
145+
r -= t;
146+
ret = op(ret, _prod_section(h * sz + r, h * sz + r + t, a, b));
147+
}
148+
h--;
149+
t <<= 1;
150+
}
151+
return ret;
163152
}
164153
};
165154

0 commit comments

Comments
 (0)