Cod sursa(job #1536181)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 25 noiembrie 2015 20:13:48
Problema Nums Scor 0
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;

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

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

    void update(int indx) {
        while (indx < N) {
            ++ T[indx];
            indx |= (indx + 1);
        }
    }

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

    int findPunctual(int x) {
        int s = T[x];
        int parent = (x & (x + 1)) - 1;
        x--;
        while (x != parent) {
            s -= T[x];
            x = (x & (x + 1)) - 1;
        }
        return s;
    }
};

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 (!FEN.findPunctual(K)) {
                FEN.update(K);
            }
        } else {
            in >> K;
            out << S[FEN.binSearch(K)] << '\n';
        }
    }
    in.close();
    out.close();
    return 0;
}