Cod sursa(job #1679631)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 8 aprilie 2016 09:32:20
Problema Nums Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <unordered_set>
#define tractoree pair< string,int >
#define F first
#define S second

using namespace std;

ifstream f("nums.in");
ofstream g("nums.out");

const int N = (1e5)+5;

unordered_set<string> Set;
int i, t[N], n, ord[N], aib[N];
vector< tractoree > a;
string A;

inline int ub(int x)
{
    return (x&(-x));
}

bool cmp(tractoree a,tractoree b)
{
     if(a.F==b.F) return (a.S<b.S);
     if(a.F.size()==b.F.size()) return (a.F<b.F);
     return (a.F.size()<b.F.size());
}

void update(int x)
{
     for(; x<=n; x+=ub(x)) ++aib[x];
}

int query(int x)
{
     int ans=0;
     for(; x; x-=ub(x)) ans+=aib[x];
     return ans;
}

string bs(int p)
{
    int st=1,dr=n,mij;
    while(st<=dr)
    {
         mij=((st+dr)>>1);
         if(query(mij)<p) st=mij+1;
         else dr=mij-1;
    }
    return a[dr].F;
}

int main()
{
    f>>n;
    for(i=1; i<=n; ++i)
    {
         f>>t[i];
         if(t[i])
         {
             f>>A;
             t[i]=0;
             if(Set.find(A)==Set.end())
             {
                 a.push_back({A,i});
                 Set.insert(A);
             }
         }
         else f>>t[i];
    }

    sort(a.begin(), a.end(), cmp);

    for(i=0; i<a.size(); ++i)
        ord[a[i].S]=i+1;

    for(i=1; i<=n; ++i)
    if(t[i])
        g<<bs(t[i])<<'\n';
    else
        if(ord[i]) update(ord[i]);

    return 0;
}