Cod sursa(job #2749602)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 7 mai 2021 13:20:21
Problema Nums Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 100005;
int n, aib[nmax];
vector <string> v;
map <string, int> h;
map <int, string> h2;

bool cmp(string a, string b){
    if (a.size() < b.size()) return true;
    if (a.size() > b.size()) return false;
    return a < b;
}

struct Query{
    int p;
    string x;
    int xx;
}q[nmax];

void Update(int pos, int val){
    while (pos <= n){
        aib[pos] += val;
        pos += (pos & (-pos));
    }
}

int Query(int pos){
    int sum = 0;
    while (pos > 0){
        sum += aib[pos];
        pos -= (pos & (-pos));
    }
    return sum;
}

int main(){
    fin >> n;
    for (int i = 1; i <= n; ++i){
        int p;
        string x;
        int xx;
        fin >> p;
        if (p == 1){
            fin >> x;
            v.push_back(x);
            q[i] = {p, x, 0};
        }
        else{
            fin >> xx;
            q[i] = {p, " ", xx};
        }
    }
    sort(v.begin(), v.end(), cmp);
    for (int i = 0; i < (int)v.size(); ++i){
        h[v[i]] = i + 1;
        h2[i + 1] = v[i];
    }
    for (int i = 1; i <= n; ++i){
        if (q[i].p == 1){
            int x = h[q[i].x];
            if (Query(x) - Query(x - 1) == 0){
                Update(x, 1);
            }
        }
        else{
            int x = q[i].xx;
            int st = 1, dr = n, ans;
            while (st <= dr){
                int mid = (st + dr) / 2;
                if (Query(mid) >= x){
                    ans = mid;
                    dr = mid - 1;
                }
                else{
                    st = mid + 1;
                }
            }
            fout << h2[ans] << "\n";
        }
    }
    return 0;
}