Cod sursa(job #444978)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 22 aprilie 2010 12:17:14
Problema Nums Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>


using namespace std;
using namespace __gnu_cxx;


#define nmax 100005
#define VIT vector <int> :: iterator
#define pb push_back
#define f first
#define s second
#define mp make_pair

string S [nmax];
struct cmp {
	bool operator()(const int &a, const int &b) {
		if (S [a].size () == S [b].size ())
			return S [a].compare (S [b]) < 0;
		return S [a].size () < S [b].size ();
	}
};
int aib [nmax], a ,j, idx, order [nmax], i, cancel [nmax], logN;
string str;
int opp [nmax][3];
inline void update (int x) {
	for (; x <= idx; x += x & - x) aib [x]++;
}
inline int query (int x) {
	int sum = 0;
	for (; x >= 1; x -= x & -x) sum += aib [x];
	return sum;
}
inline int cbin (int x)
{	
	int i = 0, step, ans, sol = 0;
	for (i = idx, step = logN; step; step >>= 1)
	{
		if (i - step > 0) {
			ans = query (i - step);
			if (x <= ans) i -= step;
			}
	}
	return i;
}
int main ()
{
	freopen ("nums.in", "r", stdin);
	freopen ("nums.out", "w", stdout);
	int m,op, qq;
	scanf ("%d\n", &m);
	for (i = 1; i <= m; i++)
	{
		scanf ("%d", &op);
		if (op == 1) 
		{	
			cin >> str;
			++idx;
			S [idx] = str;
			opp [i][0] = 1;
			//cout << S [idx] << " ";
		}
		else if (op == 0)
		{
			cin >> qq;
			opp [i][1] = qq;
		}
	}
	for (i = 1; i <= idx; i++) order [i] = i;
	sort (order + 1, order + idx + 1, cmp ());
	for (i = 1; i <= idx; i++) cancel [order [i]] = i;
	for (logN = 1; logN <= idx; logN <<= 1);
	for (i = 1; i <= m; i++)
	{
		if (opp [i][0])
			update (cancel [++j]);
		else 
		{
			a = cbin (opp [i][1]);
			cout <<S [order [a]] << "\n";
		}
	}
	return 0;
}