Cod sursa(job #716373)

Utilizator ProtomanAndrei Purice Protoman Data 18 martie 2012 18:27:32
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 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, nr, x;
int sel[MAX], nra[MAX];
char strCit[MAX];
string str[MAX];
vector <string> vctStr;
int valAIB[MAX];

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

inline int searchAIB(int loc)
{
	int rez = 0 ;
	for (int i = loc; i; i -= bit(i))
		rez += valAIB[i];

	return rez;
}

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()
{
	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)
			vctStr.pb(strCit);
		else nra[i] = 1;
	}

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

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

			if (!sel[loc])
				addAIB(loc), sel[loc] = 1;
		}
		else
		{
			nr = atoi(str[i].c_str());
			cout << vctStr[searchAIB(ns) - 1];
		}
		
	return 0;
}