Pagini recente » Istoria paginii runda/igorj_7/clasament | Cod sursa (job #2527954) | Cod sursa (job #1678880)
#include <fstream>
#include <algorithm>
#include <cstring>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int LMAX = 100005;
int aib[LMAX];
#define lsb(x) ((x) & (-(x)))
void update(int pos) {
for (; pos < LMAX; pos += lsb(pos))
++ aib[pos];
}
pair <int, int> binary_search(int k) {
-- k;
int pos = 0;
for (int i = 16; i >= 0; -- i)
if (pos + (1 << i) < LMAX && aib[pos + (1 << i)] <= k) {
pos += (1 << i);
k -= aib[pos];
}
return make_pair(k + 1, pos + 1);
}
struct trie {
int sz;
char lit;
vector <trie*> alf;
trie* get_son(char _lit) {
for (auto it: alf)
if (it -> lit == _lit)
return it;
return NULL;
}
trie* add_son(char _lit) {
int pos = alf.size();
alf.push_back(new trie(0, _lit));
while (pos && alf[pos - 1] -> lit > alf[pos] -> lit) {
swap(alf[pos - 1], alf[pos]);
-- pos;
}
return alf[pos];
}
trie(int _sz = 0, char _lit = 0): sz(_sz), lit(_lit) {}
} *roots[LMAX];
bool exists(const string &str) {
if (roots[str.size()] == NULL)
return false;
trie *node = roots[str.size()];
for (auto it: str) {
node = node -> get_son(it);
if (node == NULL)
return false;
}
return true;
}
void insert(const string &str) {
if (roots[str.size()] == NULL)
roots[str.size()] = new trie;
trie *node = roots[str.size()];
if (exists(str))
return ;
update(str.size());
for (auto it: str) {
++ node -> sz;
trie *aux = node -> get_son(it);
if (aux == NULL)
node = node -> add_son(it);
else
node = aux;
}
//Finalul
++ node -> sz;
}
string get_kth(int k) {
string ans;
pair <int, int> aux = binary_search(k);
k = aux.first;
trie *node = roots[aux.second];
while (1) {
bool out = true;
for (auto it: node -> alf)
if (it -> sz < k)
k -= it -> sz;
else {
ans += it -> lit;
node = it;
out = false;
break;
}
if (out == true)
break;
}
return ans;
}
int main()
{
ifstream cin("nums.in");
ofstream cout("nums.out");
int n = 0;
cin >> n;
bool type;
string str;
int k;
while (n --) {
cin >> type;
if (!type) {
cin >> k;
cout << get_kth(k) << '\n';
}
else {
cin >> str;
insert(str);
}
}
cin.close();
cout.close();
return 0;
}