Parallel Binary Search
Author: Muaath Alqarni
Offline way to do multiple binary search on answer efficiently
Prerequisites
- Silver - Binary Search
- divide-and-conquer
Resources
Resources | ||||
---|---|---|---|---|
CF | ||||
SPBook |
Focus Problem – try your best to solve this problem before continuing!
View Internal SolutionSolution - 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 = , if the two nodes where connected in DSU, it means the answer is , otherwise it means .
Complexity:
We need to do the sweep times, same as binary search. Sorting the queries by median is And for the DSU
Total Complexity:
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
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
HR | Easy | Show TagsDSU, PBS | |||
AC | Easy | Show TagsDSU, PBS | |||
COCI | Normal | 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!