Cod sursa(job #1316984)

Utilizator geniucosOncescu Costin geniucos Data 14 ianuarie 2015 13:51:48
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.94 kb
#include<cstdio>
#include<string>
#include<cstring>

using namespace std;

int M, Nr, aib[100009], pos[100009], al_k_lea[100009];

string sir[100009];
char actual[100009];

struct nod
{
    int cod;
    nod *urm[10];
    nod ()
    {
        cod = 0;
        memset (urm, 0, sizeof (urm));
    }
}*R[100009];

void dfs (nod *&R, int deep, int L)
{
    if (R == 0) return ;

    if (deep == L)
    {
        Nr ++;
        al_k_lea[Nr] = R->cod;
        pos[R->cod] = Nr;
        return;
    }

    for (int i=0; i<10; i++)
        if (R->urm[i] != 0)
            dfs (R->urm[i], deep + 1, L);
}

void U (int poz)
{
    while (poz <= M)
    {
        aib[poz] ++;
        poz += (poz - (poz & (poz - 1)));
    }
}

int main()
{
freopen ("nums.in", "r", stdin);
freopen ("nums.out", "w", stdout);

scanf ("%d\n", &M);
for (int i=1; i<=M; i++)
{
    int tip;
    scanf ("%d ", &tip);

    if (tip == 0)
    {
        int x;
        scanf ("%d\n", &x);
        continue;
    }

    gets (actual + 1);
    sir[i] = actual + 1;

    int L = strlen (actual + 1);
    if (R[L] == 0) R[L] = new nod;

    nod *q = R[L];
    for (int j=1; j<=L; j++)
    {
        if (q->urm[actual[j]-'0'] == 0) q->urm[actual[j]-'0'] = new nod;
        q = q->urm[actual[j]-'0'];
    }
    if (q->cod) ;
    else q->cod = i;
}

for (int i=1; i<=100000; i++)
    if (R[i] != 0)
        dfs (R[i], 0, i);

freopen ("nums.in", "r", stdin);
scanf ("%d\n", &M);
for (int i=1; i<=M; i++)
{
    int tip;
    scanf ("%d ", &tip);
    if (tip == 0)
    {
        int x, ras = 0;
        scanf ("%d\n", &x);
        for (int j=16; j>=0; j--)
            if (ras + (1<<j) <= M && aib[ras + (1<<j)] <= x )
                x -= aib[ras + (1<<j)], ras |= 1<<j;
        printf ("%s\n", sir[al_k_lea[ras]].c_str());
    }

    if (pos[i])
    {
        gets (actual + 1);
        U (pos[i]);
    }
}

return 0;
}