Cod sursa(job #1968265)

Utilizator GinguIonutGinguIonut GinguIonut Data 17 aprilie 2017 16:27:09
Problema Nums Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.89 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 1;
    if(len[a]>len[b])
        return 0;
    for(int i=0; i<len[a]; i++)
    {
        if(mat[a][i]<mat[b][i])
            return 1;
        if(mat[a][i]>mat[b][i])
            return 0;
    }

    return 1;
}

bool verif(int mid)
{
    if(newLen[mid]>len[lastNumber])
        return 1;
    if(newLen[mid]<len[lastNumber])
        return 0;
    for(int i=0; i<newLen[mid]; i++)
    {
        if(newMat[mid][i]>mat[lastNumber][i])
            return 1;
        if(newMat[mid][i]<mat[lastNumber][i])
            return 0;
    }

    return 1;
}

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++;
    }
}