Submission #1227436

#TimeUsernameProblemLanguageResultExecution timeMemory
1227436vivkostovCandies (JOI18_candies)C++20
0 / 100
3 ms576 KiB
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
struct cell
{
    long long int pos,st,type,to;
    bool operator<(const cell&a)const
    {
        return st<a.st;
    }
};
mt19937 mt(time(nullptr));
long long int n,a[200005];
long long int sum[200005],otg;
map<int,int>m;
priority_queue<cell>q;
void prec()
{
    sum[1]=a[1];
    for(int i=2; i<=n; i++)
    {
        sum[i]=sum[i-2]+a[i];
    }
}
void er(int val)
{
    if(m[val]==0)m.erase(val);
}
void merg(int a,int b)
{
    m[b]=m[a];
    m.erase(a);
}
void fix(int a)
{
    m[a+1]=m[a]-1;
    m.erase(a);
}
void check(cell&w)
{
    auto up=m.upper_bound(w.pos);
    if(m[m[w.pos]-2])
    {
        merg(m[w.pos]-2,w.pos);
    }
    else er(m[w.pos]-2);
    if(up->second==w.pos+2)
    {
        merg(w.pos,up->first);
        w.pos=up->first;
    }
}
void add(cell w)
{
    if(w.pos==n||m[w.pos]==1)return;
    cell c;
    c.pos=w.pos;
    c.to=m[w.pos];
    c.type=2;
    c.st=sum[w.pos+1]-sum[w.pos]+sum[m[w.pos]-2];
    if(m[w.pos]>2)c.st-=sum[m[w.pos]-3];
    //cout<<c.pos<<" "<<c.to<<" "<<c.st<<" "<<c.type<<" "<<w.pos<<endl;
    q.push(c);
}
void resh()
{
    cell w;
    while(q.size())
    {
        w=q.top();
        q.pop();
        if(w.type==1)
        {
            auto up=m.upper_bound(w.pos);
            if(m[w.pos-1]||up->second==w.pos+1||(up->second<w.pos&up->first>w.pos))
            {
                er(w.pos-1);
                continue;
            }
            er(w.pos-1);
            otg+=w.st;
            m[w.pos]=w.pos;
            check(w);
        }
        if(w.type==2)
        {
            if(m[w.pos]!=w.to)
            {
                continue;
            }
            otg+=w.st;
            fix(w.pos);
            w.pos++;
            check(w);
        }
        add(w);
        cout<<otg<<endl;
    }
}
void read()
{
    cell c;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        c.pos=i;
        c.st=a[i];
        c.type=1;
        c.to=0;
        q.push(c);
    }
    prec();
    resh();
}
int main()
{
    speed();
    read();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...