Hard time subtracting 1 :(

I have done a problem, wrote a solution, got accepted. Then I tweaked just 1 line (which was doing the same thing, subtracting 1) but solution got error. I am very confused about what could be the problem, I tried to debug but both cases had same effect while debugging.

Snippet from AC:

const int k = upper_bound(A.begin(), A.end(), t) - A.begin() - 1;
int r = k;

Change in SIGSEGV

const int k = upper_bound(A.begin(), A.end(), t) - A.begin();
int r = k - 1;

Any idea what could have changed between these two codes???

Check line 45 and 47 in both
Accepted Solution
Erroneous Solution

In your RE solution, the value of k in the line:

if (table.query(mid, k - 1) <= d)

is one higher than in your AC solution, and causes an out-of-bounds access (with the sample testcase) in the line:

 int j = log[R - L + 1];

Oh! how subtle it was. I completely forget about use of k there. Thanks @ssjgz, you got sharp eyes.

1 Like