library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub maspypy/library

:heavy_check_mark: test/1_mytest/slope_super.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "my_template.hpp"

#include "random/shuffle.hpp"
#include "convex/slope_trick/slope_trick_super.hpp"

void test(int N, bool from_zero, int add_prob) {
  Slope_Trick_Super<ll> ST;
  using F = typename decltype(ST)::FUNC;

  vc<int> L(2 * N - 1), R(2 * N - 1), A(N), B(N);
  vc<int> I(N);
  FOR(i, N) I[i] = i;

  FOR(i, N) {
    L[i] = RNG(0, 5);
    if (from_zero) L[i] = 0;
    R[i] = RNG(0, 5);
    if (L[i] > R[i]) swap(L[i], R[i]);
    A[i] = RNG(-3, 4);
    B[i] = RNG(-3, 4);
  }
  vvc<ll> dp(2 * N - 1);
  vc<F> func(2 * N - 1);
  FOR(i, N) {
    dp[i].resize(R[i] + 1, infty<ll>);
    FOR(x, L[i], R[i] + 1) dp[i][x] = A[i] * x + B[i];
    func[i] = ST.segment_func(L[i], R[i], A[i], B[i]);
    FOR(x, L[i], R[i] + 1) {
      ll me = ST.eval(func[i], x);
      ll god = dp[i][x];
      assert(me == god);
    }
  }

  FOR(i, N, 2 * N - 1) {
    shuffle(I);
    int t = (RNG(0, 100) < add_prob ? 0 : 1);
    int a = POP(I), b = POP(I);
    I.eb(i);
    if (dp[a].empty() || dp[b].empty()) continue;
    if (t == 0) {
      // ADD
      L[i] = max(L[a], L[b]);
      R[i] = min(R[a], R[b]);
      if (L[i] > R[i]) continue;
      dp[i].assign(R[i] + 1, infty<ll>);
      FOR(x, L[i], R[i] + 1) dp[i][x] = dp[a][x] + dp[b][x];
      F f = func[a], g = func[b];
      ST.restrict_domain(f, L[i], R[i]);
      ST.restrict_domain(g, L[i], R[i]);
      func[i] = ST.add(f, g);
    }
    if (t == 1) {
      // conv
      L[i] = L[a] + L[b];
      R[i] = R[a] + R[b];
      dp[i].assign(R[i] + 1, infty<ll>);
      FOR(x1, L[a], R[a] + 1) {
        FOR(x2, L[b], R[b] + 1) {
          chmin(dp[i][x1 + x2], dp[a][x1] + dp[b][x2]);
        }
      }
      func[i] = ST.convolve(func[a], func[b]);
    }
    vi X(R[i] + 1, infty<ll>);
    FOR(x, L[i], R[i] + 1) X[x] = ST.eval(func[i], x);
    assert(func[i].x0 == L[i]);
    assert(func[i].x1 == R[i]);
    assert(X == dp[i]);
    auto [fx, x] = ST.get_min(func[i]);
    assert(L[i] <= x && x <= R[i]);
    assert(MIN(X) == fx && X[x] == fx);
  }
  int i = 2 * N - 2;
  if (dp[i].empty()) return;
  int t = RNG(0, 2);
  if (t == 0) {
    // clear right
    FOR(x, L[i] + 1, R[i] + 1) chmin(dp[i][x], dp[i][x - 1]);
    ST.clear_right(func[i]);
  }
  if (t == 2) {
    // clear left
    FOR_R(x, L[i] + 1, R[i] + 1) chmin(dp[i][x - 1], dp[i][x]);
    ST.clear_left(func[i]);
  }
  vi X(R[i] + 1, infty<ll>);
  FOR(x, L[i], R[i] + 1) X[x] = ST.eval(func[i], x);
  assert(func[i].x0 == L[i]);
  assert(func[i].x1 == R[i]);
  assert(X == dp[i]);
}

void solve() {
  int x, y;
  cin >> x >> y;
  cout << x + y << "\n";
}

void test_repeated_middle_insert() {
  // (x-0)+, (x-2)+ の間に (x-1)+ を繰り返し加える。
  // DFS で直接差し込む実装ではここで木が片寄る。
  constexpr int N = 20000;
  Slope_Trick_Super<ll> ST;
  auto f = ST.segment_func(-1LL, 4LL, 0LL, 0LL);
  ST.add_x_minus_a(f, 0);
  ST.add_x_minus_a(f, 2);
  FOR(N) ST.add_x_minus_a(f, 1);
  assert(f.size() == N + 2);
  assert(ST.eval(f, -1) == 0);
  assert(ST.eval(f, 0) == 0);
  assert(ST.eval(f, 1) == 1);
  assert(ST.eval(f, 2) == N + 2);
  assert(ST.eval(f, 4) == 3LL * N + 6);
}

void test_sum_all_duplicate_breakpoints() {
  constexpr int N = 1000;
  Slope_Trick_Super<ll> ST;
  using F = typename decltype(ST)::FUNC;
  vc<F> funcs;
  FOR(N) {
    auto f = ST.segment_func(-1LL, 4LL, 0LL, 0LL);
    ST.add_x_minus_a(f, 1);
    funcs.eb(f);
  }
  auto f = ST.sum_all(funcs);
  assert(f.size() == N);
  assert(ST.eval(f, -1) == 0);
  assert(ST.eval(f, 1) == 0);
  assert(ST.eval(f, 4) == 3LL * N);
}

void test_unite_cancellation_and_lazy_shift() {
  Slope_Trick_Super<ll> ST;
  vc<pair<ll, ll>> p = {{-1, 0}, {1, 0}, {4, 3}};
  vc<pair<ll, ll>> q = {{-1, 0}, {1, 0}, {4, -3}};
  auto f = ST.from_points(p);
  auto g = ST.from_points(q);
  f = ST.add(f, g);
  assert(f.size() == 2);
  FOR(x, -1, 5) assert(ST.eval(f, x) == 0);

  f = ST.segment_func(-1LL, 4LL, 0LL, 0LL);
  ST.add_x_minus_a(f, 0);
  ST.add_x_minus_a(f, 2);
  ST.shift(f, 5, 7);
  g = ST.segment_func(4LL, 9LL, 0LL, 0LL);
  ST.add_x_minus_a(g, 6);
  f = ST.add(f, g);
  assert(ST.eval(f, 4) == 7);
  assert(ST.eval(f, 5) == 7);
  assert(ST.eval(f, 6) == 8);
  assert(ST.eval(f, 7) == 10);
  assert(ST.eval(f, 9) == 16);
}

void test_sum_all_random() {
  Slope_Trick_Super<ll> ST;
  using F = typename decltype(ST)::FUNC;
  FOR(100) {
    constexpr int L = -3, R = 4;
    vc<F> funcs;
    vc<ll> want(R - L + 1);
    int N = RNG(1, 30);
    FOR(N) {
      ll a = RNG(-3, 4), b = RNG(-3, 4);
      auto f = ST.segment_func(L, R, a, b);
      FOR(x, L, R + 1) want[x - L] += a * x + b;
      int p = RNG(L + 1, R);
      if (RNG(0, 2)) {
        ST.add_x_minus_a(f, p);
        FOR(x, L, R + 1) want[x - L] += max(0LL, x - p);
      } else {
        ST.add_a_minus_x(f, p);
        FOR(x, L, R + 1) want[x - L] += max(0LL, p - x);
      }
      funcs.eb(f);
    }
    auto f = ST.sum_all(funcs);
    FOR(x, L, R + 1) assert(ST.eval(f, x) == want[x - L]);
    auto dat = ST.ST.get_all(f.root);
    FOR(i, len(dat) - 1) assert(dat[i].fi <= dat[i + 1].fi);
  }
}

signed main() {
  vc<int> ns = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1000};
  vc<int> ps = {50, 100, 0, 95, 5};
  for (auto& n : ns) {
    for (auto& p : ps) {
      FOR(20) {
        test(n, false, p);
        test(n, true, p);
      }
    }
  }
  test_repeated_middle_insert();
  test_sum_all_duplicate_breakpoints();
  test_unite_cancellation_and_lazy_shift();
  test_sum_all_random();

  solve();
  return 0;
}
#line 1 "test/1_mytest/slope_super.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "my_template.hpp"
// Source: https://github.com/maspypy/library/blob/main/my_template.hpp
#if defined(USE_PCH)
#include <my_template_compiled.hpp>

#if defined(__GNUC__)
#pragma GCC optimize("Ofast,unroll-loops")
#if !defined(MASPY_NON_AVX2)
#pragma GCC target("avx2,popcnt")
#endif
#endif

#else  // MASPY_TEMPLATE_BODY
#include <bits/stdc++.h>
#include <cassert>

#if defined(__GNUC__)
#pragma GCC optimize("Ofast,unroll-loops")
#endif

using namespace std;

using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class>
constexpr bool dependent_false = false;

template <class T>
constexpr T infty = [] {
  static_assert(dependent_false<T>, "infty<T> is not defined");
  return T{};
}();
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<i128>;
template <>
constexpr long double infty<long double> = infty<i128>;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using pq_max = priority_queue<T>;
template <class T>
using pq_min = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR1_R(a) for (ll i = ll(a) - 1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = ll(a) - 1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i)
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define all(x) (x).begin(), (x).end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

// require y > 0
template <typename T>
T floor(T x, T y) {
  return x / y - (x % y < 0);
}

// require y > 0
template <typename T>
T ceil(T x, T y) {
  return (x / y) + (x % y > 0);
}

// require y > 0
template <typename T>
T bmod(T x, T y) {
  T r = x % y;
  return (r < 0 ? r + y : r);
}

// require y > 0
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = x / y, r = x % y;
  if (r < 0) --q, r += y;
  return {q, r};
}

constexpr auto TEN = [] {
  array<u64, 20> A{};
  A[0] = 1;
  for (int i = 1; i < 20; ++i) A[i] = 10 * A[i - 1];
  return A;
}();

template <typename T, typename U>
T SUM(const U &A) {
  return std::accumulate(A.begin(), A.end(), T{});
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
template <class C, class T>
inline long long LB(const C &c, const T &x) {
  return lower_bound(c.begin(), c.end(), x) - c.begin();
}
template <class C, class T>
inline long long UB(const C &c, const T &x) {
  return upper_bound(c.begin(), c.end(), x) - c.begin();
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <class T, class Container, class Compare>
T POP(priority_queue<T, Container, Compare> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
i128 binary_search(F check, i128 ok, i128 ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (1) {
    i128 x = (ok + ng) / 2;
    if (x == ok || x == ng) break;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}

template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  T c = max<T>(a, b);
  bool changed = (c != a);
  a = c;
  return changed;
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  T c = min<T>(a, b);
  bool changed = (c != a);
  a = c;
  return changed;
}

// bit operations
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T kth_bit(int k) {
  assert(0 <= k && k < int(8 * sizeof(T)));
  return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
  assert(0 <= k && k < int(8 * sizeof(T)));
  return x >> k & 1;
}

template <typename T>
struct all_bit {
  static_assert(is_integral<T>::value);
  T s;
  all_bit(T s) : s(s) { assert(s >= 0); }
  struct iter {
    T s;
    int operator*() const { return lowbit(s); }
    void operator++() { s &= s - 1; }
    bool operator!=(nullptr_t) const { return s; }
  };
  iter begin() const { return {s}; }
  nullptr_t end() const { return nullptr; }
};

template <typename T>
struct all_subset {
  static_assert(is_integral<T>::value);
  T s;
  all_subset(T s) : s(s) { assert(s >= 0); }
  struct iter {
    T s, t;
    bool done = false;
    T operator*() const { return t; }
    void operator++() {
      done = (t == 0);
      t = (t - 1) & s;
    }
    bool operator!=(nullptr_t) const { return !done; }
  };
  iter begin() const { return {s, s}; }
  nullptr_t end() const { return nullptr; }
};

constexpr u64 full_mask(int n) {
  assert(0 <= n && n <= 64);
  return n == 64 ? -1ULL : (1ULL << n) - 1;
}

u64 bit_reverse(u64 x) {
  x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
  x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
  x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
  x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
  x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
  x = (x << 32) | (x >> 32);
  return x;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(const vc<U> &A, int off = 1) {
  int N = A.size();
  vc<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vc<int> argsort(const vc<T> &A) {
  vc<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
      [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}

template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &...others) {
  first.reserve(first.size() + (others.size() + ... + 0));
  (first.insert(first.end(), others.begin(), others.end()), ...);
}

// i128
template <class T, enable_if_t<is_same_v<T, i128>, int> = 0>
constexpr i128 abs(T x) {
  return x < 0 ? -x : x;
}

constexpr i128 gcd(i128 a, i128 b) {
  while (b != 0) {
    i128 c = a % b;
    a = b, b = c;
  }
  return abs(a);
}
#endif  // MASPY_TEMPLATE_BODY
#line 3 "test/1_mytest/slope_super.test.cpp"

#line 1 "random/shuffle.hpp"
// Source: https://github.com/maspypy/library/blob/main/random/shuffle.hpp
#line 1 "random/base.hpp"
// Source: https://github.com/maspypy/library/blob/main/random/base.hpp

u64 RNG_64() {
  static u64 x_ = u64(chrono::duration_cast<chrono::nanoseconds>(
                      chrono::high_resolution_clock::now().time_since_epoch())
                          .count()) *
                  10150724397891781847ULL;
  x_ ^= x_ << 7;
  return x_ ^= x_ >> 9;
}

u64 RNG(u64 lim) {
  assert(lim > 0);
  return RNG_64() % lim;
}

ll RNG(ll l, ll r) {
  assert(l < r);
  return l + RNG_64() % (r - l);
}
#line 3 "random/shuffle.hpp"

template <typename T>
void shuffle(vc<T>& A) {
  FOR(i, len(A)) {
    int j = RNG(0, i + 1);
    if (i != j) swap(A[i], A[j]);
  }
}
#line 1 "convex/slope_trick/slope_trick_super.hpp"
// Source: https://github.com/maspypy/library/blob/main/convex/slope_trick/slope_trick_super.hpp
#line 1 "ds/splaytree/splaytree.hpp"
// Source: https://github.com/maspypy/library/blob/main/ds/splaytree/splaytree.hpp
#line 1 "ds/node_pool.hpp"
// Source: https://github.com/maspypy/library/blob/main/ds/node_pool.hpp
// マルチテストケースでも確保済み chunk を再利用する
template <class Node>
struct Node_Pool {
  union Slot {
    Node node;
    Slot* next;

    Slot() {}
    ~Slot() {}
  };
  using np = Node*;

  static constexpr int CHUNK_SIZE = 1 << 12;

  vc<unique_ptr<Slot[]>> chunks;
  int chunk_id = 0;
  int pos = 0;
  Slot* free_head = nullptr;

  ~Node_Pool() {
    auto& cache = chunk_cache();
    for (auto& p : chunks) cache.eb(std::move(p));
  }

  template <class... Args>
  np create(Args&&... args) {
    Slot* s = new_slot();
    return ::new (&s->node) Node(forward<Args>(args)...);
  }

  np clone(const np x) {
    assert(x);
    Slot* s = new_slot();
    return ::new (&s->node) Node(*x);
  }

  void destroy(np x) {
    if (!x) return;
    x->~Node();
    Slot* s = reinterpret_cast<Slot*>(x);
    s->next = free_head;
    free_head = s;
  }

  // 全 node を無効化する。
  // 確保済み chunk は解放せず、次回以降に再利用する。
  void reset() {
    free_head = nullptr;
    chunk_id = 0;
    pos = 0;
  }

  int used() const { return chunk_id * CHUNK_SIZE + pos; }

 private:
  static vc<unique_ptr<Slot[]>>& chunk_cache() {
    // static Node_Pool の destructor より先に破棄されないようにする。
    static auto* cache = new vc<unique_ptr<Slot[]>>();
    return *cache;
  }

  void alloc_chunk() {
    auto& cache = chunk_cache();
    if (cache.empty()) {
      chunks.eb(make_unique<Slot[]>(CHUNK_SIZE));
    } else {
      chunks.eb(std::move(cache.back()));
      cache.pop_back();
    }
  }

  Slot* new_slot() {
    if (free_head) {
      Slot* s = free_head;
      free_head = free_head->next;
      return s;
    }

    if (chunk_id == len(chunks)) alloc_chunk();

    Slot* s = &chunks[chunk_id][pos++];
    if (pos == CHUNK_SIZE) {
      ++chunk_id;
      pos = 0;
    }
    return s;
  }
};
#line 3 "ds/splaytree/splaytree.hpp"

// Node 型を別に定義して使う
template <typename Node>
struct SplayTree {
  Node_Pool<Node> pool;
  using np = Node *;
  using X = typename Node::value_type;
  using A = typename Node::operator_type;

  void free_subtree(np c) {
    if (!c) return;
    auto dfs = [&](auto &dfs, np c) -> void {
      if (c->l) dfs(dfs, c->l);
      if (c->r) dfs(dfs, c->r);
      c->p = c->l = c->r = nullptr;
      pool.destroy(c);
    };
    dfs(dfs, c);
  }

  void reset() { pool.reset(); }

  np new_root() { return nullptr; }

  np new_node(const X &x) {
    np n = pool.create();
    Node::new_node(n, x);
    return n;
  }

  np new_node(const vc<X> &dat) {
    auto dfs = [&](auto &dfs, int l, int r) -> np {
      if (l == r) return nullptr;
      if (r == l + 1) return new_node(dat[l]);
      int m = (l + r) / 2;
      np l_root = dfs(dfs, l, m);
      np r_root = dfs(dfs, m + 1, r);
      np root = new_node(dat[m]);
      root->l = l_root, root->r = r_root;
      if (l_root) l_root->p = root;
      if (r_root) r_root->p = root;
      root->update();
      return root;
    };
    return dfs(dfs, 0, len(dat));
  }

  u32 get_size(np root) { return (root ? root->size : 0); }

  np merge(np l_root, np r_root) {
    if (!l_root) return r_root;
    if (!r_root) return l_root;
    assert((!l_root->p) && (!r_root->p));
    splay_kth(r_root, 0);  // splay したので push 済
    r_root->l = l_root;
    l_root->p = r_root;
    r_root->update();
    return r_root;
  }
  np merge3(np a, np b, np c) { return merge(merge(a, b), c); }
  np merge4(np a, np b, np c, np d) { return merge(merge(merge(a, b), c), d); }

  pair<np, np> split(np root, u32 k) {
    assert(!root || !root->p);
    if (k == 0) return {nullptr, root};
    if (k == (root->size)) return {root, nullptr};
    splay_kth(root, k - 1);
    np right = root->r;
    root->r = nullptr, right->p = nullptr;
    root->update();
    return {root, right};
  }
  tuple<np, np, np> split3(np root, u32 l, u32 r) {
    np nm, nr;
    tie(root, nr) = split(root, r);
    tie(root, nm) = split(root, l);
    return {root, nm, nr};
  }
  tuple<np, np, np, np> split4(np root, u32 i, u32 j, u32 k) {
    np d;
    tie(root, d) = split(root, k);
    auto [a, b, c] = split3(root, i, j);
    return {a, b, c, d};
  }

  tuple<np, np, np> split_L_root_R(np root) {
    u32 s = (root->l ? root->l->size : 0);
    return split3(root, s, s + 1);
  }

  // 部分木が区間 [l,r) に対応するようなノードを作って返す
  // そのノードが root になるわけではないので、
  // このノードを参照した後にすぐに splay して根に持ち上げること
  void goto_between(np &root, u32 l, u32 r) {
    if (l == 0 && r == root->size) return;
    if (l == 0) {
      splay_kth(root, r);
      root = root->l;
      return;
    }
    if (r == root->size) {
      splay_kth(root, l - 1);
      root = root->r;
      return;
    }
    splay_kth(root, r);
    np rp = root;
    root = rp->l;
    root->p = nullptr;
    splay_kth(root, l - 1);
    root->p = rp;
    rp->l = root;
    rp->update();
    root = root->r;
  }

  vc<X> get_all(const np &root) {
    vc<X> res;
    auto dfs = [&](auto &dfs, np root) -> void {
      if (!root) return;
      root->push();
      dfs(dfs, root->l);
      res.eb(root->get());
      dfs(dfs, root->r);
    };
    dfs(dfs, root);
    return res;
  }

  X get(np &root, u32 k) {
    assert(root == nullptr || !root->p);
    splay_kth(root, k);
    return root->get();
  }

  void set(np &root, u32 k, const X &x) {
    assert(root != nullptr && !root->p);
    splay_kth(root, k);
    root->set(x);
  }

  void multiply(np &root, u32 k, const X &x) {
    assert(root != nullptr && !root->p);
    splay_kth(root, k);
    root->multiply(x);
  }

  X prod(np &root, u32 l, u32 r) {
    assert(root == nullptr || !root->p);
    using Mono = typename Node::Monoid_X;
    if (l == r) return Mono::id();
    assert(0 <= l && l < r && r <= root->size);
    goto_between(root, l, r);
    X res = root->prod;
    splay(root, true);
    return res;
  }

  X prod(np &root) {
    assert(root == nullptr || !root->p);
    using Mono = typename Node::Monoid_X;
    return (root ? root->prod : Mono::id());
  }

  void apply(np &root, u32 l, u32 r, const A &a) {
    if (l == r) return;
    assert(0 <= l && l < r && r <= root->size);
    goto_between(root, l, r);
    root->apply(a);
    splay(root, true);
  }
  void apply(np &root, const A &a) {
    if (!root) return;
    root->apply(a);
  }

  void reverse(np &root, u32 l, u32 r) {
    assert(root == nullptr || !root->p);
    if (l == r) return;
    assert(0 <= l && l < r && r <= root->size);
    goto_between(root, l, r);
    root->reverse();
    splay(root, true);
  }
  void reverse(np root) {
    if (!root) return;
    root->reverse();
  }

  void rotate(Node *n) {
    // n を根に近づける。push, update は rotate の外で行う。
    Node *pp, *p, *c;
    p = n->p;
    pp = p->p;
    if (p->l == n) {
      c = n->r;
      n->r = p;
      p->l = c;
    } else {
      c = n->l;
      n->l = p;
      p->r = c;
    }
    if (pp && pp->l == p) pp->l = n;
    if (pp && pp->r == p) pp->r = n;
    n->p = pp;
    p->p = n;
    if (c) c->p = p;
  }

  void push_from_root(np c) {
    if (!c->p) {
      c->push();
      return;
    }
    push_from_root(c->p);
    c->push();
  }

  void splay(Node *me, bool push_from_root_done) {
    // これを呼ぶ時点で、me の祖先(me を除く)は既に push 済であることを仮定
    // 特に、splay 終了時点で me は upd / push 済である
    if (!push_from_root_done) push_from_root(me);
    me->push();
    while (me->p) {
      np p = me->p;
      np pp = p->p;
      if (!pp) {
        rotate(me);
        p->update();
        break;
      }
      bool same = (p->l == me && pp->l == p) || (p->r == me && pp->r == p);
      if (same) rotate(p), rotate(me);
      if (!same) rotate(me), rotate(me);
      pp->update(), p->update();
    }
    // me の update は最後だけでよい
    me->update();
  }

  void splay_kth(np &root, u32 k) {
    assert(0 <= k && k < (root->size));
    while (1) {
      root->push();
      u32 s1 = (root->l ? root->l->size : 0);
      u32 s2 = (root->size) - (root->r ? root->r->size : 0);
      if (k < s1) root = root->l;
      elif (k < s2) { break; }
      else {
        k -= s2;
        root = root->r;
      }
    }
    splay(root, true);
  }

  // check(x), 左側のノード全体が check を満たすように切る
  template <typename F>
  pair<np, np> split_max_right(np root, F check) {
    if (!root) return {nullptr, nullptr};
    assert(!root->p);
    np c = find_max_right(root, check);
    if (!c) {
      splay(root, true);
      return {nullptr, root};
    }
    splay(c, true);
    np right = c->r;
    if (!right) return {c, nullptr};
    right->p = nullptr;
    c->r = nullptr;
    c->update();
    return {c, right};
  }

  // check(x, cnt), 左側のノード全体が check を満たすように切る
  template <typename F>
  pair<np, np> split_max_right_cnt(np root, F check) {
    if (!root) return {nullptr, nullptr};
    assert(!root->p);
    np c = find_max_right_cnt(root, check);
    if (!c) {
      splay(root, true);
      return {nullptr, root};
    }
    splay(c, true);
    np right = c->r;
    if (!right) return {c, nullptr};
    right->p = nullptr;
    c->r = nullptr;
    c->update();
    return {c, right};
  }

  // 左側のノード全体の prod が check を満たすように切る
  template <typename F>
  pair<np, np> split_max_right_prod(np root, F check) {
    if (!root) return {nullptr, nullptr};
    assert(!root->p);
    np c = find_max_right_prod(root, check);
    if (!c) {
      splay(root, true);
      return {nullptr, root};
    }
    splay(c, true);
    np right = c->r;
    if (!right) return {c, nullptr};
    right->p = nullptr;
    c->r = nullptr;
    c->update();
    return {c, right};
  }

  template <typename F>
  np find_max_right(np root, const F &check) {
    // 最後に見つけた ok の点、最後に探索した点
    np last_ok = nullptr, last = nullptr;
    while (root) {
      last = root;
      root->push();
      if (check(root->x)) {
        last_ok = root;
        root = root->r;
      } else {
        root = root->l;
      }
    }
    splay(last, true);
    return last_ok;
  }

  template <typename F>
  np find_max_right_cnt(np root, const F &check) {
    // 最後に見つけた ok の点、最後に探索した点
    np last_ok = nullptr, last = nullptr;
    ll n = 0;
    while (root) {
      last = root;
      root->push();
      ll k = (root->size) - (root->r ? root->r->size : 0);
      if (check(root->x, n + k)) {
        last_ok = root;
        n += k;
        root = root->r;
      } else {
        root = root->l;
      }
    }
    splay(last, true);
    return last_ok;
  }

  template <typename F>
  np find_max_right_prod(np root, const F &check) {
    using Mono = typename Node::Monoid_X;
    X prod = Mono::id();
    // 最後に見つけた ok の点、最後に探索した点
    np last_ok = nullptr, last = nullptr;
    while (root) {
      last = root;
      root->push();
      np tmp = root->r;
      root->r = nullptr;
      root->update();
      X lprod = Mono::op(prod, root->prod);
      root->r = tmp;
      root->update();
      if (check(lprod)) {
        prod = lprod;
        last_ok = root;
        root = root->r;
      } else {
        root = root->l;
      }
    }
    splay(last, true);
    return last_ok;
  }
};
#line 1 "alg/monoid/add_pair.hpp"
// Source: https://github.com/maspypy/library/blob/main/alg/monoid/add_pair.hpp

template <typename E>
struct Monoid_Add_Pair {
  using value_type = pair<E, E>;
  using X = value_type;
  static constexpr X op(const X &x, const X &y) {
    return {x.fi + y.fi, x.se + y.se};
  }
  static constexpr X inverse(const X &x) { return {-x.fi, -x.se}; }
  static constexpr X id() { return {0, 0}; }
  static constexpr bool commute = true;
};
#line 4 "convex/slope_trick/slope_trick_super.hpp"

namespace SLOPE_TRICK_SUPER {
/*
傾きと座標が全部 T. domain は closed.
(x0,y0,a0) / 傾き変化を splay tree で持つ.
末尾には必ず infty が入っているようにする.
(0,10),(1,6),(3,4),(6,7)
->
(x0,y0,a0)=(0,10,-4)
dat = ([1,3],[3,2])

f(x) の計算, (min,argmin) の計算
加法, 畳み込み

加法: easy
f(x) の計算: sum(a), sum(xa) が要る
畳み込み: x->x+c が要る
*/

template <typename T>
struct Node {
  using value_type = pair<T, T>;
  using operator_type = T;
  using np = Node *;
  using Monoid_X = Monoid_Add_Pair<T>;

  np p, l, r;
  bool rev;
  u32 size;
  pair<T, T> x;     // (x,a)
  pair<T, T> prod;  // (a sum, xa sum)
  T add_x;

  static void new_node(np n, const pair<T, T> &x) {
    n->p = n->l = n->r = nullptr, n->rev = 0, n->size = 1;
    n->x = x, n->prod = {x.se, x.fi * x.se}, n->add_x = 0;
  }

  void update() {
    size = 1;
    if (l) {
      size += l->size;
    }
    if (r) {
      size += r->size;
    }
    prod = {x.se, x.fi * x.se};
    if (l) prod = Monoid_X::op(prod, l->prod);
    if (r) prod = Monoid_X::op(prod, r->prod);
  }

  void push() {
    assert(!rev);
    if (add_x == 0) return;
    if (l)
      l->x.fi += add_x, l->prod.se += l->prod.fi * add_x, l->add_x += add_x;
    if (r)
      r->x.fi += add_x, r->prod.se += r->prod.fi * add_x, r->add_x += add_x;
    add_x = 0;
  }

  void apply(T a) { x.fi += a, prod.se += a * prod.fi, add_x += a; }

  // update, push 以外で呼ばれるものは、splay 後であることが想定されている。
  // したがってその時点で update, push 済であることを仮定してよい。
  pair<T, T> get() { return x; }
  void set(const pair<T, T> &xx) {
    x = xx;
    update();
  }
};

// 関数は破壊的な変更にされる
template <typename T>
struct Slope_Trick_Super {
  SplayTree<Node<T>> ST;
  using np = Node<T> *;

  struct FUNC {
    np root;  // 定義域がこわれていたら root == empty
    T x0, x1, a0, y0;
    int size() { return (root ? root->size : 0); }
  };

  // (L,R,a,b) : [L,R] で y=ax+b
  FUNC segment_func(T L, T R, T a, T b) {
    assert(L <= R);
    return {nullptr, L, R, a, a * L + b};
  }
  FUNC from_points(vc<pair<T, T>> &point) {
    return from_points(
        len(point), [&](int i) -> pair<T, T> { return point[i]; });
  }
  template <typename F>
  FUNC from_points(int N, F f) {
    vc<T> X(N), Y(N);
    FOR(i, N) tie(X[i], Y[i]) = f(i);
    FOR(i, N - 1) assert(X[i] < X[i + 1]);
    if (N == 1) return segment_func(X[0], X[0], 0, Y[0]);
    T a0 = (Y[1] - Y[0]) / (X[1] - X[0]);
    T x0 = X[0], x1 = X.back();
    vc<pair<T, T>> dat;
    T a = a0;
    FOR(i, 1, N - 1) {
      T a1 = (Y[i + 1] - Y[i]) / (X[i + 1] - X[i]);
      dat.eb(X[i], a1 - a), a = a1;
    }
    return FUNC{ST.new_node(dat), x0, x1, a0, Y[0]};
  }

  pair<T, T> domain(FUNC &f) { return {f.x0, f.x1}; }
  T eval(FUNC &f, T x) {
    auto [x0, x1] = domain(f);
    assert(x0 <= x && x <= x1);
    auto [l, r] = ST.split_max_right(
        f.root, [&](auto dat) -> bool { return dat.fi <= x; });
    auto [a_sum, xa_sum] = ST.prod(l);
    f.root = ST.merge(l, r);
    return f.y0 + f.a0 * (x - x0) + a_sum * x - xa_sum;
  }
  FUNC restrict_domain(FUNC &f, T L, T R) {
    auto [x0, x1] = domain(f);
    assert(x0 <= L && L <= R && R <= x1);
    // まずは右側をちぢめる. R 以上の傾き変化を消してしまえばよい
    auto [l, r] = ST.split_max_right(
        f.root, [&](auto dat) -> bool { return dat.fi < R; });
    ST.free_subtree(r);
    // 左側をちぢめる.
    tie(l, r) =
        ST.split_max_right(l, [&](auto dat) -> bool { return dat.fi <= L; });
    auto [a_sum, xa_sum] = ST.prod(l);
    T new_a0 = f.a0 + a_sum;
    T new_y0 = f.y0 + f.a0 * (L - x0) + a_sum * L - xa_sum;
    ST.free_subtree(l);
    f.root = r, f.x0 = L, f.x1 = R, f.a0 = new_a0, f.y0 = new_y0;
    return f;
  }
  FUNC add(FUNC &f, FUNC &g) {
    assert(f.x0 == g.x0 && f.x1 == g.x1);
    T x0 = f.x0, x1 = f.x1;
    T y0 = f.y0 + g.y0, a0 = f.a0 + g.a0;

    if (len(f) < len(g)) swap(f, g);
    auto tmp = ST.get_all(g.root);
    ST.free_subtree(g.root);

    f.x0 = x0, f.x1 = x1, f.a0 = a0, f.y0 = y0;
    for (auto &x : tmp) {
      auto [l, r] = ST.split_max_right(
          f.root, [&](auto v) -> bool { return v.fi <= x.fi; });
      f.root = ST.merge3(l, ST.new_node(x), r);
    }
    return f;
  }
  FUNC sum_all(vc<FUNC> &funcs) {
    assert(len(funcs) >= 1);
    T x0 = funcs[0].x0, x1 = funcs[0].x1;
    for (auto &g : funcs) assert(x0 == g.x0 && x1 == g.x1);
    int idx = 0;
    FOR(i, 1, len(funcs)) if (len(funcs[idx]) < len(funcs[i])) idx = i;
    swap(funcs[idx], funcs.back());
    FUNC f = POP(funcs);
    vc<pair<T, T>> dat;
    for (auto &g : funcs) {
      f.y0 += g.y0, f.a0 += g.a0;
      auto tmp = ST.get_all(g.root);
      concat(dat, tmp);
      ST.free_subtree(g.root);
    }
    sort(all(dat));
    for (auto &x : dat) {
      auto [l, r] = ST.split_max_right(
          f.root, [&](auto v) -> bool { return v.fi <= x.fi; });
      f.root = ST.merge3(l, ST.new_node(x), r);
    }
    return f;
  }

  // 各挿入で split が境界を splay するので、木を直接たどって接続しない。
  np insert_all(np root, vc<pair<T, T>> &dat) {
    if (!root) return ST.new_node(dat);
    for (auto &x : dat) {
      auto [l, r] = ST.split_max_right(
          root, [&](auto v) -> bool { return v.fi <= x.fi; });
      root = ST.merge3(l, ST.new_node(x), r);
    }
    return root;
  }

  FUNC shift(FUNC &f, T add_x, T add_y) {
    ST.apply(f.root, add_x);
    f.x0 += add_x, f.x1 += add_x, f.y0 += add_y;
    return f;
  }

  // h[z]=min(x+y==z)f(x)+g(y)
  FUNC convolve(FUNC &f, FUNC &g) {
    if (f.x0 > f.x1 || g.x0 > g.x1) {
      return {nullptr, infty<T>, -infty<T>, 0, 0};
    }
    if (len(f) < len(g)) swap(f, g);
    shift(f, g.x0, g.y0), shift(g, -g.x0, -g.y0);
    if (len(g) == 0) {
      return convolve_segment(f, 0, g.x1, g.a0, 0);
    }
    auto tmp = ST.get_all(g.root);
    ST.free_subtree(g.root);
    f = convolve_segment(f, 0, tmp[0].fi, g.a0, 0);
    T a = g.a0;
    FOR(i, len(tmp)) {
      T nx = (i + 1 < len(tmp) ? tmp[i + 1].fi : g.x1);
      a += tmp[i].se;
      f = convolve_segment(f, 0, nx - tmp[i].fi, a, 0);
      // for (auto &[x, a] : ST.get_all(f.root)) {
      //   assert(f.x0 <= x && x <= f.x1);
      //   if (f.root) assert(!f.root->p);
      // }
    }
    return f;
  }

  // [x0,x1], y=ax+b
  FUNC convolve_segment(FUNC &f, T x0, T x1, T a, T b) {
    assert(x0 <= x1);
    if (f.x0 > f.x1) {
      return {nullptr, infty<T>, -infty<T>, 0, 0};
    }
    f = shift(f, x0, a * x0 + b);
    T d = x1 - x0;
    if (d == 0) return f;
    // (0,0) から (x1,ax1) への線分をどこかに挿入する
    // 特に x0, y0 はこのままでよい
    if (f.x0 == f.x1) {
      return {nullptr, f.x0, f.x0 + d, a, f.y0};
    }
    // 先頭に挿入できる場合
    if (a <= f.a0) {
      ST.apply(f.root, d);
      f.root = ST.merge(ST.new_node({f.x0 + d, f.a0 - a}), f.root);
      f.x1 += d, f.a0 = a;
      return f;
    }
    // 末尾に挿入できる場合
    T a_last = f.a0 + ST.prod(f.root).fi;
    if (a_last <= a) {
      f.root = ST.merge(f.root, ST.new_node({f.x1, a - a_last}));
      f.x1 += d;
      return f;
    }
    // 間のどこかに挿入
    auto [l, r] = ST.split_max_right_prod(
        f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < a; });
    T asum = ST.prod(l).fi;
    T a1 = a - (asum + f.a0);
    auto [xx, aa] = ST.get(r, 0);
    ST.apply(r, d);
    ST.set(r, 0, {xx + d, aa - a1});
    f.root = ST.merge3(l, ST.new_node({xx, a1}), r);
    f.x1 += d;
    return f;
  }

  FUNC add_const(FUNC &f, T a) {
    f.y0 += a;
    return f;
  }

  FUNC add_linear(FUNC &f, T a, T b) {
    f.y0 += a * f.x0 + b;
    f.a0 += a;
    return f;
  }

  // (a-x)+
  FUNC add_a_minus_x(FUNC &f, T a) {
    auto [x0, x1] = domain(f);
    if (x0 > x1) return f;
    if (a <= x0) return f;
    if (x1 <= a) return add_linear(f, -1, a);
    vc<pair<T, T>> point;
    point.eb(x0, a - x0), point.eb(a, 0), point.eb(x1, 0);
    FUNC g = from_points(point);
    return add(f, g);
  }

  // (x-a)+
  FUNC add_x_minus_a(FUNC &f, T a) {
    auto [x0, x1] = domain(f);
    if (x0 > x1) return f;
    if (a <= x0) return add_linear(f, 1, -a);
    if (x1 <= a) return f;
    vc<pair<T, T>> point;
    point.eb(x0, 0), point.eb(a, 0), point.eb(x1, x1 - a);
    FUNC g = from_points(point);
    return add(f, g);
  }

  // |x-a|
  FUNC add_abs(FUNC &f, T a) {
    f = add_a_minus_x(f, a);
    f = add_x_minus_a(f, a);
    return f;
  }

  // fx,x
  pair<T, T> get_min(FUNC &f) {
    if (f.x0 > f.x1) return {infty<T>, 0};
    if (f.a0 >= 0) {
      return {f.y0, f.x0};
    }
    auto [l, r] = ST.split_max_right_prod(
        f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < 0; });
    auto [asum, xasum] = ST.prod(l);
    T x = (r ? ST.get(r, 0).fi : f.x1);
    f.root = ST.merge(l, r);
    T y = f.y0 + f.a0 * (x - f.x0) + x * asum - xasum;
    return {y, x};
  }

  FUNC clear_right(FUNC &f) {
    if (f.a0 >= 0) {
      ST.free_subtree(f.root), f.root = nullptr, f.a0 = 0;
      return f;
    }
    auto [l, r] = ST.split_max_right_prod(
        f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < 0; });
    f.root = l;
    if (!r) {
      return f;
    }
    T x = ST.get(r, 0).fi;
    ST.free_subtree(r);
    f.root = ST.merge(f.root, ST.new_node({x, -(f.a0 + ST.prod(l).fi)}));
    return f;
  }
  FUNC clear_left(FUNC &f) {
    if (f.a0 >= 0) {
      return f;
    }
    auto [l, r] = ST.split_max_right_prod(
        f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < 0; });
    auto [asum, xasum] = ST.prod(l);
    if (!r) {
      // 定数にする
      T x = f.x1;
      T y = f.y0 + f.a0 * (x - f.x0) + x * asum - xasum;
      ST.free_subtree(l);
      f.root = nullptr;
      f.y0 = y, f.a0 = 0;
      return f;
    }
    T x = ST.get(r, 0).fi;
    T y = f.y0 + f.a0 * (x - f.x0) + x * asum - xasum;
    T a = f.a0 + asum + ST.get(r, 0).se;
    ST.free_subtree(l);
    f.root = r;
    ST.set(r, 0, {x, a});
    f.y0 = y;
    f.a0 = 0;
    return f;
  }
#ifdef FASTIO
  void debug(FUNC &f) {
    auto dat = ST.get_all(f.root);
    SHOW(f.x0, f.x1, f.a0, f.y0);
    SHOW(dat);
  }
#endif
};
}  // namespace SLOPE_TRICK_SUPER
using SLOPE_TRICK_SUPER::Slope_Trick_Super;
#line 6 "test/1_mytest/slope_super.test.cpp"

void test(int N, bool from_zero, int add_prob) {
  Slope_Trick_Super<ll> ST;
  using F = typename decltype(ST)::FUNC;

  vc<int> L(2 * N - 1), R(2 * N - 1), A(N), B(N);
  vc<int> I(N);
  FOR(i, N) I[i] = i;

  FOR(i, N) {
    L[i] = RNG(0, 5);
    if (from_zero) L[i] = 0;
    R[i] = RNG(0, 5);
    if (L[i] > R[i]) swap(L[i], R[i]);
    A[i] = RNG(-3, 4);
    B[i] = RNG(-3, 4);
  }
  vvc<ll> dp(2 * N - 1);
  vc<F> func(2 * N - 1);
  FOR(i, N) {
    dp[i].resize(R[i] + 1, infty<ll>);
    FOR(x, L[i], R[i] + 1) dp[i][x] = A[i] * x + B[i];
    func[i] = ST.segment_func(L[i], R[i], A[i], B[i]);
    FOR(x, L[i], R[i] + 1) {
      ll me = ST.eval(func[i], x);
      ll god = dp[i][x];
      assert(me == god);
    }
  }

  FOR(i, N, 2 * N - 1) {
    shuffle(I);
    int t = (RNG(0, 100) < add_prob ? 0 : 1);
    int a = POP(I), b = POP(I);
    I.eb(i);
    if (dp[a].empty() || dp[b].empty()) continue;
    if (t == 0) {
      // ADD
      L[i] = max(L[a], L[b]);
      R[i] = min(R[a], R[b]);
      if (L[i] > R[i]) continue;
      dp[i].assign(R[i] + 1, infty<ll>);
      FOR(x, L[i], R[i] + 1) dp[i][x] = dp[a][x] + dp[b][x];
      F f = func[a], g = func[b];
      ST.restrict_domain(f, L[i], R[i]);
      ST.restrict_domain(g, L[i], R[i]);
      func[i] = ST.add(f, g);
    }
    if (t == 1) {
      // conv
      L[i] = L[a] + L[b];
      R[i] = R[a] + R[b];
      dp[i].assign(R[i] + 1, infty<ll>);
      FOR(x1, L[a], R[a] + 1) {
        FOR(x2, L[b], R[b] + 1) {
          chmin(dp[i][x1 + x2], dp[a][x1] + dp[b][x2]);
        }
      }
      func[i] = ST.convolve(func[a], func[b]);
    }
    vi X(R[i] + 1, infty<ll>);
    FOR(x, L[i], R[i] + 1) X[x] = ST.eval(func[i], x);
    assert(func[i].x0 == L[i]);
    assert(func[i].x1 == R[i]);
    assert(X == dp[i]);
    auto [fx, x] = ST.get_min(func[i]);
    assert(L[i] <= x && x <= R[i]);
    assert(MIN(X) == fx && X[x] == fx);
  }
  int i = 2 * N - 2;
  if (dp[i].empty()) return;
  int t = RNG(0, 2);
  if (t == 0) {
    // clear right
    FOR(x, L[i] + 1, R[i] + 1) chmin(dp[i][x], dp[i][x - 1]);
    ST.clear_right(func[i]);
  }
  if (t == 2) {
    // clear left
    FOR_R(x, L[i] + 1, R[i] + 1) chmin(dp[i][x - 1], dp[i][x]);
    ST.clear_left(func[i]);
  }
  vi X(R[i] + 1, infty<ll>);
  FOR(x, L[i], R[i] + 1) X[x] = ST.eval(func[i], x);
  assert(func[i].x0 == L[i]);
  assert(func[i].x1 == R[i]);
  assert(X == dp[i]);
}

void solve() {
  int x, y;
  cin >> x >> y;
  cout << x + y << "\n";
}

void test_repeated_middle_insert() {
  // (x-0)+, (x-2)+ の間に (x-1)+ を繰り返し加える。
  // DFS で直接差し込む実装ではここで木が片寄る。
  constexpr int N = 20000;
  Slope_Trick_Super<ll> ST;
  auto f = ST.segment_func(-1LL, 4LL, 0LL, 0LL);
  ST.add_x_minus_a(f, 0);
  ST.add_x_minus_a(f, 2);
  FOR(N) ST.add_x_minus_a(f, 1);
  assert(f.size() == N + 2);
  assert(ST.eval(f, -1) == 0);
  assert(ST.eval(f, 0) == 0);
  assert(ST.eval(f, 1) == 1);
  assert(ST.eval(f, 2) == N + 2);
  assert(ST.eval(f, 4) == 3LL * N + 6);
}

void test_sum_all_duplicate_breakpoints() {
  constexpr int N = 1000;
  Slope_Trick_Super<ll> ST;
  using F = typename decltype(ST)::FUNC;
  vc<F> funcs;
  FOR(N) {
    auto f = ST.segment_func(-1LL, 4LL, 0LL, 0LL);
    ST.add_x_minus_a(f, 1);
    funcs.eb(f);
  }
  auto f = ST.sum_all(funcs);
  assert(f.size() == N);
  assert(ST.eval(f, -1) == 0);
  assert(ST.eval(f, 1) == 0);
  assert(ST.eval(f, 4) == 3LL * N);
}

void test_unite_cancellation_and_lazy_shift() {
  Slope_Trick_Super<ll> ST;
  vc<pair<ll, ll>> p = {{-1, 0}, {1, 0}, {4, 3}};
  vc<pair<ll, ll>> q = {{-1, 0}, {1, 0}, {4, -3}};
  auto f = ST.from_points(p);
  auto g = ST.from_points(q);
  f = ST.add(f, g);
  assert(f.size() == 2);
  FOR(x, -1, 5) assert(ST.eval(f, x) == 0);

  f = ST.segment_func(-1LL, 4LL, 0LL, 0LL);
  ST.add_x_minus_a(f, 0);
  ST.add_x_minus_a(f, 2);
  ST.shift(f, 5, 7);
  g = ST.segment_func(4LL, 9LL, 0LL, 0LL);
  ST.add_x_minus_a(g, 6);
  f = ST.add(f, g);
  assert(ST.eval(f, 4) == 7);
  assert(ST.eval(f, 5) == 7);
  assert(ST.eval(f, 6) == 8);
  assert(ST.eval(f, 7) == 10);
  assert(ST.eval(f, 9) == 16);
}

void test_sum_all_random() {
  Slope_Trick_Super<ll> ST;
  using F = typename decltype(ST)::FUNC;
  FOR(100) {
    constexpr int L = -3, R = 4;
    vc<F> funcs;
    vc<ll> want(R - L + 1);
    int N = RNG(1, 30);
    FOR(N) {
      ll a = RNG(-3, 4), b = RNG(-3, 4);
      auto f = ST.segment_func(L, R, a, b);
      FOR(x, L, R + 1) want[x - L] += a * x + b;
      int p = RNG(L + 1, R);
      if (RNG(0, 2)) {
        ST.add_x_minus_a(f, p);
        FOR(x, L, R + 1) want[x - L] += max(0LL, x - p);
      } else {
        ST.add_a_minus_x(f, p);
        FOR(x, L, R + 1) want[x - L] += max(0LL, p - x);
      }
      funcs.eb(f);
    }
    auto f = ST.sum_all(funcs);
    FOR(x, L, R + 1) assert(ST.eval(f, x) == want[x - L]);
    auto dat = ST.ST.get_all(f.root);
    FOR(i, len(dat) - 1) assert(dat[i].fi <= dat[i + 1].fi);
  }
}

signed main() {
  vc<int> ns = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1000};
  vc<int> ps = {50, 100, 0, 95, 5};
  for (auto& n : ns) {
    for (auto& p : ps) {
      FOR(20) {
        test(n, false, p);
        test(n, true, p);
      }
    }
  }
  test_repeated_middle_insert();
  test_sum_all_duplicate_breakpoints();
  test_unite_cancellation_and_lazy_shift();
  test_sum_all_random();

  solve();
  return 0;
}
Back to top page