Cod sursa(job #1711155)

Utilizator Athena99Anghel Anca Athena99 Data 30 mai 2016 18:40:19
Problema Nums Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <algorithm>
#include <fstream>
#include <string>

using namespace std;

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

const int nmax= 100000;

bool u[nmax+1];
int q[nmax+1], aib[nmax+1];

string s[nmax+1], s2[nmax+1], v[nmax+1], aux;

bool cmp( string x, string y ) {
    if ( (int)x.size()!=(int)y.size() ) {
        return (int)x.size()<(int)y.size();
    }
    return x<=y;
}

void aib_update( int x, int n ) {
    for ( ; x<=n; x= x*2-(x&(x-1)) ) {
        ++aib[x];
    }
}

int main(  ) {
    int n, nr= 0, nr2= 1;
    fin>>n;
    for ( int i= 1, x; i<=n; ++i ) {
        fin>>x;
        if ( x==0 ) {
            fin>>q[i];
        } else {
            fin>>s2[i];
            s[++nr]= s2[i];
        }
    }

    sort( s+1, s+nr+1, cmp ) ;
    v[1]= s[1];
    for ( int i= 2; i<=nr; ++i ) {
        if ( s[i]!=s[i-1] ) {
            ++nr2;
            v[nr2]= s[i];
        }
    }

    for ( int i= 1; i<=n; ++i ) {
        int pos= 0;
        if ( q[i]!=0 ) {
            for ( int step= 1<<16, j= 0; step>0; step/= 2 ) {
                if ( pos+step<=nr2 && j+aib[pos+step]<q[i] ) {
                    j+= aib[pos+step];
                    pos+= step;
                }
            }

            fout<<v[pos+1]<<"\n";
        } else {
            for ( int step= 1<<16; step>0; step/= 2 ) {
                if ( pos+step<=nr2 && cmp(v[pos+step], s2[i])==1 ) {
                    pos+= step;
                }
            }

            if ( u[pos]==0 ) {
                u[pos]= 1;
                aib_update(pos, nr2);
            }
        }
    }

    return 0;
}