PrevNext
Has Not Appeared
 0/7

Parallel Binary Search

Author: Talha Taki

Parallel binary search optimizes query efficiency by splitting queries based on the relevance of specific ranges within the binary search. Rather than repeatedly querying the same range, this technique intelligently divides the search process to enhance overall performance.

Edit This Page

Focus Problem – try your best to solve this problem before continuing!

View Internal Solution

Tutorial

This section is not complete.

Any help would be appreciated! Just submit a Pull Request on Github.

Pseudocode

Let event[i]\text{event[i]} corresponds to some stuff that needed be done at that particular index ii (loihilo \leq i \leq hi)

For all O(logMaxAns)\mathcal{O}(\log MaxAns) levels, we maintain independent data structure DS[level]\text{DS[level]}. Associated with every data structure we have our monotonic function DS[level].f()\text{DS[level].f()} and DS[level].add()\text{DS[level].add()} for maintaining ongoing events at that level.

A pseudocode for parallel binary search looks something like this:

parallel_binary_search(l, r, level, candidate):
    m = candidate.size

	if m == 1:
        for i = 0 to m-1:
        ans[candidate[i]] = l

    candidate.clean()
    return

    mid = floor((l + r) / 2)

    while pointer[level] <= mid:
        DS[level].add(event[pointer[level]])
        pointer[level] = pointer[level] + 1


    for i = 0 to m-1:
        if DS[level].f(lo, mid, candidate[i]) == True:
            satisfied_candidate.add(candidate[i])
        else:
            unsatisfied_candidate.add(candidate[i])

    candidate.clean()

    parallel_binary_search(l, mid, level + 1, satisfied_candidate)
    parallel_binary_search(mid + 1, r, level + 1, unsatisfied_candidate)

// Calculating answers for the range [lo, hi] where `relevant_candidate` belongs to that range
parallel_binary_search(lo, hi, 0, relevant_candidate)

Generalized Complexity

This section is not complete.

Any help would be appreciated! Just submit a Pull Request on Github.

C++

Solution - New Road Queries

In this problem, the required data structure is DSU for knowing whether there is a path between two vertexes after adding some edges of the graph. Here:

event[i]\text{event[i]} = Add iith edge.

DS[level].add()\text{DS[level].add()} = DSU[level].Union()\text{DSU[level].Union()}

DS[level].f()\text{DS[level].f()} = DSU[level].Find()\text{DSU[level].Find()}

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAX_LOG = 20;
struct Disjoint_Set_Union {
int n;

Problems

This section is not complete.

Any help would be appreciated! Just submit a Pull Request on Github.
  • Tags, difficulty yet to be justified.
  • Internal solutions need to be added.
  • More problems are welcomed
StatusSourceProblem NameDifficultyTags
ACEasy
Show Tagsbinary-search, dsu
COCINormal
Show Tagspersistent
POINormal
Show TagsPURS, binary-search
TCNormal
HDUNormal
HRHard

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

PrevNext