lmori's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub lmorinn/library

:heavy_check_mark: verify/LibraryChecker/data-structure/others/SegmentAddGetMin.test.cpp

Depends on

Code

#include "../../../../template/template.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/segment_add_get_min"
#include "../../../../data-structure/others/LiChaoTree.hpp"

int main() {
    cin.tie(0)->sync_with_stdio(0);
    LiChaoTree t(-1e9, 1e9 + 1);
    int n, q;
    in(n, q);
    rep(i, n) {
        lint l, r, a, b;
        in(l, r, a, b);
        t.add_segment(l, r, a, b);
    }

    rep(i, q) {
        int com;
        in(com);
        if (com == 0) {
            lint l, r, a, b;
            in(l, r, a, b);
            t.add_segment(l, r, a, b);
        } else {
            lint p;
            in(p);
            lint res = t.get_min(p);
            if (res == LLONG_MAX) {
                out("INFINITY");
            } else {
                out(res);
            }
        }
    }
}
#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
  cout << fixed << setprecision(14) << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion
#line 2 "verify/LibraryChecker/data-structure/others/SegmentAddGetMin.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/segment_add_get_min"
#line 1 "data-structure/others/LiChaoTree.hpp"

class LiChaoTree {
   private:
    struct Node {
        long long a, b;
        Node *left;
        Node *right;
        Node(long long a, long long b) : a(a), b(b), left(nullptr), right(nullptr) {}
    };
    long long lower_x, upper_x;
    Node *root = nullptr;

    inline long long f(long long a, long long b, long long x) {
        return a * x + b;
    }

    void add_line(long long line_a, long long line_b, Node *&t, long long l, long long r) {
        if (!t) {
            t = new Node(line_a, line_b);
            return;
        }

        if (r - l == 1) {
            if (f(line_a, line_b, l) < f(t->a, t->b, l)) {
                t->a = line_a;
                t->b = line_b;
            }
            return;
        }
        long long m = (l + r) >> 1ll;

        bool left_min = f(line_a, line_b, l) < f(t->a, t->b, l);
        bool mid_min = f(line_a, line_b, m) < f(t->a, t->b, m);
        bool right_min = f(line_a, line_b, r) < f(t->a, t->b, r);

        if (left_min and right_min) {
            t->a = line_a;
            t->b = line_b;
            return;
        }
        if (!left_min and !right_min) {
            return;
        }

        if (mid_min) {
            swap(t->a, line_a);
            swap(t->b, line_b);
        }
        if (left_min != mid_min) {
            add_line(line_a, line_b, t->left, l, m);
        } else {
            add_line(line_a, line_b, t->right, m, r);
        }
    }

    void add_segment(long long line_a, long long line_b, Node *&t, long long a, long long b, long long l, long long r) {
        if (r <= a or b <= l) return;
        if (!t) t = new Node(0, LLONG_MAX);

        long long m = (l + r) >> 1ll;
        if (!(a <= l and r <= b)) {
            add_segment(line_a, line_b, t->left, a, b, l, m);
            add_segment(line_a, line_b, t->right, a, b, m, r);
            return;
        }
        if (t->a == 0 and t->b == LLONG_MAX) {
            t->a = line_a;
            t->b = line_b;
        } else {
            add_line(line_a, line_b, t, l, r);
        }
    }

    long long get_min(long long x, Node *t, long long l, long long r) {
        if (!t) return LLONG_MAX;
        long long y = f(t->a, t->b, x);
        if (r - l == 1) return y;
        long long m = (l + r) >> 1ll;
        if (x < m) {
            return min(y, get_min(x, t->left, l, m));
        } else {
            return min(y, get_min(x, t->right, m, r));
        }
    }

   public:
    LiChaoTree() {}
    LiChaoTree(long long lower_x, long long upper_x) : lower_x(lower_x), upper_x(upper_x) {
    }

    void add_line(long long a, long long b) {
        add_line(a, b, root, lower_x, upper_x);
    }

    void add_segment(long long l, long long r, long long a, long long b) {
        add_segment(a, b, root, l, r, lower_x, upper_x);
    }

    long long get_min(long long x) {
        return get_min(x, root, lower_x, upper_x);
    }
};
#line 4 "verify/LibraryChecker/data-structure/others/SegmentAddGetMin.test.cpp"

int main() {
    cin.tie(0)->sync_with_stdio(0);
    LiChaoTree t(-1e9, 1e9 + 1);
    int n, q;
    in(n, q);
    rep(i, n) {
        lint l, r, a, b;
        in(l, r, a, b);
        t.add_segment(l, r, a, b);
    }

    rep(i, q) {
        int com;
        in(com);
        if (com == 0) {
            lint l, r, a, b;
            in(l, r, a, b);
            t.add_segment(l, r, a, b);
        } else {
            lint p;
            in(p);
            lint res = t.get_min(p);
            if (res == LLONG_MAX) {
                out("INFINITY");
            } else {
                out(res);
            }
        }
    }
}
Back to top page