Cod sursa(job #2648270)

Utilizator vladth11Vlad Haivas vladth11 Data 9 septembrie 2020 18:12:01
Problema Nums Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "

using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, pii> muchie;

const ll NMAX = 100005;
const ll INF = (1 << 22) - 1;
const ll MOD = 1000000007;
const ll BLOCK = 101;


class FenwickTree{
    int aib[NMAX];
public:
    void update(int x){
        for(int i = x;i < NMAX;i+=i&(-i)){
            aib[i] += 1;
        }
    }
    int query(int x){
        int s = 0;
        for(int i = x;i > 0;i-=i&(-i))
            s += aib[i];
        return s;
    }
    int kth(int k){
        int r = 0, pas = 1 << 16;
        while(pas){
            if(query(r + pas) <= k)
                r += pas;
            pas /= 2;
        }
        return r;
    }
}aib;

unordered_map <string,int> mp;
string coresp[NMAX];

unordered_map <string,bool> mm;
string v[NMAX];
int vf = 0;

bool cmp(string a, string b) {
    if(b.size() > a.size()) {
        return 1;
    }
    if(a.size() > b.size()) {
        return 0;
    }
    for(int i = 0; i < a.size(); i++) {
        if(a[i] != b[i])
            return (a[i] < b[i]);
    }
}

struct query{
    int x;
    string y;
    int a;

}qq[NMAX];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ifstream cin("nums.in");
    ofstream cout("nums.out");
    int n,i;
    cin >> n;
    for(i = 1; i <= n; i++) {
        cin >> qq[i].x;
        if(qq[i].x == 1){
            cin >> qq[i].y;
            v[++vf] = qq[i].y;
        }else{
            cin >> qq[i].a;
        }
    }
    sort(v + 1,v + vf + 1,cmp);
    for(i = 1; i <= n; i++) {
        if(v[i] != v[i - 1]){
            mp[v[i]] = i;
            coresp[i] = v[i];
        }
    }
    for(i = 1; i <= n; i++) {
        int x = qq[i].x;
        if(x == 0) {
            int y;
            y = qq[i].a;
            cout << coresp[aib.kth(y)] << "\n";
        } else {
            string s;
            s = qq[i].y;
            if(mm[s]){
                continue;
            }
            mm[s] = 1;
            aib.update(mp[s]);
        }

    }
    return 0;
}