Pagini recente » Cod sursa (job #791452) | Cod sursa (job #1450745) | Cod sursa (job #1299448) | Cod sursa (job #1554917) | Cod sursa (job #393190)
Cod sursa(job #393190)
#include <cstdio>
#include <string>
#include <map>
#include <ext/hash_map>
using namespace __gnu_cxx;
using namespace std;
const int MAX_N = 100005;
int N, M, L, T, A[MAX_N], U[MAX_N];
struct eqstr
{
bool operator() (const char* s1, const char *s2) const
{
return strcmp(s1, s2) == 0;
}
};
hash_map <const char*, int, hash <const char*>, eqstr> H;
pair <int, string> Q[MAX_N];
string V[MAX_N];
string P[MAX_N];
struct cmp
{
bool operator() (string a, string b)
{
if(a.size() < b.size()) return 1;
return a.compare(b) < 0;
}
};
inline int lsb(int x)
{
return (x & -x);
}
void update(int poz)
{
for(; poz < MAX_N; poz += lsb(poz))
++A[poz];
}
int sum(int poz)
{
int rez = 0;
for(; poz; poz -= lsb(poz))
rez += A[poz];
return rez;
}
int main()
{
freopen("nums.in","rt",stdin);
freopen("nums.out","wt",stdout);
scanf("%d\n", &N);
for(int i = 1; i <= N; ++i)
{
char buf[MAX_N];
fgets(buf, MAX_N, stdin);
int m = strlen(buf);
if(buf[m-1] == '\n') buf[m-1] = 0;
if(buf[0] == '1')
{
V[++L] = string(buf+2),
Q[++T] = make_pair(1, string(buf+2));
//fprintf(stderr, "%s\n", V[L]);
}
else
Q[++T] = make_pair(2, string(buf+2));
}
// fprintf(stderr, "Bulan");
sort(V+1, V+L+1, cmp());
int nr = 0;
for(int i = 1; i <= L; ++i)
{
// fprintf(stderr, "%s\n", V[i]);
const char *s = V[i].c_str();
if(H.find(s) == H.end())
{
H[s] = ++nr;
P[nr] = string(s);
//fprintf(stderr, "%s\n", s);
}
}
N = nr;
for(int i = 1; i <= T; ++i)
{
int op = Q[i].first;
const char *s = Q[i].second.c_str();
// fprintf(stderr, "%d %s\n", op, s);
if(op == 1)
{
int poz = H[s];
if(U[poz]) continue;
//fprintf(stderr, "%d\n", poz);
U[poz] = 1;
update(poz);
}
if(op == 2)
{
int k = 0;
for(int j = 0; s[j]; ++j)
k = k * 10 + s[j] - '0';
// fprintf(stderr, "%d\n", k);
int lg = (1 << 18), i = 0;
for(; lg; lg >>= 1)
if(i + lg < MAX_N && sum(i + lg) < k)
i += lg;
printf("%s\n", P[i+1].c_str() );
}
}
}