Pagini recente » Cod sursa (job #2646184) | Cod sursa (job #2881183) | Cod sursa (job #1413165) | Cod sursa (job #2816326) | Cod sursa (job #445015)
Cod sursa(job #445015)
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
using namespace __gnu_cxx;
#define nmax 100005
#define VIT vector <int> :: iterator
#define pb push_back
#define f first
#define s second
#define mp make_pair
vector <string> S;
vector <pair <int, string> > op;
int idx, M, type, logN, poz;
struct cmp {
bool operator()(const string &a, const string &b) {
if (a.size () == b.size ())
return a.compare (b) < 0;
return a < b;
}
};
int aib [nmax];
string str;
inline void update (int x) {
for (++x; x <= idx; x += x & -x) ++aib [x];
}
inline int query (int x) {
int sum = 0;
for (++x; x >= 1; x -= x & -x) sum += aib [x];
return sum;
}
inline int srch (int x) {
int step, rez = 0, sum = 0;
for (step = 1 << 17; step; step >>= 1)
if ((rez | step) < idx)
if (sum + aib [rez | step] < x) {
sum += aib [rez | step];
rez |= step;
}
return rez;
}
int main () {
freopen ("nums.in", "r", stdin);
freopen ("nums.out", "w", stdout);
int a, i;
scanf ("%d", &M);
for (i = 0; i < M; i++) {
cin >> type >> str;
if (type == 1) ++idx;
S.pb (string (str));
op.pb (mp (type, string (str)));
}
for (logN = 1; logN < idx; logN <<= 1);
sort (S.begin (), S.end (), cmp ());
S.resize (unique (S.begin (), S.end ()) - S.begin ());
for (i = 0; i < M; i++)
{
type = op [i].f;
if (type) {
poz = lower_bound (S.begin (), S.end (), op [i].s, cmp ()) - S.begin ();
if (query (poz) - query (poz - 1) == 0)
update (poz);
}else {
sscanf (op [i].s.c_str (), "%d", &a);
poz = srch (a);
printf ("%s\n", S [poz].c_str ());
}
}
return 0;
}