Cod sursa(job #716401)

Utilizator ProtomanAndrei Purice Protoman Data 18 martie 2012 19:04:12
Problema Nums Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#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], nra[MAX];
char strCit[MAX];
string str[MAX];
vector <int> vctSort;
vector <string> vctStr;
int valAIB[MAX];

inline void addAIB(int loc)
{
	for (int i = loc; i <= ns; i += bit(i))
		valAIB[i]++;
}

inline int searchAIB(int nr)
{
	int i = 1, loc = 0;
	for (; i * 2 <= ns && valAIB[i] < nr; i *= 2);
	
	for (; i; i /= 2)
		if (loc + i <= ns && valAIB[loc + i] < nr)
		{
			nr -= valAIB[loc + i];
			loc += i;
		}

	return loc;
}

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

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

int main()
{
	ifstream cin("nums.in");
	ofstream cout("nums.out");

	cin >> n;

	for (int i = 1; i <= n; i++)
	{
		cin >> x;

		cin >> strCit;

		str[i] = strCit;

		if (x)
			vctSort.pb(i);
		else nra[i] = 1;
	}

	sort(vctSort.begin(), vctSort.end(), cmp);
	ns = vctSort.size();
	
	for (int i = 0; i < ns; i++)
		vctStr.pb(str[vctSort[i]]);
	
	for (int i = 1; i <= n; i++)
		if (!nra[i])
		{
			vector <string>::iterator it = lower_bound(vctStr.begin(), vctStr.end(), str[i], cmps);

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

			if (!sel[loc])
				addAIB(loc), sel[loc] = 1;
		}
		else cout << vctStr[searchAIB(atoi(str[i].c_str()))] << "\n";
		
	return 0;
}