Cod sursa(job #3372710)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 4 noiembrie 2026 19:10:28
Problema Nums Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.32 kb
#include <bits/stdc++.h>

using namespace std;

const int max_size = 1e5 + 20, max_seg = 4e5 + 20, mod = 1e9 + 7;

struct str{
    int len, idx, idxreal;
    string s;

    bool operator < (const str &oth) const
    {
        if (len != oth.len)
        {
            return len < oth.len;
        }
        return s < oth.s;
    }
};

int seg[max_seg], legatura[max_size];
pair <int, string> ord[max_size];

void upd (int l, int r, int pos, int nod)
{
    if (l == r)
    {
        seg[nod] = 1;
        return;
    }
    int m = (l + r) / 2;
    if (pos <= m)
    {
        upd(l, m, pos, 2 * nod);
    }
    else
    {
        upd(m + 1, r, pos, 2 * nod + 1);
    }
    seg[nod] = seg[2 * nod] + seg[2 * nod + 1];
}

int query (int l, int r, int val, int nod)
{
    if (l == r)
    {
        return l;
    }
    int m = (l + r) / 2;
    if (val <= seg[2 * nod])
    {
        return query(l, m, val, 2 * nod);
    }
    else
    {
        return query(m + 1, r, val - seg[2 * nod], 2 * nod + 1);
    }
}

void solve()
{
    int n;
    cin >> n;
    vector <str> aux;
    int ct = 0;
    set <string> s;
    for (int i = 1; i <= n; i++)
    {
        cin >> ord[i - 1].first >> ord[i - 1].second;
        if (ord[i - 1].first == 1 && s.find(ord[i - 1].second) == s.end())
        {
            s.insert(ord[i - 1].second);
            ++ct;
            aux.push_back({(int)ord[i - 1].second.size(), ct, i - 1, ord[i - 1].second});
        }
    }
    sort(aux.begin(), aux.end());
    for (int i = 0; i < aux.size(); i++)
    {
        legatura[aux[i].idxreal] = i + 1;
    }
    for (int i = 0; i < n; i++)
    {
        if (ord[i].first == 1)
        {
            if (legatura[i] == 0)
            {
                continue;
            }
            upd(1, ct, legatura[i], 1);
        }
        else
        {
            int val = atoi(ord[i].second.c_str());
            int pos = query(1, ct, val, 1);
            cout << ord[aux[pos - 1].idxreal].second << "\n";
        }
    }
}

signed main()
{
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#else
    freopen("nums.in", "r", stdin);
    freopen("nums.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long tt;
    //cin >> tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0;
}