Cod sursa(job #445414)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 23 aprilie 2010 18:30:02
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 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 VST vector <string> :: iterator
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inf nmax + 1000

vector <string> strs;
struct cmp {
	bool operator()(const string &a, const string &b) {
		if (a.size () == b.size ())
			return a.compare (b) < 0;
		return a.size () < b.size ();
	}
};

int aib [nmax], idx, C [nmax], i, logN, X;
string str;
int opp [nmax];
inline void update (int x) {
	for (; x <= idx; x += x & - x) aib [x]++;
}
inline int query (int x) {
	int sum = 0;
	for (x++; x >= 1; x -= x & -x) sum += aib [x];
	return sum;
}
inline int cbin (int x)
{	
	int step, i;
	for (step = logN, i = idx; step; step >>= 1)
		if (i - step > 0) 
			if (x < query (i - step))
				i -= step;
	return i;
}
vector <string> orig;
int a, poz;
void afis (vector <string> T) {for (VST it = T.begin (); it != T.end (); it++) cout << *it <<endl;cout << endl;}
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;
			strs.pb (str);
			orig.pb (str);
		}
		else if (op == 0) {
			cin >> qq;
			opp [i] = qq;
		}
	}
	for (logN = 1; logN < idx; logN <<= 1);
	sort (strs.begin (), strs.end (), cmp ());
	strs.resize (unique (strs.begin (), strs.end ()) - strs.begin ());
	//afis (strs); afis (orig);
	X = -1;
	for (i = 1; i <= m; i++)
	{
		if (!opp [i]) {
			++X;
			poz = lower_bound (strs.begin (), strs.end (), orig [X], cmp ()) - strs.begin ();
			update (poz + 1);
		}else {
			a = cbin (opp [i]);
			printf ("%s\n",strs [a - 1].c_str ());
		}
	}
	return 0;
}