Cod sursa(job #631684)

Utilizator SpiderManSimoiu Robert SpiderMan Data 9 noiembrie 2011 16:27:34
Problema Nums Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.98 kb
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <vector>
using namespace std;

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

vector < string > V;
vector < pair <int, string> > T;
int N, ai[MAX], viz[MAX];
char ch[MAX];

bool comp (const string &x, const string &y) {
    if (x.length () == y.length ())
        return x.compare (y) < 0;
    return x.length () < y.length ();
}

inline void update (int x) {
    for (; x <= N; x += x & -x)
        ai[x] += 1;
}

inline int query (int x) {
    int sol;
    for (sol = 0; x; x -= x & -x)
        sol += ai[x];
    return sol;
}

int main (void) {
    freopen (FIN, "r", stdin);
    freopen (FOU, "w", stdout);

    scanf ("%d\n", &N);
    for (int i = 1, len; i <= N; ++i) {
        fgets (ch, MAX, stdin);
        if (ch[(len = strlen (ch)) - 1] == '\n') ch[len - 1] = 0;
        string aux = string (ch + 2);
        if (ch[0] == '1') {
            V.push_back (aux);
            T.push_back (make_pair (1, aux));
        } else {
            T.push_back (make_pair (2, aux));
        }
    }
    sort (V.begin (), V.end (), comp);
    V.resize (unique (V.begin (), V.end ()) - V.begin ());
    for (vector < pair <int, string> > :: iterator it = T.begin (); it != T.end (); ++it) {
        if (it -> first == 1) {
            int getpoz = lower_bound (V.begin (), V.end (), it -> second, comp) - V.begin () + 1;
            if (viz[getpoz] == 0) {
                viz[getpoz] = 1;
                update (getpoz);
            }
        } else {
            int poz = 0;
            for (string :: iterator IT = it -> second.begin (); IT != it -> second.end (); ++IT)
                poz = poz * 10 + *IT - '0';
            int cnt, i;
            for (i = 0, cnt = 1 << 17; cnt; cnt >>= 1)
                if (i + cnt <= N && query (i + cnt) < poz)
                    i += cnt;
            printf ("%s\n", V[i].c_str ());
        }
    }
}