Cod sursa(job #444890)

Utilizator prisonbreakMichael Scofield prisonbreak Data 21 aprilie 2010 23:29:10
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <cstdio>
#include <algorithm>
#include <ext/hash_map>
#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 <pair <int, string> > S;
struct cmp {
	bool operator()(const pair <int, string>& a, const pair <int, string>& b) {
		if (a.s.size () == b.s.size ())
			return a.s.compare (b.s) < 0;
		return a.s.size () < b.s.size ();
	}
};
int aib [nmax], idx, order [nmax];
string str;
vector <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 >= 1; x -= x & -x) sum += aib [x];
	return sum;
}
inline int cbin (int x)
{	
	int st, dr, mid, ans = 0;
	st = 1; dr = idx;
	while (st <= dr)
	{
		mid = (st + dr) / 2;
		ans = query (mid);
		if (ans < x)
			st = mid + 1;
		else if (ans > x)
			dr = mid - 1;
		else if (ans == x)
			return mid;
	}
	return 0;
}
int main ()
{
	freopen ("nums.in", "r", stdin);
	freopen ("nums.out", "w", stdout);
	int m, i, op, qq;
	scanf ("%d\n", &m);
	for (i = 1; i <= m; i++)
	{
		scanf ("%d", &op);
		if (op == 1) 
		{	
			cin >> str;
			++idx;
			S.pb (mp (idx, str));
			//cout << S [idx] << " ";
		}
		else if (op == 0)
		{
			cin >> qq;
			opp [idx].pb (qq);
		}
	}
	sort (S.begin (), S.end (), cmp ());
	for (i = 0; i < S.size (); i++)
		order [S [i].f] = i + 1;
	int a;
	for (i = 1; i <= idx; i++)
	{
		update (order [i]);
		if (opp [i].size ()) 
			for (VIT it = opp [i].begin (); it != opp [i].end (); it++)
			{
				a = cbin (*it);
				cout << S [a - 1].s << "\n";
			}
	}
	return 0;
}