Submission #842432

#TimeUsernameProblemLanguageResultExecution timeMemory
842432arnold518Overtaking (IOI23_overtaking)C++17
100 / 100
406 ms46196 KiB
#include "overtaking.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1000;
const ll INF = 1e18;

int L, N;
pll P[MAXN+10];
int M, S[MAXN+10];
ll X;

int C[MAXN+10];
ll A[MAXN+10], B[MAXN+10], A2[MAXN+10][MAXN+10], ans[MAXN+10][MAXN+10];

struct Frac
{
    ll a, b;
    Frac(ll a, ll b) : a(a), b(b) {}
};
bool operator <= (Frac p, Frac q)
{
    if(p.a<0) p.a*=-1, p.b*=-1;
    if(q.a<0) q.a*=-1, q.b*=-1;
    return (__int128)p.b*q.a<=(__int128)q.b*p.a;
}

struct CHT
{
    vector<pair<pll, int>> V;
    CHT() {}
    Frac cross(pll p, pll q) { return {p.first-q.first, q.second-p.second}; }
    Frac f(pll p, ll y) { return {p.first, y-p.second}; }
    ll f2(pll p, ll y) { return (y-p.second+p.first-1)/p.first; }
    void push(pll p, int q)
    {
        while(!V.empty() && V.back().first.first<=p.first) V.pop_back();
        while(V.size()>1 && (V.back().first.first==p.first || cross(V[V.size()-2].first, V[V.size()-1].first)<=cross(V[V.size()-1].first, p))) V.pop_back();
        V.push_back({p, q});
    }
    pll query(ll y)
    {
        while(V.size()>1 && f(V[V.size()-2].first, y)<=f(V[V.size()-1].first, y)) V.pop_back();
        if(V.empty()) return {0, 0};
        else
        {
            return {V.back().second, f2(V.back().first, y)};
        }
    }
};
vector<ll> V1;
vector<CHT> V2;

void init(int _L, int _N, vector<ll> _T, vector<int> _W, int _X, int _M, vector<int> _S)
{
    L=_L; N=_N;
    for(int i=1; i<=N; i++) P[i]=pll(_W[i-1], _T[i-1]);
    M=_M; X=_X;
    for(int i=0; i<M; i++) S[i]=_S[i];
    M--;

    sort(P+1, P+N+1);
    reverse(P+1, P+N+1);
    while(N && P[N].first<=X) N--;

    for(int i=1; i<=N; i++) C[i]=i;
    for(int i=1; i<=N; i++) A[i]=P[i].second;
    for(int i=1; i<=N; i++) A2[i][0]=A[i];
    for(int i=1; i<=M; i++)
    {
        for(int j=1; j<=N; j++) B[j]=A[j]+(ll)(S[i]-S[i-1])*P[j].first;
        sort(C+1, C+N+1, [&](const int &p, const int &q) { return pll(A[p], B[p])<pll(A[q], B[q]); });
        ll t=0;
        for(int j=1; j<=N; j++) A[C[j]]=t=max(t, B[C[j]]);
        for(int j=1; j<=N; j++) A2[j][i]=A[j];
    }

    for(int i=M; i>=0; i--)
    {
        sort(C+1, C+N+1, [&](const int &p, const int &q) { return A2[p][i]<A2[q][i]; });
        CHT cht;
        for(int j=1; j<=N;)
        {
            auto p=cht.query(A2[C[j]][i]); p.second+=S[i];
            ll val;
            if(p.first==0) val=A2[C[j]][i]+X*(L-S[i]);
            else
            {
                if(p.second>L) val=A2[C[j]][i]+X*(L-S[i]);
                else
                {
                    int it=lower_bound(S, S+M+1, p.second)-S;
                    val=ans[p.first][it];
                }
            }
            ll tt=A2[C[j]][i];
            for(; j<=N && A2[C[j]][i]==tt; j++)
            {
                ans[C[j]][i]=val;
                cht.push({P[C[j]].first-X, A2[C[j]][i]}, C[j]);
            }
            if(i==0) V1.push_back(tt), V2.push_back(cht);
        }
    }
}

ll arrival_time(ll Y)
{
    auto it=lower_bound(V1.begin(), V1.end(), Y);
    if(it==V1.begin()) return Y+X*L;
    it--;
    auto &cht = V2[it-V1.begin()];
    int lo=0, hi=cht.V.size();
    while(lo+1<hi)
    {
        int mid=lo+hi>>1;
        if(cht.f(cht.V[mid].first, Y)<=cht.f(cht.V[mid-1].first, Y)) lo=mid;
        else hi=mid;
    }
    pll p;
    if(cht.V.empty()) p={0, 0};
    else p=pll(cht.V[lo].second, cht.f2(cht.V[lo].first, Y));
    
    if(p.first==0) return Y+X*L;
    else
    {
        if(p.second>L) return Y+X*L;
        else
        {
            int it=lower_bound(S, S+M+1, p.second)-S;
            return ans[p.first][it];
        }
    }
}

Compilation message (stderr)

overtaking.cpp: In function 'll arrival_time(ll)':
overtaking.cpp:120:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  120 |         int mid=lo+hi>>1;
      |                 ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...