Submission #1327134

#TimeUsernameProblemLanguageResultExecution timeMemory
1327134SSKMFRabbit Carrot (LMIO19_triusis)C++20
100 / 100
60 ms12892 KiB
#include <bits/stdc++.h>
using namespace std;

int64_t maxim[800002] , lazy[800002];

inline void Lazy_Update (const int nod , const int stanga , const int dreapta)
{
    if (!lazy[nod])
        { return; }

    if (stanga != dreapta)
    {
        const int mijloc = (stanga + dreapta) >> 1;
        lazy[nod + ((mijloc - stanga + 1) << 1)] += lazy[nod];
        lazy[nod + 1] += lazy[nod];
    }

    maxim[nod] += lazy[nod];
    lazy[nod] = 0;
}

int64_t Query (const int nod , const int stanga , const int dreapta , const int indice)
{
    Lazy_Update(nod , stanga , dreapta);

    if (stanga == dreapta)
        { return maxim[nod]; }

    const int mijloc = (stanga + dreapta) >> 1;
    if (indice <= mijloc)
        { return Query(nod + 1 , stanga , mijloc , indice); }

    return Query(nod + ((mijloc - stanga + 1) << 1) , mijloc + 1 , dreapta , indice);
}

void Insert (const int nod , const int stanga , const int dreapta , const int indice , const int valoare)
{
    Lazy_Update(nod , stanga , dreapta);

    if (stanga == dreapta)
        { maxim[nod] = valoare; return; }

    const int mijloc = (stanga + dreapta) >> 1;
    if (indice <= mijloc)
        { Insert(nod + 1 , stanga , mijloc , indice , valoare); maxim[nod] = maxim[nod + 1]; }
    else
        { Insert(nod + ((mijloc - stanga + 1) << 1) , mijloc + 1 , dreapta , indice , valoare); }
}

inline int Search (const int nod , const int stanga , const int dreapta , const int inceput , const int sfarsit , const int minim)
{
    Lazy_Update(nod , stanga , dreapta);

    if (dreapta < inceput || stanga > sfarsit || maxim[nod] < minim)
        { return -1; }

    if (stanga == dreapta)
        { return stanga; }

    const int mijloc = (stanga + dreapta) >> 1;
    const int candidat = Search(nod + ((mijloc - stanga + 1) << 1) , mijloc + 1 , dreapta , inceput , sfarsit , minim);
    return candidat == -1 ? Search(nod + 1 , stanga , mijloc , inceput , sfarsit , minim) : candidat;
}

int main ()
{
    ios :: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int lungime , limita;
    cin >> lungime >> limita;

    int inceput = 0 , sfarsit = lungime;
    for (int indice = 1 , valoare ; indice <= lungime ; indice++)
    {
        cin >> valoare;

        if (Query(1 , 0 , 2 * lungime , inceput) < valoare - limita)
        {
            lazy[1] += limita;
            inceput++;
            continue;
        }

        if (Query(1 , 0 , 2 * lungime , sfarsit) >= valoare - limita)
        {
            lazy[1] += limita;
            inceput++;
            sfarsit++;
            Insert(1 , 0 , 2 * lungime , sfarsit , valoare);
            continue;
        }

        int locatie = Search(1 , 0 , 2 * lungime , inceput , sfarsit , valoare - limita);
        lazy[1] += limita;
        inceput++;
        Insert(1 , 0 , 2 * lungime , locatie + 1 , valoare);
    }

    cout << lungime - (sfarsit - inceput);
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...