Cod sursa(job #325549)

Utilizator bogdan2412Bogdan-Cristian Tataroiu bogdan2412 Data 21 iunie 2009 01:06:07
Problema Nums Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

#define MAXN 100005
#define MAXL 100005

int N;

char str[MAXL];
vector<int> aib;
vector<string> strs; 
vector<pair<int, string> > q;

inline void aib_add(int poz, int val) {
    for (poz++; poz < (int)aib.size(); poz += ((poz ^ (poz - 1)) & poz)) {
        aib[poz] += val;
    }
}

inline int aib_query(int poz) {
    int rez = 0;
    for (poz++; poz; poz &= (poz - 1)) {
        rez += aib[poz];
    }
    return rez;
}

inline int aib_search(int S) {
    int rez = 0, step = 1 << 17, sum = 0;
    for (; step; step >>= 1) {
        if ((rez | step) < (int)aib.size()) {
            if (sum + aib[rez | step] < S) {
                sum += aib[rez | step];
                rez |= step;
            }
        }
    }
    return rez/* + 1 - 1 */;
}

inline int cmp(const string &a, const string &b) {
    if (a.length() < b.length()) {
        return 1;
    }
    if (a.length() > b.length()) {
        return 0;
    }
    return a < b;
}

int main() {
    freopen("nums.in", "rt", stdin);
    freopen("nums.out", "wt", stdout);

    scanf("%d", &N);
    aib.clear();
    aib.resize(MAXL + 1);
    strs.clear();
    for (int i = 0; i < N; i++) {
        int type;
        scanf("%d %s", &type, str);
        strs.push_back(string(str));
        q.push_back(make_pair(type, string(str)));
    }
    sort(strs.begin(), strs.end(), cmp);
    strs.resize(unique(strs.begin(), strs.end()) - strs.begin());

    for (int i = 0; i < N; i++) {
        int type = q[i].first;
        if (type == 1) {
            int poz = lower_bound(strs.begin(), strs.end(), q[i].second, cmp) - strs.begin();
            if (aib_query(poz) - aib_query(poz - 1) == 0) {
                aib_add(poz, 1);
            }
        } else {
            int num;
            sscanf(q[i].second.c_str(), "%d", &num);
            int poz = aib_search(num);
            printf("%s\n", strs[poz].c_str());
        }
    }

    return 0;
}