File tree Expand file tree Collapse file tree 2 files changed +8
-22
lines changed
library/data_structures_[l,r]/seg_tree_uncommon
tests/library_checker_aizu_tests/data_structures Expand file tree Collapse file tree 2 files changed +8
-22
lines changed Original file line number Diff line number Diff line change 1- int go (int l, int r) {
2- // return __lg(min(l & -l, r - l + 1));
3- // return __lg(min((l + n) & -(l + n), r - l + 1));
4- return min (__builtin_ctz (l + n), __lg (r - l + 1 ));
5- }
61int walk (int l, int r, const auto & f) {
7- /*
8- for (l += n, r += n; l <= r;)
9- if (int u = nxt(l, r); f(s[u])) {
10- while (u < n)
11- if (f(s[2 * u])) u *= 2;
12- else (u *= 2)++;
13- return u - n;
14- }
15- */
16- int good=0 ;
17- while (l <= r-good) {
18- int u = l + n, x = __lg (min (u & -u, r - l +1 -good));
19- if (f (s[u >> x])) good=1 ,r = l + (1 << x) - 1 ;
2+ while (l <= r) {
3+ int u = l + n, x = __lg (min (u & -u, r - l + 1 ));
4+ if (f (s[u >> x])) r = l + (1 << x) - 2 ;
205 else l += 1 << x;
216 }
22- return good ? l : -1 ;
23- // return l;
7+ return l;
248}
Original file line number Diff line number Diff line change @@ -22,9 +22,11 @@ int main() {
2222 cout << st.query (k, k) << ' \n ' ;
2323 } else if (type == 3 ) {
2424 // returns first element in [k,n-1] such that sum > 0
25- cout << st.walk (k, n - 1 , [&](int sum) {
25+ int idx = st.walk (k, n - 1 , [&](int sum) {
2626 return sum > 0 ;
27- }) << ' \n ' ;
27+ });
28+ if (idx == n) idx = -1 ;
29+ cout<<idx<<' \n ' ;
2830 } else {
2931 assert (type == 4 );
3032 int total = st.query (0 , k);
You can’t perform that action at this time.
0 commit comments