Cod sursa(job #2605198)

Utilizator alextodoranTodoran Alexandru Raul alextodoran Data 24 aprilie 2020 16:45:54
Problema Nums Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.48 kb
#include <bits/stdc++.h>

using namespace std;

FILE* fin = fopen("nums.in", "r");
ofstream fout ("nums.out");

const int N_MAX = 100002;
const int D_MAX = 100002;

const int BUFFER_SIZE = 1000000;

char buffer[BUFFER_SIZE];
int bpos = BUFFER_SIZE - 1;

char read_char ()
{
    bpos++;
    if(bpos == BUFFER_SIZE)
    {
        fread(buffer, sizeof(char), BUFFER_SIZE, fin);
        bpos = 0;
    }
    return buffer[bpos];
}

bool isDigit[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int read_int ()
{
    char c;
    while(!isDigit[c = read_char()] && c != '-');
    int ans = 0;
    bool neg = false;
    if(c == '-')
    {
        neg = true;
        c = read_char();
    }
    while(isDigit[c])
    {
        ans = ans * 10 + c - '0';
        c = read_char();
    }
    if(neg == true)
        ans *= -1;
    return ans;
}

string read_string ()
{
    string ans = "";
    char c;
    while(isDigit[c = read_char()])
        ans += c;
    return ans;
}

int n;

struct Operation
{
    bool type;
    int k;
    string str;
};

Operation op[N_MAX];

vector <int> vd[D_MAX];

bool cmp (const int &a, const int &b)
{
    for(int i = 0; i < op[a].str.size(); i++)
        if(op[a].str[i] != op[b].str[i])
            return op[a].str[i] < op[b].str[i];
    return false;
}

vector <int> vsort;

int vn[N_MAX];

int BIT[N_MAX];

void update (int pos)
{
    for(int i = pos; i <= (int)vsort.size(); i += i & -i)
        BIT[i]++;
}

int query (int k)
{
    int ans = 0;
    for(int step = 16; step >= 0; step--)
        if(ans + (1 << step) <= (int)vsort.size() && BIT[ans + (1 << step)] < k)
        {
            ans += (1 << step);
            k -= BIT[ans];
        }
    return ans + 1;
}

int main()
{
    n = read_int();
    for(int i = 1; i <= n; i++)
    {
        op[i].type = read_int();
        if(op[i].type == 0)
            op[i].k = read_int();
        else
        {
            op[i].str = read_string();
            vd[(int)op[i].str.size()].push_back(i);
        }
    }
    for(int i = 1; i < D_MAX; i++)
        if(vd[i].size() > 0)
        {
            sort(vd[i].begin(), vd[i].end(), cmp);
            vsort.push_back(vd[i][0]);
            vn[vd[i][0]] = vsort.size();
            for(int j = 1; j < vd[i].size(); j++)
                if(op[vd[i][j]].str != op[vd[i][j - 1]].str)
                {
                    vsort.push_back(vd[i][j]);
                    vn[vd[i][j]] = vsort.size();
                }
        }
    for(int i = 1; i <= n; i++)
        if(op[i].type == 1)
        {
            if(vn[i] > 0)
                update(vn[i]);
        }
        else
            fout << op[vsort[query(op[i].k) - 1]].str << "\n";
    return 0;
}