Skip to content

Commit f7cae8b

Browse files
committed
speedup one test
1 parent e3980ba commit f7cae8b

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

library/data_structures/seg_tree_uncommon/merge_sort_tree.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ struct merge_sort_tree {
1313
begin(tree[i]));
1414
}
1515
}
16-
//! count of i in [l, r) such that a[i] in [vl, vr)
16+
//! count of i in [l, r) such that a[i] < x
1717
//! @time O(log(n)^2)
1818
//! @space O(1)
19-
int query(int l, int r, int vl, int vr) {
20-
return query_impl(l, r, vl, vr, 0, n, 1);
19+
int query(int l, int r, int x) {
20+
return query_impl(l, r, x, 0, n, 1);
2121
}
22-
int query_impl(int l, int r, int vl, int vr, int tl,
23-
int tr, int v) {
22+
int query_impl(int l, int r, int x, int tl, int tr,
23+
int v) {
2424
if (r <= tl || tr <= l) return 0;
2525
if (l <= tl && tr <= r)
26-
return ranges::lower_bound(tree[v], vr) -
27-
ranges::lower_bound(tree[v], vl);
26+
return ranges::lower_bound(tree[v], x) -
27+
begin(tree[v]);
2828
int tm = split(tl, tr);
29-
return query_impl(l, r, vl, vr, tl, tm, 2 * v) +
30-
query_impl(l, r, vl, vr, tm, tr, 2 * v + 1);
29+
return query_impl(l, r, x, tl, tm, 2 * v) +
30+
query_impl(l, r, x, tm, tr, 2 * v + 1);
3131
}
3232
};

tests/library_checker_aizu_tests/strings/lcs_queries_merge_sort_tree.test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/prefix_substring_lcs"
3+
// as debug mode causes lower_bound (in MST) to be O(n)
4+
#undef _GLIBCXX_DEBUG
35
#include "../template.hpp"
46
#include "../../../library/strings/longest_common_subsequence/lcs_dp.hpp"
57
#include "../../../library/data_structures/seg_tree_uncommon/merge_sort_tree.hpp"
@@ -30,7 +32,7 @@ int main() {
3032
for (int i = 0; i < q; i++) {
3133
int a, b, c;
3234
cin >> a >> b >> c;
33-
cout << msts[a].query(b, c, -1, b) << '\n';
35+
cout << msts[a].query(b, c, b) << '\n';
3436
}
3537
return 0;
3638
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#define PROBLEM \
2+
"https://judge.yosupo.jp/problem/prefix_substring_lcs"
3+
#include "../template.hpp"
4+
#include "../../../library/strings/longest_common_subsequence/lcs_dp.hpp"
5+
#include "../../../library/data_structures/seg_tree_uncommon/wavelet_matrix.hpp"
6+
int main() {
7+
cin.tie(0)->sync_with_stdio(0);
8+
int q;
9+
string s, t;
10+
cin >> q >> s >> t;
11+
vector<wavelet_matrix> msts;
12+
{
13+
lcs_dp lcs(t);
14+
msts.emplace_back(vector<ull>(all(lcs.dp)));
15+
for (char c : s) {
16+
lcs.push_onto_s(c);
17+
{
18+
vector<bool> seen(sz(t));
19+
for (int i = 0; i < sz(t); i++) {
20+
int val = lcs.dp[i];
21+
assert(val <= i);
22+
if (val == -1) continue;
23+
assert(!seen[val]);
24+
seen[val] = 1;
25+
}
26+
}
27+
msts.emplace_back(vector<ull>(all(lcs.dp)));
28+
}
29+
}
30+
for (int i = 0; i < q; i++) {
31+
int a, b, c;
32+
cin >> a >> b >> c;
33+
cout << msts[a].count(b, c, b) << '\n';
34+
}
35+
return 0;
36+
}

0 commit comments

Comments
 (0)