Cod sursa(job #445442)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 23 aprilie 2010 18:55:24
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 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; 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, rez = 0, sum = 0;
	for (step = 1 << 17; step; step >>= 1)
		if ((rez | step) <= idx) 
			if (sum + aib [rez | step]  <= x) {
				sum += aib [rez | step];
				rez |= step;
			}
	return rez;
}
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;
		}
	}
	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 ();
			if (query (poz) == query (poz - 1))
				update (poz);
		}else {
			a = cbin (opp [i]);
			//cout << a <<endl;
			printf ("%s\n",strs [a - 1].c_str ());
		}
	}
	//afis (strs);
	return 0;
}