ttamx's library

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

View on GitHub

:heavy_check_mark: verify/yosupo/data-structure/convex/line-container/line_add_get_min.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/line_add_get_min"
#include "template.hpp"
#include "data-structure/convex/line-container.hpp"

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int n,q;
    cin >> n >> q;
    LineContainer<ll,true> cht;
    for(int i=0;i<n;i++){
        ll a,b;
        cin >> a >> b;
        cht.insert(a,b);
    }
    while(q--){
        int op;
        cin >> op;
        if(op){
            ll x;
            cin >> x;
            cout << cht.query(x) << "\n";
        }else{
            ll a,b;
            cin >> a >> b;
            cht.insert(a,b);
        }
    }
}
#line 1 "verify/yosupo/data-structure/convex/line-container/line_add_get_min.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/line_add_get_min"
#line 2 "template.hpp"
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using db = long double;
using i128 = __int128_t;
using u32 = uint32_t;
using u64 = uint64_t;
template<class T>
using PQ = priority_queue<T,vector<T>,greater<T>>;

const int INF=INT_MAX/2;
const ll LINF=LLONG_MAX/4;
const db DINF=numeric_limits<db>::infinity();
const db EPS=1e-9;
const db PI=acos(db(-1));

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SZ(a) (int)(a.size())
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define REV(a) reverse(ALL(a))
#define UNI(a) a.erase(unique(ALL(a)),a.end())
#define LB(a,x) (int)(lower_bound(ALL(a),x)-a.begin())
#define UB(a,x) (int)(upper_bound(ALL(a),x)-a.begin())
#define FIND(a,x) (int)(find(ALL(a),x)-a.begin())
#define COUNT(a,x) (int)count(ALL(a),x)
#define MIN(a) *min_element(ALL(a))
#define MAX(a) *max_element(ALL(a))

template<class T,class U>
bool chmin(T &a,U b){return b<a?a=b,1:0;}
template<class T,class U>
bool chmax(T &a,U b){return a<b?a=b,1:0;}

template<class T>
T floor(T a,T b){return a/b-((a^b)<0&&a%b);}
template<class T>
T ceil(T a,T b){return a/b+((a^b)>=0&&a%b);}

int popcnt(ll x){return __builtin_popcountll(x);}
int topbit(ll x){return x==0?-1:63-__builtin_clzll(x);}
int lowbit(ll x){return x==0?-1:__builtin_ctzll(x);}

template<class T,class U>
T SUM(const U &a){return accumulate(ALL(a),T{});}
template<class T>
vector<int> argsort(const vector<T> &a){
    vector<int> p(SZ(a));
    iota(ALL(p),0);
    stable_sort(ALL(p),[&](int i,int j){return a[i]<a[j];});
    return p;
}

template<class T,size_t D=0>
struct VecBuilder{
    array<size_t,D> d;
    VecBuilder<T,D+1> operator[](size_t n)const{
        VecBuilder<T,D+1> res{};
        for(size_t i=0;i<D;i++)res.d[i]=d[i];
        res.d[D]=n;
        return res;
    }
    template<size_t I=0>
    auto build(const T &x)const{
        if constexpr(I+1==D)return vector<T>(d[I],x);
        else return vector<decltype(build<I+1>(x))>(d[I],build<I+1>(x));
    }
    auto operator()(const T &x=T())const{
        static_assert(D>0);
        return build(x);
    }
};
template<class T>
inline constexpr VecBuilder<T> VEC{};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
#line 2 "data-structure/convex/line-container.hpp"

template<class T>
struct LineContainerLine{
    mutable T m,c,p;
    LineContainerLine(T _m,T _c,T _p=0):m(_m),c(_c),p(_p){}
    bool operator<(const LineContainerLine &o)const{return m<o.m;}
    bool operator<(T x)const{return p<x;}
    inline T eval(T x)const{return m*x+c;}
};

template<class T,bool IS_MIN>
struct LineContainer:multiset<LineContainerLine<T>,less<>>{
    using Line = LineContainerLine<T>;
    using base = multiset<Line,less<>>;
    using iterator = typename base::iterator;
    static constexpr T INF=numeric_limits<T>::max();
    static T div(T a,T b){
        if constexpr(is_integral_v<T>)return a/b-((a^b)<0&&a%b);
        else return a/b;
    }
    bool isect(iterator x,iterator y){
        if(y==this->end())return x->p=INF,false;
        if(x->m==y->m)x->p=x->c>y->c?INF:-INF;
        else x->p=div(y->c-x->c,x->m-y->m);
        return x->p>=y->p;
    }
    void insert(T m,T c){
        if constexpr(IS_MIN)m=-m,c=-c;
        auto z=base::insert(Line(m,c)),y=z++,x=y;
        while(isect(y,z))z=this->erase(z);
        if(x!=this->begin()&&isect(--x,y))isect(x,y=this->erase(y));
        while((y=x)!=this->begin()&&(--x)->p>=y->p)isect(x,this->erase(y));
    }
    T query(T x){
        if(this->empty())return IS_MIN?INF:-INF;
        T res=this->lower_bound(x)->eval(x);
        return IS_MIN?-res:res;
    }
};
#line 4 "verify/yosupo/data-structure/convex/line-container/line_add_get_min.test.cpp"

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int n,q;
    cin >> n >> q;
    LineContainer<ll,true> cht;
    for(int i=0;i<n;i++){
        ll a,b;
        cin >> a >> b;
        cht.insert(a,b);
    }
    while(q--){
        int op;
        cin >> op;
        if(op){
            ll x;
            cin >> x;
            cout << cht.query(x) << "\n";
        }else{
            ll a,b;
            cin >> a >> b;
            cht.insert(a,b);
        }
    }
}
Back to top page