Cod sursa(job #1536184)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 25 noiembrie 2015 20:14:59
Problema Nums Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 1e5;

vector <string> S;
bitset <kMaxN> seen;

class fenwickTree {
private:
    int T[kMaxN + 1];
    int N;
public:
    fenwickTree() {
    };

    void setSize(int n) {
        N = n;
    }

    void update(int indx, int key) {
        while (indx <= N) {
            T[indx] += key;
            indx += (indx & -indx);
        }
    }

    int query(int indx) {
        int s = 0;
        while (indx > 0) {
            s += T[indx];
            indx -= (indx & -indx);
        }
        return s;
    }

    int binSearch(int q) {
        int pos = 0;
        for (int step = 1 << (31 - __builtin_clz(N)); step; step >>= 1) {
            if ((pos + step <= N) && (T[pos + step] < q)) {
                q -= T[pos + step];
                pos += step;
            }
        }
        return pos + 1;
    }
};

fenwickTree FEN;

bool cmp(const string &A, const string &B) {
    if (A.size() == B.size()) {
        return A < B;
    }
    return A.size() < B.size();
}

int main(void) {
    ifstream in("nums.in");
    ofstream out("nums.out");
    in.tie(0);
    ios_base::sync_with_stdio(0);
    int N, K, opType;
    string A;

    in >> N;
    for (int i = 0; i < N; i++) {
        in >> opType;
        if (opType == 1) {
            in >> A;
            S.emplace_back(A);
        } else {
            in >> K;
        }
    }

    sort(S.begin(), S.end(), cmp);
    S.erase(unique(S.begin(), S.end()), S.end());
    FEN.setSize(S.size());

    in.seekg(0);
    in >> N;
    for (int i = 0; i < N; i++) {
        in >> opType;
        if (opType == 1) {
            in >> A;
            K = lower_bound(S.begin(), S.end(), A, cmp) - S.begin();
            if (!seen[K]) {
                seen[K] = 1;
                FEN.update(1 + K, 1);
            }
        } else {
            in >> K;
            out << S[FEN.binSearch(K) - 1] << '\n';
        }
    }
    in.close();
    out.close();
    return 0;
}