Submission #1344496

#TimeUsernameProblemLanguageResultExecution timeMemory
1344496Jakub_WozniakThe short shank; Redemption (BOI21_prison)C++20
70 / 100
97 ms34500 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
#define st first
#define nd second
const ll maxn = 2000000+912;
int t[maxn];
int pi[maxn];
int siz[maxn];
pii dep[maxn]; // {odleglosc , wierzcholek}
vector <int> tri[maxn];
bool czyS[maxn];
int n , D , T;

const int base = (1 << 21);
bool czy[base*2];

int Qp(int X )
{
    ////cerr << X << '\n';
    X += base;
    while(X)
    {
        if(czy[X])return 1;
        X /= 2;
    }
    return 0;
}

void Up(int l , int r)
{
    ////cerr << l << ' ' << r << '\n';

    if(r < l)return ;
    l += base;
    r += base;
    l--;
    r++;
    while(l/2 != r/2)
    {
        if((l&1) == 0)czy[l+1] = 1;
        if((r&1) == 1)czy[r-1] = 1;
        l /= 2;
        r /= 2;
    }
}

bool zap[base*2];

void DFS(int v)
{
    dep[v] = {1,v};
    siz[v] = 1;
    for(auto p : tri[v])
    {
        DFS(p);
        siz[v] += siz[p];
        dep[v] = max(dep[v] , {dep[p].st+1 , dep[p].nd});
    }

    //cerr << v << ": " << dep[v].st << ' ' << dep[v].nd << ' ' << siz[v] << '\n';
}

priority_queue <pii> Q;

int ido(int X)
{
    int ile = 0;
    while(X != -1 && zap[X])
    {
        ile++;
        zap[X] = 0;
        //cerr << X << ' ' << tri[X].size() << '\n';
        for(auto p : tri[X])
        {
            if(zap[p] == 0)continue;
            Q.push({dep[p].st,dep[p].nd});
        }
        X = pi[X];
    }
    return ile;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> D >> T;
    for(int i = 1; i<= n ;i++)
    {
        cin >> t[i];
        t[i] = T-t[i];
        zap[i] = 1;
    }

    stack <int > stck;
    for(int i = n ; i >= 1 ; i--)
    {
        if(t[i] >= 0)
        {
            pi[i] = -2;
            ////cerr << i << ' ' << i+t[i] << '\n';
            Up(i , min(i+t[i],n+123));
            continue;
        }
        while(stck.size() && Qp(stck.top()))stck.pop();
        if(stck.size() == 0)pi[i] = -1;
        else pi[i] = stck.top();
        stck.push(i);
    }

    for(int i = n ; i >= 1; i--)
    {
        if(t[i] >= 0)continue;
        if(!Qp(i))czyS[i] = 1;
    }

    vector <int> roots;
    int res = 0;

    vector<int> freee;
    for(int i = n;  i >= 1 ; i--)
    {
        if(pi[i] == -1){roots.push_back(i);}
        else if(pi[i] != -2)
        {
            //cerr << "KRAWEDAZ\n";
            tri[pi[i]].push_back(i);
        }

        if(czyS[i] == 1)freee.push_back(i);
    }

   
    for(auto p : roots)
    {
        if(zap[p] == 0)continue;
        DFS(p);
        Q.push({dep[p].st,p});
    }

    for(auto p : freee)
    {
        if(zap[p] == 0)continue;
        res += ido(p);
    }

    int lim = 0;
    while(!Q.empty() && lim < D)
    {
        auto p = Q.top(); 
        //cerr <<"Q:" <<  p.st << ' ' << p.nd << '\n';
        Q.pop(); 
        if(zap[p.nd] == 0)continue;
        lim++;
        res += ido(dep[p.nd].nd);;
    }

    cout << n-res << '\n';
    
    
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...