Problem solution · C++

Min Plus Convolution Convex Convex

Min Plus Convolution Convex Convex: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to NyaanNyaan Competitive Programming Library.

Technique
Segment tree or range structure
Source
NyaanNyaan Competitive Programming Library
Length
40 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For Min Plus Convolution Convex Convex, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 40 lines of C++ from the credited upstream file yosupo-concave-min-plus-convolution-4.test.cpp.
  • The implementation keeps its working state in language-native values and containers.
  • 1 loop block detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from NyaanNyaan Competitive Programming Library by NyaanNyaan and is used under the CC0-1.0 licence.

Full codeMin Plus Convolution Convex Convex · C++C++
Use this to learn the idea, then write your own version.
#define PROBLEM "https://judge.yosupo.jp/problem/min_plus_convolution_convex_convex"//#include "../../template/template.hpp"//#include "../../segment-tree/li-chao-tree-abstruct.hpp"using namespace Nyaan; vl c, d;struct D {  ll i;  ll operator()(ll x) {    if (i == -1) return infLL;    return d[i] + c[x - i];  };}; vl conv(vl a, vl b) {  if (a.empty() or b.empty()) return {};  c = a, d = b;  int s = sz(a) + sz(b) - 1;  LiChaoTree<D, true, false> lct(s, D{-1});  rep(j, sz(b)) lct.add_segment(j, j + sz(a), D{j});  vl res(s);  rep(i, s) res[i] = lct.get_val(i).fi;  return res;} void q() {  ini(N, M);  vl a(N), b(M);  in(a, b);  out(conv(a, b));} void Nyaan::solve() {  int t = 1;  // in(t);  while (t--) q();} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗