Cod sursa(job #1835478)

Utilizator ade_tomiEnache Adelina ade_tomi Data 26 decembrie 2016 22:07:53
Problema Nums Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.11 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>
#include <cstring>

using namespace std;
const int NMAX =  100004;
int n, query[NMAX], aib[NMAX];
string s[NMAX], s2[NMAX];


struct cmp {
    bool operator()(const string& a, const string& b) const {
        if (a.size() != b.size())
            return a.size() < b.size();
        return a < b;
    }
};
typedef map <string, int, cmp> myMap;
myMap poz;
myMap :: iterator it;
int lsb (int x)
{

    return x&(-x);
}
void update(int poz, int val)
{

    while (poz <= n)
    {
        aib[poz] += val;
        poz += lsb(poz);
    }
}
string sir;
int main ()
{
    ifstream cin ("nums.in");
    ofstream cout ("nums.out");
    cin >> n;
    int nr = 0;
    int pp = 0;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if (x == 1)
        {

            cin >> sir;
            //cout << poz[s2[pp]] << " ";
            if (poz[sir] == 0)
            {
                nr++;
                pp++;
                s2[pp] = sir;
                poz[s2[pp]] = 1;
                //cout << poz[s2[pp]] << "\n";
            }
           // s2[i] = sir;

        }
        else
        {
            pp++;
            cin >> query[pp];
        }


    }
    //sort (s + 1, s + 1 + nr, cmp);
    int shp = 0;
    for (it = poz.begin(); it != poz.end(); it++)
    {
        shp++;
        s[shp] = it->first;
        it->second = shp;
        //cout << it->first << " " << it->second << "\n";
    }

    for (int i = 1; i <= pp; i++)
    {

        if (query[i] == 0)
        {

            update(poz[s2[i]], 1);
            //cout << poz[s2[i]] << " " ;
        }
        else
        {
            int ans = 0;

            for (int step = 1 << 16; step > 0; step /= 2)
            {

                if (ans + step < n && aib[ans + step] < query[i])
                {

                    ans += step;
                    query[i] -= aib[ans];

                }
            }
            cout << s[ans + 1] << "\n";
        }
    }
    return 0;
}