Cod sursa(job #904725)

Utilizator danalex97Dan H Alexandru danalex97 Data 4 martie 2013 20:00:03
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.36 kb
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;

ifstream F("nums.in");
ofstream G("nums.out");

const int Nmax = 100010;

typedef struct { int ord,type; string S; } q_type;
typedef struct { int val,pos; } a_type;

a_type A[Nmax<<2];
q_type Q[Nmax];

bool Mark[Nmax];

typedef pair<string,int> Pair;
#define one first
#define two second
#define m_p make_pair

Pair V[Nmax];
int Ord,N,M;
string S;int type;

void Update(int Nod,int St,int Dr,int pos,int val,int now)
{
    if (St == Dr)
    {
        A[Nod].val = 1;
        A[Nod].pos = now;
        return;
    }

    int Mid = (St + Dr) / 2;
    if ( pos <= Mid )
        Update(Nod*2, St, Mid, pos, val, now);
    else
        Update(Nod*2+1, Mid+1, Dr, pos, val, now);

    A[Nod].val = A[Nod*2].val + A[Nod*2+1].val;
}

void Query(int Nod, int St, int Dr, int what, int &pos)
{
    if ( St == Dr )
    {
        pos = A[Nod].pos;
        return;
    }

    int Mid = (St + Dr) / 2;
    if ( what <= A[Nod*2].val )
        Query(Nod*2, St, Mid, what, pos);
    else
        Query(Nod*2+1, Mid+1, Dr, what - A[Nod*2].val, pos);
}

/*
struct cmp
{
    bool operator () ( Pair& A, Pair& B)
    {
        return ( A.one.size() == B.one.size() )
            ? A.one < B.one : A.one.size() < B.one.size();
    }
};
*/

bool cmp(Pair& A, Pair& B)
{
     return ( A.one.size() == B.one.size() )
            ? A.one < B.one : A.one.size() < B.one.size();
}

int main()
{
    F>>N;
    for (int i=1;i<=N;++i)
    {
        S.clear();
        F>>Q[i].type,F>>Q[i].S;
        if ( type == 1 )
            V[++M] = m_p(Q[i].S,i);
    }
    sort(V+1,V+M+1,cmp);

    int now = 1;
    Q[ V[1].two ].ord = now;

    for (int i=2;i<=M;++i)
        if ( V[i].one == V[i-1].one )
            Q[ V[i].two ].ord = now;
        else
            Q[ V[i].two ].ord = ++now;

    for (int i=1;i<=N;++i)
    {
        if ( Q[i].type == 0 )
        {
            int what=0, pos=0;
            string S = Q[i].S;

            for (int i=0;i<S.size();++i)
                what = what*10 + S[i]-'0';
            Query(1, 1, N, what, pos);

            G<<Q[pos].S<<"\n";
            continue;
        }

        if ( Mark[ Q[i].ord ] == 1 )
            continue;
        Mark[ Q[i].ord ] = 1;
        Update(1, 1, N, Q[i].ord, 1, i);
    }
}