Cod sursa(job #1481435)

Utilizator lflorin29Florin Laiu lflorin29 Data 4 septembrie 2015 15:03:46
Problema Nums Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <bits/stdc++.h>
#include <hash_map>
using namespace std;
using namespace __gnu_cxx;

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

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

struct clascom{
    bool operator() (const string& a, const string& b){
        if (a.size() != b.size() )
            return a.size() < b.size();
        for (int i = 0; i < a.size(); ++i)
           if (a[i] != b[i])
             return a[i] < b[i];
        return 0;
    }
};


void Prepare() {
    sort(v.begin(), v.end(), clascom());
    v.resize( unique(v.begin(), v.end()) - v.begin());
    int index = 0;
    for (const auto& it : v){
        ++ index;
        umap[it] = index;
        umap2[index] = it;
    }
    mlStep = 1;
    for (; mlStep < goQuery; mlStep <<= 1);
}

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

int que (int k){
    int step = mlStep;
    int ret = 0, i = 0;
    for (; step; step >>= 1)
        if (i + step <= v.size() && aib[i + step] <= k ) {
          i += step;
          k -= aib[i];
          ret = i;
        }
    return ret;
}

void Solve() {
    ifstream cin ("nums.in");
    ofstream cout ("nums.out");
    cin >> n;
    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 << umap2[t] << "\n";
        }
    }
}

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