Cod sursa(job #2331770)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 29 ianuarie 2019 21:50:44
Problema Schi Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <bits/stdc++.h>
#define maxn 30000
#define maxl2 15

using namespace std;

struct nod
{
  int loc, ind; /// pop = is populated
  bool pop;
  int upi; /// indice update
};

struct data
{
  int loc, ind;
};

nod aib[maxn*maxl2+5];
int v[maxn+5];
vector<data> upd;

ifstream fin ( "schi.in" );
ofstream fout ( "schi.out" );

void add ( int i, int p )
{
  int sz = upd.size();
  for ( ; aib[p].upi < sz; aib[p].upi++ )
    aib[p].loc += (aib[p].ind < upd[aib[p].upi].ind && aib[p].loc >= upd[aib[p].upi].loc );

  if ( aib[p*2].pop == 0 )
  {
    if ( v[i] <= aib[p].loc )
    {
      aib[p*2] = { v[i], i, 1, sz };
      upd.push_back ( {v[i],i} );
      return;
    }
  }
  else if ( v[i] <= aib[p].loc ) add ( i, p * 2 );

  if ( aib[p*2+1].pop == 0 )
  {
    if ( v[i] > aib[p].loc )
    {
      aib[p*2+1] = { v[i], i, 1, sz };
      upd.push_back ( {v[i],i} );
      return;
    }
  }
  else if ( v[i] > aib[p].loc ) add ( i, p * 2 + 1 );
}

void print ( int p )
{
  if ( aib[p].pop == 1 )
  {
    print ( p * 2 );
    fout << aib[p].ind << '\n';
    print ( p * 2 + 1 );
  }
}

int main ()
{
  int n;
  fin >> n;

  int i;
  for ( i = 1; i <= n; i++ )
    fin >> v[i];

  aib[1] = { 1, 1, 1, 0 };
  for ( i = 2; i <= n; i++ )
    add ( i, 1 );

  print ( 1 );

  fin.close ();
  fout.close ();

  return 0;
}