library

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

View the Project on GitHub tko919/library

:heavy_check_mark: Verify/LC_segment_add_get_min.test.cpp

Depends on

Code

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

#include "Template/template.hpp"

#include "DataStructure/lichaotree.hpp"


int main(){
    int N,Q;
    cin>>N>>Q;
    vector<ll> l(N),r(N),a(N),b(N);
    rep(i,0,N)cin>>l[i]>>r[i]>>a[i]>>b[i];
    vector<ll> t(Q),L(Q),R(Q),c(Q),d(Q);
    vector<ll> xs;
    rep(i,0,Q){
        cin>>t[i];
        if(t[i]==0)cin>>L[i]>>R[i]>>c[i]>>d[i];
        else{
            cin>>L[i];
            xs.push_back(L[i]);
        }
    }
    xs.push_back(-INF);
    xs.push_back(INF);
    sort(ALL(xs));
    xs.erase(unique(ALL(xs)),xs.end());

    CHT<ll,INF> cht(xs);
    rep(i,0,N)cht.add_segment(a[i],b[i],l[i],r[i]);
    rep(i,0,Q){
        if(t[i]==0)cht.add_segment(c[i],d[i],L[i],R[i]);
        else{
            ll ret=cht.getmin(L[i]);
            if(ret==INF)puts("INFINITY");
            else cout<<ret<<'\n';
        }
    }
    return 0;
}
#line 1 "Verify/LC_segment_add_get_min.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/segment_add_get_min"

#line 1 "Template/template.hpp"
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b-1); i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T, template <class> class C>
ostream &operator<<(ostream &os, const C<T> &v) {
    os << "[";
    for (auto d : v)
        os << d << ", ";
    os << "]";
    return os;
}
#line 2 "DataStructure/lichaotree.hpp"

template<typename T,T MX>struct CHT{
    using Line=pair<T,T>;
    int n;
    vector<T> xs;
    vector<Line> ls;
    CHT(vector<T>& ps){
        xs=ps;
        UNIQUE(xs);
        n=1;
        while(n<(int)xs.size())n<<=1;
        xs.resize(n,xs.back());
        ls.resize(2*n-1,Line(0,MX));
    }
    T eval(Line& f,T x){return f.first*x+f.second;}
    void add(T a,T b,int k=0,int L=0,int R=-1){
        if(R==-1)R=n;
        Line f={a,b};
        while(L!=R){
            int mid=(L+R)>>1;
            T lx=xs[L],mx=xs[mid],rx=xs[R-1];
            Line& g=ls[k];
            if(eval(f,lx)<eval(g,lx) and eval(f,rx)<eval(g,rx)){
                g=f;
                return;
            }
            if(eval(f,lx)>=eval(g,lx) and eval(f,rx)>=eval(g,rx))return;
            if(eval(f,mx)<eval(g,mx))swap(f,g);
            if(eval(f,lx)<eval(g,lx)){
                k=k*2+1;
                R=mid;
            }
            else{
                k=k*2+2;
                L=mid;
            }
        }
    }
    void add_segment(T a,T b,T L,T R){
        int l=lower_bound(ALL(xs),L)-xs.begin(),r=lower_bound(ALL(xs),R)-xs.begin();
        int a0=l,b0=r;
        l+=n,r+=n;
        int sz=1;
        while(l<r){
            if(r&1){
                r--;
                b0-=sz;
                add(a,b,r-1,b0,b0+sz);
            }
            if(l&1){
                add(a,b,l-1,a0,a0+sz);
                l++;
                a0+=sz;
            }
            l>>=1;
            r>>=1;
            sz<<=1;
        }
    }
    T getmin(T x){
        int k=lower_bound(ALL(xs),x)-xs.begin()+n-1;
        T res=eval(ls[k],x);
        while(k){
            k=(k-1)>>1;
            chmin(res,eval(ls[k],x));
        }
        return res;
    }
};

/**
 * @brief Convex Hull Trick (Li Chao Tree)
 * @docs docs/lichaotree.md
 */
#line 5 "Verify/LC_segment_add_get_min.test.cpp"

int main(){
    int N,Q;
    cin>>N>>Q;
    vector<ll> l(N),r(N),a(N),b(N);
    rep(i,0,N)cin>>l[i]>>r[i]>>a[i]>>b[i];
    vector<ll> t(Q),L(Q),R(Q),c(Q),d(Q);
    vector<ll> xs;
    rep(i,0,Q){
        cin>>t[i];
        if(t[i]==0)cin>>L[i]>>R[i]>>c[i]>>d[i];
        else{
            cin>>L[i];
            xs.push_back(L[i]);
        }
    }
    xs.push_back(-INF);
    xs.push_back(INF);
    sort(ALL(xs));
    xs.erase(unique(ALL(xs)),xs.end());

    CHT<ll,INF> cht(xs);
    rep(i,0,N)cht.add_segment(a[i],b[i],l[i],r[i]);
    rep(i,0,Q){
        if(t[i]==0)cht.add_segment(c[i],d[i],L[i],R[i]);
        else{
            ll ret=cht.getmin(L[i]);
            if(ret==INF)puts("INFINITY");
            else cout<<ret<<'\n';
        }
    }
    return 0;
}
Back to top page