Cod sursa(job #2605216)

Utilizator alextodoranTodoran Alexandru Raul alextodoran Data 24 aprilie 2020 16:58:56
Problema Nums Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.25 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;
    while(isDigit[c])
    {
        ans = ans * 10 + c - '0';
        c = read_char();
    }
    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;

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

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.push_back(i);
        }
    }
    sort(vd.begin(), vd.end(), cmp);
    vsort.push_back(vd[0]);
    vn[vd[0]] = vsort.size();
    for(int j = 1; j < (int)vd.size(); j++)
        if(op[vd[j]].str != op[vd[j - 1]].str)
        {
            vsort.push_back(vd[j]);
            vn[vd[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;
}