Cod sursa(job #393475)

Utilizator Mishu91Andrei Misarca Mishu91 Data 9 februarie 2010 15:11:22
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <cstdio>
#include <map>
#include <string>
#include <ext/hash_map>

using namespace __gnu_cxx;
using namespace std;

const int MAX_N = 100005;

/*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;*/
struct comp
{
	bool operator() (const char *a, const char *b) const
	{
		return strcmp(a, b) < 0;
	}
};

map <const char*, int> H;
string V[MAX_N], P[MAX_N];
pair <int, string> Q[MAX_N];
int N, L, T, A[MAX_N], I[MAX_N], U[MAX_N];

struct cmp
{
	bool operator() (const int &a, const int &b)
	{
		if(V[a].size() ==  V[b].size())
			return V[a].compare(V[b]) < 0;
		return V[a].size() < V[b].size();
	}
};
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;

		string s = string(buf+2);
		if(buf[0] == '1')
		{
			V[++L] = s;
			Q[++T] = make_pair(1, s);
			I[L] = L;
		}

		else
			Q[++T] = make_pair(2, s);
		
	}

	sort(I+1, I+L+1, cmp());

	int nr = 0;
	for(int i = 1; i <= L; ++i)
	{
		const char *s = V[I[i]].c_str();
		if(H.find(s) == H.end())
		{
			H[s] = ++nr;
			P[nr] = V[I[i]];
		}
	}

	N = nr;

	for(int i = 1; i <= T; ++i)
	{
		int op = Q[i].first;
		const char *s = Q[i].second.c_str();

		if(op == 1)
		{
			int poz = H[s];
			if(U[poz]) continue;
			U[poz] = 1;

			update(poz);
		}

		if(op == 2)
		{
			int k = 0;
			for(int j = 0; s[j]; ++j)
				k = k * 10 + s[j] - '0';

			int lg = (1 << 17), 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());
		}
	}
}