Embercode
✕
Segment TreesQuick quiz

Node merging

6 questions. Covers Maximum subarray with node merging.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``cpp #include <algorithm> #include <iostream> struct Info { long long total, prefix, suffix, best; }; Info leaf(long long v) { return {v, v, v, v}; } Info merge(const Info& a, const Info& b) { return {a.total + b.total, std::max(a.prefix, a.total + b.prefix), std::max(b.suffix, b.total + a.suffix), std::max({a.best, b.best, a.suffix + b.prefix})}; } int main() { Info x = merge(merge(leaf(-4), leaf(3)), leaf(-1)); std::cout << x.best << ' ' << x.prefix << ' ' << x.suffix << ' ' << x.total << '\n'; } ``
  2. 2. What does this program print? ``cpp #include <algorithm> #include <iostream> struct Info { long long total, prefix, suffix, best; }; Info leaf(long long v) { return {v, v, v, v}; } Info merge(const Info& a, const Info& b) { return {a.total + b.total, std::max(a.prefix, a.total + b.prefix), std::max(b.suffix, b.total + a.suffix), std::max({a.best, b.best, a.suffix + b.prefix})}; } int main() { Info y = merge(leaf(-3), leaf(-1)); std::cout << y.total << ' ' << y.best << '\n'; } ``
  3. 3. In the merge rule for best, why is a.suffix + b.prefix included?
  4. 4. Which formula is the merge rule for prefix in lesson 14?
  5. 5. True or false: a zero-filled Info can serve as the empty answer for a query that covers no positions, because its best value of 0 never beats a real answer.

5 question(s) left on this page.