PrevNext
Has Not Appeared
 0/4

Parallel Binary Search

Author: Muaath Alqarni

Answering multiple binary search queries parallelly

Edit This Page

Resources

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

View Internal Solution

Solution - New Roads Queries

Let's think how to binary search a single query. It's clear that we can use DSU with linear search, but also we can do binary search with DSU in O(Nlog2N)O(N \cdot \log_2{N}).

Now let's use parallel binary search: Sweep through the array of edges and union them in the DSU, whenevery you find a query its median = ii, if the two nodes where connected, it means shrink to left, otherwise shrink to right.

You need to do the sweep O(log2N)O(\log_2{N}) times, so you know the exact time the nodes where connected. And you need to sort the queries by median before every sweep thats O(Qlog2Q)O(Q \cdot \log_2{Q}) And O(αN)O(\alpha{N}) for the DSU

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

Implementation - New Roads Queries

C++

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = 2e5 + 1;
int n, m, q;
vector<pii> edges;

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