Cod sursa(job #632428)

Utilizator SpiderManSimoiu Robert SpiderMan Data 11 noiembrie 2011 08:12:23
Problema Nums Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.16 kb
# include <algorithm>
# include <fstream>
# include <cstring>
# include <string>
# include <deque>
using namespace std;

const char *FIN = "nums.in", *FOU = "nums.out";
const int MAX = 100005;

deque < int > ind[MAX];
deque < string > sir;
deque < pair <int, string> > T;
string S;
int N, cnt, max_len, poz[MAX], dp[MAX], ai[MAX << 2];

inline bool comp (const int x, const int y) {
    return sir[x].compare (sir[y]) < 0;
}

void update (int nod, int st, int dr, int poz) {
    if (st == dr) {
        ai[nod] = 1;
        return ;
    }
    int mij = (st + dr) >> 1;
    if (poz <= mij) update (nod << 1, st, mij, poz);
    else update (nod << 1 | 1, mij + 1, dr, poz);

    ai[nod] = ai[nod << 1] + ai[nod << 1 | 1];
}

int query (int nod, int st, int dr, int poz) {
    if (st == dr) return dr;
    int mij = (st + dr) >> 1;

    return poz <= ai[nod << 1] ? query (nod << 1, st, mij, poz) : query (nod << 1 | 1, mij + 1, dr, poz - ai[nod << 1]);
}

int main (void) {
    ifstream f (FIN);
    ofstream g (FOU);

    f >> N;
    for (int i = 1, ce, lun; i <= N; ++i) {
        f >> ce >> S, T.push_back (make_pair (ce, S));
        if (ce) {
            max_len = max (max_len, lun = S.size ());
            sir.push_back (S), ind[lun].push_back (sir.end () - sir.begin () - 1);
        }
    }
    for (int i = 0, aux; i <= max_len; ++i)
        if (ind[i].size ()) {
            sort (ind[i].begin (), ind[i].end (), comp);
            dp[poz[aux = ind[i].front ()] = ++cnt] = aux;
            for (deque <int> :: iterator it = ind[i].begin () + 1; it != ind[i].end (); ++it) {
                poz[*it] = sir[aux].compare (sir[*it]) ? ++cnt : cnt;
                aux = dp[cnt] = *it;
            }
        }
    int pp = 0;
    for (deque < pair <int, string> > :: iterator it = T.begin (); it != T.end (); ++it) {
        if (it -> first == 1)
            update (1, 1, cnt, poz[pp++]);
        else {
            int k = 0;
            for (string :: iterator IT = it -> second.begin (); IT != it -> second.end (); ++IT)
                k = k * 10 + *IT - '0';
            g << sir[dp[query (1, 1, cnt, k)]] << "\n";
        }
    }
}