Cod sursa(job #591157)

Utilizator celmaicontcont de cont celmaicont Data 22 mai 2011 18:54:12
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 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, x;
int sel[MAX];
char strCit[MAX];
vector <string> vctStr;
int aIntAp[4 * MAX];

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

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

		aIntAp[nod] = aIntAp[nod * 2] + aIntAp[nod * 2 + 1];
	}
}

inline int searchAInt(int nod, int fr, int ls, int nr)
{
	if (fr == ls)
		return fr;
	else
	{
		int med = (fr + ls) / 2;

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

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);

			addAInt(1, 1, ns, int(it - vctStr.begin()) + 1);
		}
		else
		{
			int poz;
			scanf("%d", &poz);

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

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