Submission #1324483

#TimeUsernameProblemLanguageResultExecution timeMemory
1324483modwwe모임들 (IOI18_meetings)C++20
60 / 100
708 ms851968 KiB
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
//#define ll long long
#define ll long long
#define ull unsigned long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define modwwe  ll t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task2 "top1apio"
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define mask(k) (1ll<<k)
#define checktime   cerr <<  (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
#define getchar_unlocked getchar
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
using i128 = __int128;
ll rand(ll l, ll r)
{
    return uniform_int_distribution<ll>(l, r)(rd);
}
void phongbeo();
const ll inf = 1e9;
const ll mod2 = 998244353;
//const ll base=67;
ll  n, m, s1, s2, s4, s3, sf, k, s5, s6, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2,
                                                               r2, center;
ll  i, s10, s12, k1, k2, k3, s11, lim, w, l, r, dem5, dem6, dem7, dem9, root,q;
ll el = 19;
int st[20][750007];
ll a[750007];
int f[20][750007];
vector<ll> ans;
struct id
{
    int a,b,c;
};
vector<id>v[750007];
ll get(ll l,ll r)
{
    ll k=log2(r-l+1);
    if(st[k][l]>=st[k][r-mask(k)+1])return f[k][l];
    return f[k][r-mask(k)+1];
}
struct ic
{
    int a,b,c,d;
};
struct segtree
{
    ic t[3000001];
    int f[750007];
    ic mer(ic a,ic b)
    {
        return {max(a.a,b.a),min(a.b,b.b),min(a.c,b.c),max(a.d,b.d)};
    }
    void upd(int node,int l,int r,int x,int y)
    {
        if(l==r)
        {
            f[l]=y;
            if(y==-1)t[node]= {y,inf,inf,0};
            else  t[node]= {y,y,l,l};
            return;
        }
        int mid=l+r>>1;
        if(x<=mid)upd(node<<1,l,mid,x,y);
        else  upd(node<<1|1,mid+1,r,x,y);
        t[node]=mer(t[node<<1],t[node<<1|1]);
    }
    int fl(int node,int l,int r,int l1,int r1,int x)
    {
        if(l>r1||r<l1||t[node].b>x)return -1;
        if(l==r)
        {
            return l;
        }
        int mid=l+r>>1;
        int haha=fl(node<<1|1,mid+1,r,l1,r1,x);
        if(haha==-1)return fl(node<<1,l,mid,l1,r1,x);
        return haha;
    }
    int fr(int node,int l,int r,int l1,int r1,int x)
    {
        if(l>r1||r<l1||t[node].a<x)return -1;
        if(l==r)return l;
        int mid=l+r>>1;
        int haha=fr(node<<1,l,mid,l1,r1,x);
        if(haha==-1)return fr(node<<1|1,mid+1,r,l1,r1,x);
        return haha;
    }
    void build(int node,int l,int r,int x)
    {
        if(l==r)
        {
            f[l]=x;
            t[node]= {x,x,l,l};
            return;
        }
        int mid=l+r>>1;
        build(node<<1,l,mid,x);
        build(node<<1|1,mid+1,r,x);
        t[node]=mer(t[node<<1],t[node<<1|1]);
    }
    ic get(int node,int l,int r,int l1,int r1)
    {
        if(l>r1||r<l1)return {-1,-1,inf,0};
        if(l>=l1&&r<=r1)return t[node];
        int mid=l+r>>1;
        if(l1>mid)return get(node<<1|1,mid+1,r,l1,r1);
        if(r1<=mid)return get(node<<1,l,mid,l1,r1);
        return mer(get(node<<1,l,mid,l1,r1),get(node<<1|1,mid+1,r,l1,r1));
    }
} lf,rt;
struct jump
{
    ll cost[20][750001];
    int st[20][750001];
    ll gx(ll l,ll z,ll mx,ll h)
    {
        for(ll i=19; i>=0; --i)
            if(st[i][l]<=n&&st[i][l]>0&&a[st[i][l]]<mx&&cost[i][l]>abs(st[i][l]-l)*mx+z)
            {
                z=z+abs(st[i][l]-l)*mx-cost[i][l];
                l=st[i][l];
            }
        if(cost[0][l]>abs(st[0][l]-l)*mx+z)
        {
            if(h==1)return inf;
            return 0;
        }
        ll low=z/(mx-a[l])+1;
        return l-low*h;
    }
    ll gr(ll l,ll r)
    {
        ll s=0;
        for(ll i=19; i>=0; --i)
            if(st[i][l]<=r)
                s+=cost[i][l],l=st[i][l];
        return s+a[l]*(r-l+1);
    }
    ll gl(ll l,ll r)
    {
        ll s=0;
        for(ll i=19; i>=0; --i)
            if(st[i][l]>=r)
                s+=cost[i][l],l=st[i][l];
        return s+a[l]*(l-r+1);
    }
    void build()
    {
        for(ll i=1; i<=19; i++)
            for(ll j=0; j<=n+1; j++)
                st[i][j]=st[i-1][st[i-1][j]],
                         cost[i][j]=cost[i-1][j]+cost[i-1][st[i-1][j]];
    }
} hl,hr;
bool ok=0;
bool checkl(ll x,ll l,ll r)
{
    if(lf.f[x]==-1||x==l||x==r)return 0;
    ll hcl=lf.get(1,1,n,x+1,r).c;
    if(hcl!=-1&&lf.f[x]>lf.f[hcl])return 1;
    return 0;
}
bool checkr(ll x,ll l,ll r)
{
    if(rt.f[x]==-1||x==l||x==r)return 0;
    ll hcr=rt.get(1,1,n,l,x-1).d;
    if(hcr!=-1&&rt.f[x]<rt.f[hcr])return 1;
    return 0;
}
void dell(ll x)
{
    lf.upd(1,1,n,x,-1);
    ok=1;
}
void del(ll x)
{
    rt.upd(1,1,n,x,-1);
    ok=1;
}
void mg(ll x,ll y,ll l,ll r,ll mx)
{
    ll cy=(hr.gr(y,r)+hl.gl(y,x)-a[y]-mx)-(hr.gr(x,r)-a[x]);
    if(cy+mx-a[x]>=0)lf.upd(1,1,n,y,x+1);
    else
    {
        ll dame=hl.gx(x,cy,mx,-1);
        if(dame<l)dell(x);
        else  lf.upd(1,1,n,y,dame);
    }
}
void mc(ll x,ll y,ll l,ll r,ll mx)
{
    ll cy=(hl.gl(x,l)+hr.gr(x,y)-a[x]-mx)-hl.gl(y,l)+a[y];
    if(cy+mx-a[y]>=0)rt.upd(1,1,n,x,y-1);
    else
    {
        ll dame=hr.gx(y,cy,mx,1);
        if(dame>r)del(y);
        else rt.upd(1,1,n,x,dame);
    }
}
void buff(ll l,ll mid,ll r)
{
    if(l==r)return;
    ll cx=a[mid];
    while(true)
    {
        ok=0;
        if(lf.f[mid]==-1)break;
        ll hx=lf.get(1,1,n,l,mid-1).d;
        if(hx<1)break;
        mg(hx,mid,l,mid,cx);
        if(checkl(hx,l,r))dell(hx);
        if(!ok)break;
    }
    while(true)
    {
        ok=0;
        if(rt.f[mid]==-1)break;
        ll hx=rt.get(1,1,n,l,mid-1).d;
        if(hx<1)break;
        mc(hx,mid,l,mid,cx);
        if(checkr(hx,l,r))del(hx);
        if(!ok)break;
    }
    while(true)
    {
        ok=0;
        ll hx=lf.get(1,1,n,l,mid).d;
        ll hy=lf.get(1,1,n,mid+1,r).c;
        if(hx<1||hy>n)break;
        mg(hx,hy,l,r,cx);
        if(checkl(hx,l,r))dell(hx);
        else if(checkl(hy,l,r))dell(hy);
        if(!ok) break;
    }
    while(true)
    {
        ok=0;
        ll hx=rt.get(1,1,n,l,mid).d;
        ll hy=rt.get(1,1,n,mid+1,r).c;
        if(hx<1||hy>n)break;
        mc(hx,hy,l,r,cx);
        if(checkr(hx,l,r))del(hx);
        else if(checkr(hy,l,r))del(hy);
        if(!ok) break;
    }
}
void dnc(ll l,ll r)
{
    if(l>r)return;
    ll mid=get(l,r);
    dnc(l,mid-1);
    dnc(mid+1,r);
    for(auto x:v[mid])
    {
        ans[x.a]=a[mid]*(x.c-x.b+1);
        if(mid>x.b)
        {
            ll hihi=lf.fl(1,1,n,x.b,mid-1,x.b);

            ans[x.a]=min(ans[x.a],hl.gl(hihi,x.b)+hr.gr(hihi,x.c)-a[hihi]);
        }
        if(mid<x.c)
        {
            ll hihi=rt.fr(1,1,n,mid+1,x.c,x.c);
            ans[x.a]=min(ans[x.a],hl.gl(hihi,x.b)+hr.gr(hihi,x.c)-a[hihi]);
        }
    }
    buff(l,mid,r);
}
vector<ll> minimum_costs(vector<int> H,vector<int> L,vector<int> R)
{
    n=H.size();
    for(ll i=1; i<=n; i++)
        a[i]=H[i-1];
    for(ll i=1; i<=n; i++)
        st[0][i]=a[i],f[0][i]=i;
    for(ll i=1; i<19; i++)
        for(ll j=1; j+mask(i)-1<=n; j++)
        {
            st[i][j]=max(st[i-1][j],st[i-1][j+mask(i-1)]);
            if(st[i][j]==st[i-1][j])f[i][j]=f[i-1][j];
            else f[i][j]=f[i-1][j+mask(i-1)];
        }
    stack<ll> s;
    s.push(0);
    for(ll i=1; i<=n; i++)
    {
        while(s.top()!=0&&a[i]>a[s.top()])s.pop();
        hl.st[0][i]=s.top();
        hl.cost[0][i]=(i-s.top())*a[i];
        s.push(i);
    }
    while(!s.empty())s.pop();
    s.push(n+1);
    hr.st[0][n+1]=n+1;
    for(ll i=n; i>=1; --i)
    {
        while(s.top()!=n+1&&a[i]>a[s.top()])s.pop();
        hr.st[0][i]=s.top();
        hr.cost[0][i]=(s.top()-i)*a[i];
        s.push(i);
    }
    ans.resize(L.size());
    hl.build();
    hr.build();
    lf.build(1,1,n,1);
    rt.build(1,1,n,n);
    for(ll i=0; i<L.size(); i++)
    {
        v[get(L[i]+1,R[i]+1)].pb({i,L[i]+1,R[i]+1});
    }
    dnc(1,n);
    return ans;
}

Compilation message (stderr)

meetings.cpp: In function 'std::vector<long long int> minimum_costs(std::vector<int>, std::vector<int>, std::vector<int>)':
meetings.cpp:320:35: warning: narrowing conversion of 'i' from 'long long int' to 'int' [-Wnarrowing]
  320 |         v[get(L[i]+1,R[i]+1)].pb({i,L[i]+1,R[i]+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...