Cod sursa(job #1514710)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 31 octombrie 2015 14:55:36
Problema Nums Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

#define DIM 100005
#define infile "nums.in"
#define outfile "nums.out"

using namespace std;

vector<string> vec;

map<string, int> pos;
map<string, bool> vis;

int aib[DIM];

int pow2, n;

inline bool comp(const string &a, const string &b) {

	if (a.size() == b.size())
		return a < b;
	else
		return a.size() < b.size();

}

inline unsigned int lsb(const int &x) {

	return (x & (-x));

}

inline void updateAIB(const int &pos) {

	for (int i = pos; i <= n; i += lsb(i))
		++aib[i];

}

inline int queryAIB(int cnt) {

	int ans = 0;

	for (int pow = pow2; pow; pow >>= 1) {

		if (ans + pow < n && aib[ans + pow] < cnt) {

			cnt -= aib[ans + pow];

			ans += pow;

		}

	}

	return ans + 1;

}

int main() {

	ifstream fin(infile);
	ofstream fout(outfile);

	fin >> n;

	string x;

	for (int i = 1; i <= n; ++i) {

		int op;

		fin >> op;

		if (op == 1) {

			fin >> x;

			vec.push_back(x);

		}
		else {

			fin >> op;

		}

	}

	sort(vec.begin(), vec.end(), comp);

	for (unsigned int i = 0; i < vec.size(); ++i) {

		pos[vec[i]] = i + 1;

	}

	fin.close();
	ifstream fin2(infile);

	fin2 >> n;

	pow2 = 1;

	while (2 * pow2 <= n)
		pow2 <<= 1;

	for (int i = 1; i <= n; ++i) {

		int op;

		fin2 >> op;

		if (op == 1) {

			fin2 >> x;

			if (vis[x])
				continue;

			vis[x] = true;

			updateAIB(pos[x]);

		}
		else {

			int x;

			fin2 >> x;

			fout << vec[queryAIB(x) - 1] << '\n';

		}

	}

	return 0;

}

//Trust me, I'm the Doctor!