Cod sursa(job #1135820)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 8 martie 2014 14:10:15
Problema Nums Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <map>
#include <cstring>

#define DN 100003
using namespace std;


ifstream f("nums.in");
ofstream g("nums.out");

struct operatii{

    int op,k;
    string s;
};

int sz,arb[4*DN],n,poz,k,S;
string v[DN];
map < string , int > mp;
operatii M[DN];

bool mic(string a, string b) /// a<b ?
{
    if(a.size() == b.size()){

        return a < b;
    }
    return a.size() < b.size();
}

void update(int nod,int left,int right){

    if(left == right){

        ++arb[nod];
        return;
    }
    int mij = ( left + right)/2;
    if( poz <= mij) update( 2*nod , left , mij);
        else update(2*nod+1,mij+1,right);

    arb[nod] = arb[2*nod] + arb[2*nod+1];
}

void fnd(int nod,int left,int right){

    if(left == right){

        g<<v[left]<<"\n";
        return;
    }
    int mij = ( left + right)/2;
    if( S + arb[2*nod] >= k){

        fnd( 2*nod , left , mij);
    }
    else{
        S+=arb[2*nod];
        fnd(2*nod+1,mij+1,right);
    }
    arb[nod] = arb[2*nod] + arb[2*nod+1];
}

int caut(string x){

    int st=1,dr=n,mij;
    for( ; st<=dr ; ){

        mij = (st+dr)/2;
        if( v[ mij ] == x)
            return mij;
        if( mic(x,v[mij]) )
            dr = mij - 1;
            else
                st = mij + 1;
    }
}
int main()
{
    int m;
    f>>m;
    for(int i=1;i<=m;++i){

        f>>M[i].op; f.get();

        if(M[i].op){
            getline(f,v[++sz]);
            M[i].s = v[sz];
        }
        else
            f>>M[i].k;
    }

    sort(v+1,v+sz+1,mic);
    n = sz;

    for(int i=1;i<=m;++i){

        if(M[i].op){

            poz = caut(M[i].s);
            update(1,1,n);
        }else{

            k = M[i].k;
            S=0;
            fnd(1,1,n);
        }


    }


    return 0;
}