Cod sursa(job #1968274)

Utilizator GinguIonutGinguIonut GinguIonut Data 17 aprilie 2017 16:33:03
Problema Nums Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.52 kb
#include <fstream>
#include <iostream>
#include <cstdio>
#include <vector>
#include <string.h>
#include <algorithm>
#include <queue>

#define nMax 100001
#define mMax 100001

using namespace std;

ifstream fin("nums.in");
ofstream fout("nums.out");

int n, nrEl, newNr, m, nrQuery, lastNumber=1;
string mat[nMax], newMat[nMax], sir;
int len[nMax], arb[4*nMax], poz[nMax], op[nMax], k[nMax], newLen[nMax];

bool cmp(const int &a, const int &b)
{
    if(len[a]==len[b])
        return mat[a]<mat[b];
    return len[a]<len[b];
}

bool verif(int mid)
{
    if(newLen[mid]==len[lastNumber])
        return newMat[mid]>=mat[lastNumber];
    return newLen[mid]>len[lastNumber];
}

void upDate(int node, int st, int dr, int poz)
{
    if(st==dr)
    {
        arb[node]=1;
        return;
    }

    int mid=st+(dr-st)/2;
    if(poz<=mid)
        upDate(2*node, st, mid, poz);
    else
        upDate(2*node+1, mid+1, dr, poz);
    arb[node]=arb[2*node]+arb[2*node+1];
}

void query(int node, int st, int dr, int poz)
{
    if(st==dr)
    {
        fout<<newMat[st]<<'\n';
        return;
    }

    int mid=st+(dr-st)/2;
    if(arb[2*node]>=poz)
        query(2*node, st, mid, poz);
    else
        query(2*node+1, mid+1, dr, poz-arb[2*node]);
}

int main()
{
    fin>>n;
    for(int i=1; i<=n; i++)
    {
        fin>>op[i];
        if(op[i]==0)
        {
            fin>>k[++nrQuery];
            continue;
        }
        fin>>mat[++nrEl];
        for(int j=0; mat[nrEl][j]>='0' && mat[nrEl][j]<='9'; j++)
            len[nrEl]++;
    }

    for(int i=1; i<=nrEl; i++)
        poz[i]=i;
    sort(poz+1, poz+nrEl+1, cmp);
    for(int i=1; i<=nrEl; i++)
        newMat[i]=mat[poz[i]];
    for(int i=1; i<=nrEl; i++)
        if(newMat[i]!=newMat[i-1])
            newMat[++newNr]=newMat[i];
    for(int i=1; i<=newNr; i++)
    {
        newLen[i]=0;
        for(int j=0; newMat[i][j]>='0' && newMat[i][j]<='9'; j++)
            newLen[i]++;
    }

    int lastQuery=1;
    for(int i=1; i<=n; i++)
    {
        if(op[i]==0)
        {

            query(1, 1, newNr, k[lastQuery]);
            lastQuery++;
            continue;
        }

        int st=1, dr=newNr, Sol=0;
        while(st<=dr)
        {
            int mid=st+(dr-st)/2;
            if(verif(mid))
            {
                Sol=mid;
                dr=mid-1;
            }
            else
                st=mid+1;
        }
        upDate(1, 1, newNr, Sol);
        lastNumber++;
    }
}