Embercode
✕
Segment TreesQuick quiz

Sparse table and GCD

8 questions. Covers Range GCD and why it is not invertible.

Questions 1 to 5 of 8

  1. 1. Predict the output of this program. It prints one line with four numbers. ``cpp #include <algorithm> #include <bit> #include <cstddef> #include <iostream> #include <vector> class SparseMin { public: explicit SparseMin(const std::vector<int>& values) { const std::size_t n = values.size(); table_.push_back(values); for (std::size_t len = 2; len <= n; len *= 2) { const std::vector<int>& prev = table_.back(); const std::size_t half = len / 2; std::vector<int> level(n - len + 1); for (std::size_t i = 0; i + len <= n; ++i) { level[i] = std::min(prev[i], prev[i + half]); } table_.push_back(std::move(level)); } } int query(std::size_t l, std::size_t r) const { const std::size_t len = r - l + 1; const std::size_t k = std::bit_width(len) - 1; return std::min(table_[k][l], table_[k][r + 1 - (std::size_t{1} << k)]); } private: std::vector<std::vector<int>> table_; }; int main() { const SparseMin sp({9, 4, 7, 2, 8, 6}); std::cout << sp.query(0, 5) << ' ' << sp.query(1, 2) << ' ' << sp.query(1, 4) << ' ' << sp.query(4, 5) << '\n'; } ``
  2. 2. Predict the output of this program. It prints one line with two numbers. ``cpp #include <iostream> #include <numeric> #include <vector> int main() { const std::vector<int> a = {12, 18, 8}; int with_zero = 0; // identity for gcd int with_one = 1; // wrong start value for (int x : a) { with_zero = std::gcd(with_zero, x); with_one = std::gcd(with_one, x); } std::cout << with_zero << ' ' << with_one << '\n'; } ``
  3. 3. Why can a sparse table answer a range-minimum query with two overlapping blocks, while the same trick gives a wrong answer for a range sum?
  4. 4. A sparse table is built for an array of 8 values. Then one value in the array is changed, and no rebuild happens. What do later queries do?
  5. 5. How many levels does a sparse table have for an array of length 16?

    Type a number.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.