Cod sursa(job #445010)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 22 aprilie 2010 14:44:40
Problema Nums Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 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

vector <string> S;
vector <pair <int, string> > op;
int idx, M, type, logN, poz;
struct cmp {
	bool operator()(const string &a, const string &b) {
		if (a.size () == b.size ())
			return a.compare (b) < 0;
		return a < b;
	}
};
int aib [nmax];
string str;
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 srch (int x) {
	int i, step;
	for (i = idx, step = logN; step; step >>= 1)
		if (i - step > 0 && x <= query (i - step))
			i -= step;
	return i;
}
int main () {
	
	freopen ("nums.in", "r", stdin);
	freopen ("nums.out", "w", stdout);
	int a, i;
	scanf ("%d", &M);
	for (i = 0; i < M; i++) {
		cin >> type >> str;
		if (type == 1) ++idx;
		S.pb (string (str));
		op.pb (mp (type, string (str)));
	}
	for (logN = 1; logN < idx; logN <<= 1);
	sort (S.begin (), S.end (), cmp ());
	S.resize (unique (S.begin (), S.end ()) - S.begin ());
	for (i = 0; i < M; i++)
	{
		type = op [i].f;
		if (type) {
			poz = lower_bound (S.begin (), S.end (), op [i].s, cmp ()) - S.begin ();
			update (poz);
		}else {
		sscanf (op [i].s.c_str (), "%d", &a);
		poz = srch (a);
		printf ("%s\n", S [poz].c_str ());
		}
	}
	return 0;
}