PrevNext
Has Not Appeared
 0/4

Parallel Binary Search

Author: Muaath Alqarni

Offline way to do multiple binary search on answer efficiently

Edit This Page

Prerequisites

Resources

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

View Internal Solution

Solution - New Roads Queries

Let's think about how to use binary to answer single query. We can see that connvectivity of the nodes is a monotonic function.

Now let's use parallel binary search:

First, sort the queries by median.

Then sweep through the array of edges and union them in the DSU, whenevery there is a query with median = ii, if the two nodes where connected in DSU, it means the answer is i\leq i, otherwise it means >i\gt i.

Complexity:

We need to do the sweep O(log2N)O(\log_2{N}) times, same as binary search. Sorting the queries by median is O(Qlog2Q)O(Q \cdot \log_2{Q}) And O(α(N))O(\alpha(N)) for the DSU

Total Complexity: O((N+Q)log2(N+Q)2α(N))O((N + Q) \cdot \log_2(N+Q)^2 \cdot \alpha(N))

Implementation - New Roads Queries

C++

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
struct DSU {
int cnt[N], par[N];
void Init(int sz) {
for (int i = 0; i <= sz; i++) par[i] = i, cnt[i] = 1;
}

Problems

StatusSourceProblem NameDifficultyTags
HREasy
Show TagsDSU, PBS
ACEasy
Show TagsDSU, PBS
COCINormal
Show TagsPBS, Segtree

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