Cod sursa(job #2648259)

Utilizator vladth11Vlad Haivas vladth11 Data 9 septembrie 2020 17:58:38
Problema Nums Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.06 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 = 100001;
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;

map <string,int> mp;
map <int,string> coresp;

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]);
    }
}

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++) {
        int x;
        string y;
        cin >> x >> y;
        if(x == 1)
            v[++vf] = y;
    }
    sort(v + 1,v + vf + 1,cmp);
    for(i = 1; i <= n; i++) {
        mp[v[i]] = i;
        coresp[i] = v[i];
    }
    cin.close();
    cin.open("nums.in");
    cin >> n;
    for(i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if(x == 0) {
            int y;
            cin >> y;
            cout << coresp[aib.kth(y)] << "\n";
        } else {
            string s;
            cin >> s;
            if(mm[s]){
                continue;
            }
            mm[s] = 1;
            aib.update(mp[s]);
        }

    }
    return 0;
}