Cod sursa(job #591161)

Utilizator celmaicontcont de cont celmaicont Data 22 mai 2011 19:01:03
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>

#define MAX 100010
#define bit(x) (x & (x ^ (x - 1)))
#define pb push_back

using namespace std;

int n, ns, nr, x;
int sel[MAX];
char strCit[MAX];
vector <string> vctStr;
int aIntAp[3 * MAX];

inline void addAInt(int nod, int fr, int ls, int loc)
{
	if (fr == ls)
		aIntAp[nod] = 1;
	else
	{
		int med = (fr + ls) >> 1;

		if (loc <= med)
			addAInt(nod << 1, fr, med, loc);
		if (med < loc)
			addAInt((nod << 1) + 1, med + 1, ls, loc);

		aIntAp[nod] = aIntAp[nod << 1] + aIntAp[(nod << 1) + 1];
	}
}

inline int searchAInt(int nod, int fr, int ls)
{
	if (fr == ls)
		return fr;
	else
	{
		int med = (fr + ls) >> 1;

		if (aIntAp[nod << 1] >= nr)
			return searchAInt(nod << 1, fr, med);
		nr -= aIntAp[nod << 1];
		return searchAInt((nod << 1) + 1, med + 1, ls);
	}
}

inline bool cmp(const string &a, const string &b)
{
	if (a.size() != b.size())
		return a.size() < b.size();
	else return a < b;
}

int main()
{
	freopen("nums.in", "r", stdin);
	freopen("nums.out", "w", stdout);

	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d ", &x);
		fgets(strCit, MAX, stdin);
		strCit[strlen(strCit) - 1] = 0;

		if (x)
			vctStr.pb(strCit);
	}

	sort(vctStr.begin(), vctStr.end(), cmp);
	ns = vctStr.size();

	fclose(stdin);
	freopen("nums.in", "r", stdin);

	scanf("%d", &n);
	
	for (int i = 1; i <= n; i++)
	{
		scanf("%d ", &x);

		if (x)
		{
			fgets(strCit, MAX, stdin);
			strCit[strlen(strCit) - 1] = 0;
			string str = strCit;
			vector <string>::iterator it = lower_bound(vctStr.begin(), vctStr.end(), str, cmp);

			int loc = int(it - vctStr.begin()) + 1;

			if (!sel[loc])
				addAInt(1, 1, ns, loc), sel[loc] = 1;
		}
		else
		{
			scanf("%d", &nr);

			puts(vctStr[searchAInt(1, 1, ns) - 1].c_str());
		}
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}