Cod sursa(job #393203)

Utilizator Mishu91Andrei Misarca Mishu91 Data 9 februarie 2010 00:27:47
Problema Nums Scor 0
Compilator cpp Status done
Runda porc_ag Marime 2.22 kb
#include <cstdio>
#include <string>
#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], W[MAX_N];
char viz[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];
vector <string> V[MAX_N];
string P[MAX_N];

/*struct cmp
{
	bool operator() (const int &a, const int &b)
	{
		if(W[a] < W[b]) return 1;
		return V[a].compare(V[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);

	int nrl = 0;
	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[m].push_back(string(buf+2));
			Q[++T] = make_pair(1, string(buf+2));
			if(!viz[m])
				viz[m] = 1, W[++nrl] = m;

			//fprintf(stderr, "%s\n", V[L]);
		}

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

//	fprintf(stderr, "Bulan");
	sort(W+1, W+nrl+1);
	for(int i = 1; i <= nrl; ++i)
		sort(V[W[i]].begin(), V[W[i]].end());

	int nr = 0;
	for(int i = 1; i <= nrl; ++i)
		for(typeof V[W[i]].begin() it = V[W[i]].begin(); it != V[W[i]].end(); ++it)
	{
	//	fprintf(stderr, "%s\n", V[i]);
		const char *s = (*it).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() );
		}
	}
}