Cod sursa(job #1481441)

Utilizator lflorin29Florin Laiu lflorin29 Data 4 septembrie 2015 15:22:49
Problema Nums Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <bits/stdc++.h>
using namespace std;

const int goQuery = 100003;
const int godigits = 100003;
int n;
int step;
int aib[goQuery];
string str[goQuery];
vector <string> v;
unordered_map <string, int> umap;
unordered_map <string, bool> umapbool;

void Read() {
    ifstream cin ("nums.in");
    cin >> n;
    for (int tr = 0; tr < n; ++tr){
        int type, x;
        string s;
        cin >> type;
        if (!type){
            cin >> x; continue;
        }
        cin >> s;
        v.push_back(s);
    }
    cin.close();
}

struct clascom{
    bool operator() (const string& a, const string& b){
        if (a.size() != b.size() )
            return a.size() < b.size();
        return a < b;
    }
};


void Prepare() {
    sort(v.begin(), v.end(), clascom());
    int index = 0;
    for (const auto& it : v){
        ++ index;
        umap[it] = index;
        str[index] = it;
    }
}

void upAib (int x) {
    for (; x <= n; x += x & -x)
        ++aib[x];
}

int que (int k){
    int i = 0;
    for (int mlc = step; mlc; mlc >>= 1)

       if (i + mlc < n && aib[i + mlc] < k){
          i += mlc;
          k -= aib[i];
       }
    return i + 1;
}

void Solve() {

    ifstream cin ("nums.in");
    ofstream cout ("nums.out");
    cin >> n;
    step = 1;
    for (; step << 1 <= n; step <<= 1);

    for (int tr = 0; tr < n; ++tr){
        int type;
        cin >> type;
        if (type){
            string s; cin >> s;
            if (umapbool[s])
                continue;
            umapbool[s] = 1;
            upAib(umap[s]);
        }
        else {
                int k; cin >> k;
                int t = que(k);
                cout << str[t] << "\n";
        }
    }
}

int main(){
    Read();
    Prepare();
    Solve();
}