firiexp's Library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_line_add_get_min.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/line_add_get_min"

#include <algorithm>
#include <deque>
#include <limits>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>

#include "../util/fastio.cpp"
#include "../datastructure/li_chao_tree.cpp"

int main() {
    Scanner in;
    Printer out;

    int n, q;
    in.read(n, q);
    vector<pair<ll, ll>> init(n);
    for (int i = 0; i < n; ++i) {
        in.read(init[i].first, init[i].second);
    }

    struct Query {
        int t;
        ll a, b, p;
    };
    vector<Query> qs;
    qs.reserve(q);
    vector<ll> xs;
    xs.reserve(q);

    for (int i = 0; i < q; ++i) {
        int t;
        in.read(t);
        if (!t) {
            ll a, b;
            in.read(a, b);
            qs.push_back({0, a, b, 0});
        } else {
            ll p;
            in.read(p);
            qs.push_back({1, 0, 0, p});
            xs.push_back(p);
        }
    }

    LiChaoTree<ll> li(xs);
    for (auto [a, b] : init) li.add_line(a, b);

    for (auto qu : qs) {
        if (qu.t == 0) li.add_line(qu.a, qu.b);
        else out.println(li.query(qu.p));
    }
    return 0;
}
#line 1 "test/yosupo_line_add_get_min.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/line_add_get_min"

#include <algorithm>
#include <deque>
#include <limits>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>

#line 1 "util/fastio.cpp"
using namespace std;

extern "C" int fileno(FILE *);
extern "C" int isatty(int);

template<class T, class = void>
struct is_fastio_range : false_type {};

template<class T>
struct is_fastio_range<T, void_t<decltype(declval<T &>().begin()), decltype(declval<T &>().end())>> : true_type {};

template<class T, class = void>
struct has_fastio_value : false_type {};

template<class T>
struct has_fastio_value<T, void_t<decltype(declval<const T &>().value())>> : true_type {};

struct FastIoDigitTable {
    char num[40000];

    constexpr FastIoDigitTable() : num() {
        for (int i = 0; i < 10000; ++i) {
            int x = i;
            for (int j = 3; j >= 0; --j) {
                num[i * 4 + j] = char('0' + x % 10);
                x /= 10;
            }
        }
    }
};

struct Scanner {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE + 1];
    int idx, size;
    bool interactive;

    Scanner() : idx(0), size(0), interactive(isatty(fileno(stdin))) {}

    inline void load() {
        int len = size - idx;
        memmove(buf, buf + idx, len);
        if (interactive) {
            if (fgets(buf + len, BUFSIZE + 1 - len, stdin)) size = len + (int)strlen(buf + len);
            else size = len;
        } else {
            size = len + (int)fread(buf + len, 1, BUFSIZE - len, stdin);
        }
        idx = 0;
        buf[size] = 0;
    }

    inline void ensure() {
        if (idx + OFFSET > size) load();
    }

    inline void ensure_interactive() {
        if (idx == size) load();
    }

    inline char skip() {
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            return buf[idx++];
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        return buf[idx++];
    }

    template<class T, typename enable_if<is_integral<T>::value, int>::type = 0>
    void read(T &x) {
        if (interactive) {
            char c = skip();
            bool neg = false;
            if constexpr (is_signed<T>::value) {
                if (c == '-') {
                    neg = true;
                    ensure_interactive();
                    c = buf[idx++];
                }
            }
            x = 0;
            while (c >= '0') {
                x = x * 10 + (c & 15);
                ensure_interactive();
                c = buf[idx++];
            }
            if constexpr (is_signed<T>::value) {
                if (neg) x = -x;
            }
            return;
        }
        char c = skip();
        bool neg = false;
        if constexpr (is_signed<T>::value) {
            if (c == '-') {
                neg = true;
                c = buf[idx++];
            }
        }
        x = 0;
        while (c >= '0') {
            x = x * 10 + (c & 15);
            c = buf[idx++];
        }
        if constexpr (is_signed<T>::value) {
            if (neg) x = -x;
        }
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void read(T &x) {
        long long v;
        read(v);
        x = T(v);
    }

    template<class Head, class Next, class... Tail>
    void read(Head &head, Next &next, Tail &...tail) {
        read(head);
        read(next, tail...);
    }

    template<class T, class U>
    void read(pair<T, U> &p) {
        read(p.first, p.second);
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void read(T &a) {
        for (auto &x : a) read(x);
    }

    void read(char &c) {
        c = skip();
    }

    void read(string &s) {
        s.clear();
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            while (true) {
                int start = idx;
                while (idx < size && buf[idx] > ' ') ++idx;
                s.append(buf + start, idx - start);
                if (idx < size) break;
                load();
                if (size == 0) break;
            }
            if (idx < size) ++idx;
            return;
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        while (true) {
            int start = idx;
            while (idx < size && buf[idx] > ' ') ++idx;
            s.append(buf + start, idx - start);
            if (idx < size) break;
            load();
        }
        if (idx < size) ++idx;
    }
};

struct Printer {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE];
    int idx;
    bool interactive;
    inline static constexpr FastIoDigitTable table{};

    Printer() : idx(0), interactive(isatty(fileno(stdout))) {}
    ~Printer() { flush(); }

    inline void flush() {
        if (idx) {
            fwrite(buf, 1, idx, stdout);
            idx = 0;
        }
    }

    inline void pc(char c) {
        if (idx > BUFSIZE - OFFSET) flush();
        buf[idx++] = c;
        if (interactive && c == '\n') flush();
    }

    inline void print_range(const char *s, size_t n) {
        size_t pos = 0;
        while (pos < n) {
            if (idx == BUFSIZE) flush();
            size_t chunk = min(n - pos, (size_t)(BUFSIZE - idx));
            memcpy(buf + idx, s + pos, chunk);
            idx += (int)chunk;
            pos += chunk;
        }
    }

    void print(const char *s) {
        print_range(s, strlen(s));
    }

    void print(const string &s) {
        print_range(s.data(), s.size());
    }

    void print(char c) {
        pc(c);
    }

    void print(bool b) {
        pc(char('0' + (b ? 1 : 0)));
    }

    template<class T, typename enable_if<is_integral<T>::value && !is_same<T, bool>::value, int>::type = 0>
    void print(T x) {
        if (idx > BUFSIZE - 100) flush();
        using U = typename make_unsigned<T>::type;
        U y;
        if constexpr (is_signed<T>::value) {
            if (x < 0) {
                buf[idx++] = '-';
                y = U(0) - static_cast<U>(x);
            } else {
                y = static_cast<U>(x);
            }
        } else {
            y = x;
        }
        if (y == 0) {
            buf[idx++] = '0';
            return;
        }
        static constexpr int TMP_SIZE = sizeof(U) * 10 / 4;
        char tmp[TMP_SIZE];
        int pos = TMP_SIZE;
        while (y >= 10000) {
            pos -= 4;
            memcpy(tmp + pos, table.num + (y % 10000) * 4, 4);
            y /= 10000;
        }
        if (y >= 1000) {
            memcpy(buf + idx, table.num + (y << 2), 4);
            idx += 4;
        } else if (y >= 100) {
            memcpy(buf + idx, table.num + (y << 2) + 1, 3);
            idx += 3;
        } else if (y >= 10) {
            unsigned q = (unsigned(y) * 205) >> 11;
            buf[idx] = char('0' + q);
            buf[idx + 1] = char('0' + (unsigned(y) - q * 10));
            idx += 2;
        } else {
            buf[idx++] = char('0' + y);
        }
        memcpy(buf + idx, tmp + pos, TMP_SIZE - pos);
        idx += TMP_SIZE - pos;
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void print(const T &x) {
        print(x.value());
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void print(const T &a) {
        bool first = true;
        for (auto &&x : a) {
            if (!first) pc(' ');
            first = false;
            print(x);
        }
    }

    template<class T>
    void println(const T &x) {
        print(x);
        pc('\n');
    }

    template<class Head, class... Tail>
    void println(const Head &head, const Tail &...tail) {
        print(head);
        ((pc(' '), print(tail)), ...);
        pc('\n');
    }

    void println() {
        pc('\n');
    }
};

template<class T>
Scanner &operator>>(Scanner &in, T &x) {
    in.read(x);
    return in;
}

template<class T>
Printer &operator<<(Printer &out, const T &x) {
    out.print(x);
    return out;
}

/**
 * @brief 高速入出力(Fast IO)
 */
#line 1 "datastructure/li_chao_tree.cpp"
template<class T, bool get_max = false>
struct LiChaoTree {
    struct Line {
        T a, b;
        Line(T a = 0, T b = inf()) : a(a), b(b) {}
        T get(T x) const { return a * x + b; }
    };

    vector<T> xs;
    vector<Line> seg;
    int n;

    explicit LiChaoTree(vector<T> xs) : xs(xs) {
        sort(this->xs.begin(), this->xs.end());
        this->xs.erase(unique(this->xs.begin(), this->xs.end()), this->xs.end());
        n = (int)this->xs.size();
        seg.assign(max(1, 4 * n), Line());
    }

    void add_line(T a, T b) {
        if (n == 0) return;
        if (get_max) a = -a, b = -b;
        add_line_node(1, 0, n, Line(a, b));
    }

    void add_segment(T a, T b, T l, T r) {
        if (n == 0 || l >= r) return;
        if (get_max) a = -a, b = -b;
        int L = lower_bound(xs.begin(), xs.end(), l) - xs.begin();
        int R = lower_bound(xs.begin(), xs.end(), r) - xs.begin();
        if (L >= R) return;
        add_segment_node(1, 0, n, L, R, Line(a, b));
    }

    T query(T x) const {
        if (n == 0) return get_max ? -inf() : inf();
        int i = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
        if (i == n || xs[i] != x) return get_max ? -inf() : inf();
        T ret = query_node(1, 0, n, i, x);
        return get_max ? -ret : ret;
    }

private:
    static constexpr T inf() {
        return numeric_limits<T>::max() / 4;
    }

    void add_line_node(int k, int l, int r, Line x) {
        int m = (l + r) / 2;
        bool lef = x.get(xs[l]) < seg[k].get(xs[l]);
        bool mid = x.get(xs[m]) < seg[k].get(xs[m]);
        if (mid) swap(seg[k], x);
        if (r - l == 1) return;
        if (lef != mid) add_line_node(k * 2, l, m, x);
        else add_line_node(k * 2 + 1, m, r, x);
    }

    void add_segment_node(int k, int l, int r, int a, int b, Line x) {
        if (r <= a || b <= l) return;
        if (a <= l && r <= b) {
            add_line_node(k, l, r, x);
            return;
        }
        int m = (l + r) / 2;
        add_segment_node(k * 2, l, m, a, b, x);
        add_segment_node(k * 2 + 1, m, r, a, b, x);
    }

    T query_node(int k, int l, int r, int i, T x) const {
        T ret = seg[k].get(x);
        if (r - l == 1) return ret;
        int m = (l + r) / 2;
        if (i < m) return min(ret, query_node(k * 2, l, m, i, x));
        return min(ret, query_node(k * 2 + 1, m, r, i, x));
    }
};

template<class T, bool get_max = false>
struct OnlineLiChaoTree {
    struct Line {
        T a, b;
        Line(T a = 0, T b = inf()) : a(a), b(b) {}
        T get(T x) const { return a * x + b; }
    };

    struct Node {
        Line line;
        int l, r;
        explicit Node(const Line &line) : line(line), l(-1), r(-1) {}
    };

    T low, high;
    int root;
    deque<Node> nodes;

    explicit OnlineLiChaoTree(T low, T high) : low(low), high(high), root(-1) {}

    void add_line(T a, T b) {
        if (get_max) a = -a, b = -b;
        add_line(root, low, high, Line(a, b));
    }

    void add_segment(T a, T b, T l, T r) {
        if (l >= r) return;
        if (get_max) a = -a, b = -b;
        add_segment(root, low, high, l, r, Line(a, b));
    }

    T query(T x) const {
        T ret = query(root, low, high, x);
        return get_max ? -ret : ret;
    }

private:
    static constexpr T inf() {
        return numeric_limits<T>::max() / 4;
    }

    int new_node(const Line &line) {
        nodes.emplace_back(line);
        return (int)nodes.size() - 1;
    }

    void add_line(int &t, T l, T r, Line x) {
        if (t == -1) {
            t = new_node(x);
            return;
        }
        Node &node = nodes[t];
        T m = l + (r - l) / 2;
        bool lef = x.get(l) < node.line.get(l);
        bool mid = x.get(m) < node.line.get(m);
        if (mid) swap(node.line, x);
        if (r - l == 1) return;
        if (lef != mid) add_line(node.l, l, m, x);
        else add_line(node.r, m, r, x);
    }

    void add_segment(int &t, T l, T r, T a, T b, Line x) {
        if (r <= a || b <= l) return;
        if (a <= l && r <= b) {
            add_line(t, l, r, x);
            return;
        }
        if (t == -1) t = new_node(Line());
        Node &node = nodes[t];
        T m = l + (r - l) / 2;
        if (a < m) add_segment(node.l, l, m, a, b, x);
        if (m < b) add_segment(node.r, m, r, a, b, x);
    }

    T query(int t, T l, T r, T x) const {
        T ret = inf();
        while (t != -1) {
            const Node &node = nodes[t];
            ret = min(ret, node.line.get(x));
            if (r - l == 1) break;
            T m = l + (r - l) / 2;
            if (x < m) {
                t = node.l;
                r = m;
            } else {
                t = node.r;
                l = m;
            }
        }
        return ret;
    }
};

/**
 * @brief Li Chao Tree
 */
#line 19 "test/yosupo_line_add_get_min.test.cpp"

int main() {
    Scanner in;
    Printer out;

    int n, q;
    in.read(n, q);
    vector<pair<ll, ll>> init(n);
    for (int i = 0; i < n; ++i) {
        in.read(init[i].first, init[i].second);
    }

    struct Query {
        int t;
        ll a, b, p;
    };
    vector<Query> qs;
    qs.reserve(q);
    vector<ll> xs;
    xs.reserve(q);

    for (int i = 0; i < q; ++i) {
        int t;
        in.read(t);
        if (!t) {
            ll a, b;
            in.read(a, b);
            qs.push_back({0, a, b, 0});
        } else {
            ll p;
            in.read(p);
            qs.push_back({1, 0, 0, p});
            xs.push_back(p);
        }
    }

    LiChaoTree<ll> li(xs);
    for (auto [a, b] : init) li.add_line(a, b);

    for (auto qu : qs) {
        if (qu.t == 0) li.add_line(qu.a, qu.b);
        else out.println(li.query(qu.p));
    }
    return 0;
}
Back to top page