Cod sursa(job #631792)

Utilizator cont_de_testeCont Teste cont_de_teste Data 9 noiembrie 2011 19:21:31
Problema Nums Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
using namespace std;

#define DN 100005
using namespace std;

string V[DN], P[DN];
pair <int, string> Q[DN];
int N, L, T, A[DN], U[DN];

struct cmp
{
	bool operator() (const string &a, const string &b) const
	{
		if(a.size() ==  b.size())
			return a.compare(b) < 0;
		return a.size() < b.size();
	}
};

inline int lsb(int x)
{
	return (x & -x);
}

void update(int poz)
{
	for(; poz <= 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[DN];
		fgets(buf, DN, 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);
		}

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

	}

	sort(V+1, V+L+1, cmp());
	P[N = 1] = V[1];
	for(int i = 2; i <= L; ++i)
		if(V[i] != P[N])
			P[++N] = V[i];

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

		if(op == 1)
		{
			int poz = lower_bound(P+1, P+N+1, s, cmp()) - P;
			if(U[poz]) continue;
			U[poz] = 1;

			update(poz);
		}

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

			int lg = (1 << 17), i = 0;

			for(; lg; lg >>= 1)
				if(i + lg <= N && sum(i + lg) < k)
					i += lg;

			printf("%s\n", P[i+1].c_str());
		}
	}
}