Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<unsigned long long> findOneIndices() {
- vector<unsigned long long> result;
- unsigned long long L = 1; // assuming 1-indexing
- const unsigned long long M = (1ULL << 62) - 1; // size of the array
- // Continue searching as long as there is at least one '1' from L to M.
- while (L <= M && query(L, M) == 1) {
- unsigned long long lo = L, hi = M;
- // Binary search for the leftmost '1' in the range [L, M].
- while (lo < hi) {
- unsigned long long mid = lo + (hi - lo) / 2;
- if (query(L, mid) == 1) {
- hi = mid; // there's a '1' in [L, mid]
- } else {
- lo = mid + 1;
- }
- }
- // 'lo' is now the index of the leftmost '1'
- result.push_back(lo);
- L = lo + 1; // Move past this found '1'
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement