Cod sursa(job #1317448)

Utilizator Tester100Tester Tester100 Data 14 ianuarie 2015 21:50:17
Problema Nums Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <iostream>
#include <set>
#include <string>
#include <algorithm>

using namespace std;
const int VALUE = 33000;
const int NR = 700;
const int NMAX = 100002;
set < string >S[NMAX];
int cnt[VALUE][10];
int aib[NMAX];
inline void Update(int pos)
{
    for(;pos<NMAX;pos+=pos&-pos)
        aib[pos]++;
}
inline int Query(int pos)
{
    int ret = 0;
    for(;pos>0;pos-=pos&-pos)
        ret += aib[pos];
    return ret;
}
int main()
{
    freopen("nums.in","r",stdin);
    freopen("nums.out","w",stdout);
    cin.sync_with_stdio(false);
    string s;
    int pos, n;char ch;
    cin >> n;
    while(n--)
    {
        cin >> ch;
        if(ch=='1'){
            cin >> s;
            int L = s.length();
            Update(L);
            if(S[L].find(s)!=S[L].end())
                continue;
            S[L].insert(s);
        }
        else{
            cin >> pos;
            int L = NMAX;
            int Left = 1;
            int Right = L-1,mid;
            while(Left<=Right)
            {
                mid = (Left+Right)/2;
                if(Query(mid) >= pos)
                {
                    L = mid;
                    Right=mid-1;
                }
                else
                    Left =  mid+1;
            }
            pos -= Query(L-1);
            //if(L >= VALUE || pos <= NR)
            {
                auto x = S[L].begin();
                for(int i=1;i<pos;++i,x++);
                cout<<*x<<"\n";
            }
        }
    }
    return 0;
}