Cod sursa(job #1418435)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 13 aprilie 2015 02:03:16
Problema Nums Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define x first
#define y second

using namespace std;

const int nmax = 100005;

int n, i, op, cnt, x, flc, lim, aib[nmax];
string s, str[nmax];

unordered_map<string, int> where;

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

vector <string> v;

void update(int poz)
{
	for (int i = poz; i <= n; i += i & (-i))
		aib[i]++;
}

int query(int val)
{
	int r = 0;
	for (int step = lim; step; step /= 2)
		if (r + step < n && aib[r + step] < val)
		{
			val -= aib[r + step];
			r += step;
		}
	return r + 1;
}

int main()
{
	ifstream fin("nums.in");
	ofstream fout("nums.out");

	fin >> n;
	for (i = 1; i <= n; i++)
	{
		fin >> op;
		if (op == 1)
		{
			fin >> s;
			v.pb(s);
		}
		else
			fin >> op;
	}

	sort(all(v), cmp());

	for (auto it : v)
	{
		cnt++;
		where[it] = cnt;
		str[cnt] = it;
	}

	fin.close();
	ifstream f("nums.in");


	f >> n;

	for (lim = 1; 2 * lim <= n; lim *= 2);

	for (i = 1; i <= n; i++)
	{
		f >> op;
		if (op == 1)
		{
			f >> s;
			update(where[s]);
		}
		else
		{
			f >> x;
			flc = query(x);
			fout << str[flc] << '\n';
		}
	}

	return 0;
}