Data Structures & AlgorithmsVery hardCodeforces Div2 E · offline queries
Problem: Mo's algorithm answers q range queries on an array of length n by sorting queries into blocks of size about sqrt(n) and moving the window pointers. What is its total running time?
- A. O((n + q) * sqrt(n))
- B. O(n + q)
- C. O(n * sqrt(q) * log n)
- D. O((n + q) * log n)
Correct answer: A. O((n + q) * sqrt(n))
Within each block the right pointer moves monotonically, giving O(n sqrt n) per block sweep and O(q sqrt n) pointer moves over all queries. The total is O((n + q) sqrt n).
More Coding Interview Problems questions
- Problem: In a segment tree with lazy propagation, what is the cost of one range-add update on an array of length n?
- Problem: What does the following print? python print(-7 // 2, -7 % 2, 7 // -2, int(-7 / 2))
- Problem: What does the following print? python print(bin(12 & -12), 12 & -12)
- Problem: Coins of values {1, 3, 4} are available, unlimited copies each. To make amount 6, the greedy algorithm always takes the largest…
- Problem: What does the following print? python def ed(a, b): m, n = len(a), len(b) dp = [[0] (n + 1) for in range(m + 1)] for i in range(m…
- Problem: A solver runs Dijkstra from A on a directed graph. Which graph makes Dijkstra output a wrong shortest distance?