Cod sursa(job #849829)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 7 ianuarie 2013 18:19:57
Problema Nums Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;

const int MaxN = 100005;

struct Operation {
    int T, K, nValue;
    string Value;

    Operation() {}
};

Operation Q[MaxN];
int N, M, Index[MaxN], BIT[MaxN];
bool Used[MaxN];

struct Compare {
    bool operator ()(const int &a, const int &b) {
        if (Q[a].Value.length() == Q[b].Value.length())
            return Q[a].Value < Q[b].Value;
        return Q[a].Value.length() < Q[b].Value.length();
    }
};

void Normalize() {
    sort(Index + 1, Index + N + 1, Compare());
    for (int i = 1; i <= N; ++i) {
        if (i > 1 && Q[Index[i]].Value == Q[Index[i - 1]].Value)
            Q[Index[i]].nValue = Q[Index[i - 1]].nValue;
        else
            Q[Index[i]].nValue = i;
    }
}

inline int LSB(const int X) {
    return X & (-X);
}

inline void Update(int Position, const int Value) {
    for (; Position <= N; Position += LSB(Position))
        BIT[Position] += Value;
}

inline int KthElement(int K) {
    int Step, Position = 0;
    for (Step = 1; (Step << 1) <= N; Step <<= 1);
    for (; Step > 0; Step >>= 1)
        if (Position + Step <= N && BIT[Position + Step] < K)
            K -= BIT[Position += Step];
    return Position + 1;
}

void Solve() {
    Normalize();
    ofstream fout("nums.out");
    for (int i = 1; i <= M; ++i) {
        if (Q[i].T == 0)
            fout << Q[Index[KthElement(Q[i].K)]].Value << "\n";
        else if (!Used[Q[i].nValue])
            Update(Q[i].nValue, 1), Used[Q[i].nValue] = true;
    }
    fout.close();
}

void Read() {
    ifstream fin("nums.in");
    fin >> M;
    for (int i = 1; i <= M; ++i) {
        fin >> Q[i].T;
        if (Q[i].T == 0) {
            fin >> Q[i].K;
        } else {
            fin >> Q[i].Value;
            Index[++N] = i;
        }
    }
    fin.close();
}

int main() {
    Read();
    Solve();
    return 0;
}