Cod sursa(job #535114)

Utilizator ooctavTuchila Octavian ooctav Data 16 februarie 2011 19:51:27
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.83 kb
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<deque>
#include<queue>
#include<set>
#include<vector>
using namespace std;

const char IN[] = {"nums.in"};
const char OUT[] = {"nums.out"};
const int INF = 1000000005;
const double PI  = 3.14159265;
const int NMAX = 100005;
const int SIRMAX = 100005;
const int MOD1 = 105473;

#define II inline
#define LL long long
#define PII pair<int, int>
#define PDD pair<double, double>
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define FOR(i, a, b) 	for(int i = a ; i <= b ; i++)
#define IFOR(i, a, b) 	for(int i = a ; i >= b ; i--)
#define FORIT(it, V) 	for(vector<int> :: iterator it = V.begin() ; it != V.end() ; it++)
#define cifra(a) ('0' <= a && a <= '9')

void scrie(int x);

int N, NR;
string s[NMAX];
int lung[NMAX];
int ind[NMAX], rev[NMAX];
PII op[NMAX];
int h1[MOD1];
int Arb[8 * NMAX];
int X;

void citi()
{
	scanf("%d", &N);
	
	int id;
	static char c[SIRMAX] = {0};
	int scad = 0;
	
	FOR(i, 1, N)
	{
		scanf("%d ", &id);
		if(id == 1)
		{
			scanf("%s", c);
			lung[++NR] = strlen(c);
			while(!cifra(c[lung[NR] - 1]))	lung[NR]--;
			FOR(k, 0, lung[NR] - 1)	h1[NR] = (10 * h1[NR] + c[k] - '0') % MOD1;
			
			bool b = 0;
			FOR(j, 1, NR - 1)	
				if(lung[NR] == lung[j] && h1[NR] == h1[j])
				{
					b = 1;
					break;
				}
			if(!b)	s[NR] = c;
			fill(c, c + lung[NR] + 1, 0);
			if(b)	{lung[NR] = 0;h1[NR] = 0;NR--;scad++;}
		}
		else
			scanf("%d\n", &op[i - scad].sc);
		op[i - scad].fs = id;
	}
	N = N - scad;
}

bool comp(int x, int y)
{
	if(lung[x] < lung[y])	return 1;
	else if(lung[x] > lung[y])	return 0;
	return s[x] < s[y];
}

void aranja()
{
	FOR(i, 1, NR)	ind[i] = i;
	sort(ind + 1, ind + NR + 1, comp);
	
	int NR2 = NR;
	FOR(i, 1, NR)	if(s[i] == s[i - 1])	lung[i] = INF, NR2--;
	
	sort(ind + 1, ind + NR + 1, comp);
	NR = NR2;
	FOR(i, 1, NR) 	rev[ind[i]] = i;
}

void update(int nod, int st, int dr)
{
	if(st == dr)
	{
		Arb[nod] = 1;
		return;
	}
	int mij = (st + dr) / 2;
	if(X <= mij)	update(2 * nod, st, mij);
	else			update(2 * nod + 1, mij + 1, dr);
	Arb[nod]++;
}

int query(int nod, int st, int dr)
{
	if(st == dr)
		return st;
	int mij = (st + dr) / 2;
	if(X <= Arb[2 * nod])	return query(2 * nod, st, mij);
	else			{X-= Arb[2 * nod]; return query(2 * nod + 1, mij + 1, dr);}
}


void arbore()
{
	for(int i = 1, j = 0 ; i <= N ; i++)
		if(op[i].fs == 1)
		{
			j++;
			X = rev[j];
			update(1, 1, NR);
		}
		else
		{
			X = op[i].sc;
			scrie(ind[query(1, 1, NR)]);
		}
}

void scrie(int x)
{
	cout << s[x];
	printf("\n");
}

int main()
{
	freopen(IN, "r", stdin);
	freopen(OUT, "w", stdout);
	citi();
	aranja();
	arbore();
	//FOR(i, 1, NR)	cout << s[i] << '\n';
	return 0;
}