Pagini recente » Cod sursa (job #2212813) | Cod sursa (job #1786620) | Statistici Pop Iannis (iannis) | Cod sursa (job #1528760) | Cod sursa (job #1514713)
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#define DIM 100005
#define infile "nums.in"
#define outfile "nums.out"
using namespace std;
vector<string> vec;
map<string, int> pos;
map<string, bool> vis;
int aib[DIM];
vector<int> ops, auxQ;
vector<string> auxS;
int pow2, n;
inline bool comp(const string &a, const string &b) {
if (a.size() == b.size())
return a < b;
else
return a.size() < b.size();
}
inline unsigned int lsb(const int &x) {
return (x & (-x));
}
inline void updateAIB(const int &pos) {
for (int i = pos; i <= n; i += lsb(i))
++aib[i];
}
inline int queryAIB(int cnt) {
int ans = 0;
for (int pow = pow2; pow; pow >>= 1) {
if (ans + pow < n && aib[ans + pow] < cnt) {
cnt -= aib[ans + pow];
ans += pow;
}
}
return ans + 1;
}
int main() {
ifstream fin(infile);
ofstream fout(outfile);
fin >> n;
string x;
for (int i = 1; i <= n; ++i) {
int op;
fin >> op;
if (op == 1) {
fin >> x;
vec.push_back(x);
auxS.push_back(x);
ops.push_back(1);
}
else {
fin >> op;
auxQ.push_back(op);
ops.push_back(2);
}
}
sort(vec.begin(), vec.end(), comp);
for (unsigned int i = 0; i < vec.size(); ++i) {
pos[vec[i]] = i + 1;
}
pow2 = 1;
while (2 * pow2 <= n)
pow2 <<= 1;
int i1 = 0, i2 = 0;
for (int i = 0; i < n; ++i) {
int op;
op = ops[i];
if (op == 1) {
x = auxS[i1++];
if (vis[x])
continue;
vis[x] = true;
updateAIB(pos[x]);
}
else {
int x;
x = auxQ[i2++];
fout << vec[queryAIB(x) - 1] << '\n';
}
}
return 0;
}
//Trust me, I'm the Doctor!