Submission #254574

#TimeUsernameProblemLanguageResultExecution timeMemory
254574MKopchev코끼리 (Dancing Elephants) (IOI11_elephants)C++14
50 / 100
9097 ms16120 KiB
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;

const int nmax=1.5e5+42;

int n,l,where[nmax];

set< pair<int/*position*/,int/*id*/> > active;

int BLOCK;

int which_block[nmax];

void init(int N,int L,int X[])
{
    n=N;
    l=L;
    for(int i=0;i<N;i++)
    {
        where[i]=X[i];
        active.insert({where[i],i});
    }

    BLOCK=sqrt(n)+1;
}

int IN=0;

set< pair<int/*position*/,int/*id*/> > BLOCKS[nmax];

pair<int/*photos*/,int/*last from the same block*/> nxt[nmax];

pair<int/*position*/,int/*id*/> help[nmax];

int get_nxt(int who)
{
    /*
    cout<<"who= "<<who<<endl;
    cout<<"where: ";for(int i=0;i<n;i++)cout<<where[i]<<" ";cout<<endl;
    cout<<"active: ";for(auto k:active)cout<<k.first<<" "<<k.second<<"\t";cout<<endl;
    cout<<"nxt: ";for(int i=0;i<n;i++)cout<<nxt[i].first<<" "<<nxt[i].second<<"\t";cout<<endl;
    cout<<"BLOCKS: "<<endl;
    for(int i=0;i<(n-1)/BLOCK+1;i++)
    {
        cout<<i<<" -> ";for(auto k:BLOCKS[i])cout<<k.first<<" "<<k.second<<"\t";cout<<endl;
    }
    */
    int t=l+where[who];

    t++;

    set< pair<int/*position*/,int/*id*/> >::iterator it=active.lower_bound({t,0});

    if(it==active.end())return -1;

    return (*it).second;
}

int build_block(int which)
{
    vector< pair<int/*position*/,int/*id*/> > given={};
    for(auto k:BLOCKS[which])
        given.push_back(k);

    for(int i=given.size()-1;i>=0;i--)
    {
        if(given[i].first+l>=given[given.size()-1].first)nxt[given[i].second]={0,given[i].second};
        else
        {
            int moved=get_nxt(given[i].second);
            nxt[given[i].second]={nxt[moved].first+1,nxt[moved].second};
        }
    }
    /*
    cout<<"building "<<which<<endl;
    for(auto k:BLOCKS[which])cout<<k.first<<" "<<k.second<<"\t";cout<<endl;
    for(int i=0;i<given.size();i++)cout<<nxt[given[i].second].first<<" "<<nxt[given[i].second].second<<"\t";cout<<endl;
    system("pause");
    */
}

void build()
{
    for(int i=0;i<n;i++)
        help[i]={where[i],i};

    sort(help,help+n);

    int total=(n-1)/BLOCK+1;

    for(int i=0;i<total;i++)BLOCKS[i]={};

    for(int i=0;i<n;i++)
    {
        BLOCKS[i/BLOCK].insert({help[i].first,help[i].second});
        which_block[help[i].second]=i/BLOCK;
    }

    for(int i=0;i<total;i++)
        build_block(i);
}


int update(int id,int val)
{
    //cout<<"UPDATE "<<id<<" "<<val<<endl;

    if(n==1)return 1;

    if(IN==0)build();

    IN=(IN+1)%BLOCK;

    active.erase({where[id],id});
    BLOCKS[which_block[id]].erase({where[id],id});

    build_block(which_block[id]);

    where[id]=val;

    set< pair<int/*position*/,int/*id*/> >::iterator it=active.lower_bound({where[id],id});
    if(it==active.end())it--;

    int new_block=which_block[(*it).second];

    which_block[id]=new_block;

    active.insert({where[id],id});
    BLOCKS[which_block[id]].insert({where[id],id});

    build_block(which_block[id]);

    int ret=0,who=(*active.begin()).second;

    while(who!=-1)
    {
        ret+=nxt[who].first;

        who=nxt[who].second;

        ret++;

        who=get_nxt(who);
    }

    return ret;
}

/*
int N_,L_,X_[nmax];
int main()
{
    cin>>N_>>L_;
    for(int i=0;i<N_;i++)cin>>X_[i];

    init(N_,L_,X_);

    int q;
    cin>>q;

    for(int i=1;i<=q;i++)
    {
        int pos,val;
        cin>>pos>>val;

        cout<<update(pos,val)<<endl;
    }


    cout<<update(0,10)<<endl;

    cout<<update(5,11)<<endl;

    cout<<update(7,12)<<endl;

    cout<<update(8,13)<<endl;

    cout<<update(4,14)<<endl;

    cout<<update(6,15)<<endl;
    return 0;
}
*/

Compilation message (stderr)

elephants.cpp: In function 'int build_block(int)':
elephants.cpp:81:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
#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...