Cod sursa(job #658295)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 8 ianuarie 2012 15:16:44
Problema Nums Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.41 kb
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include <cstring>

using namespace std;

#define nmax 100005
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define INF nmax + 1000

vector <char> V [nmax];
struct cmp {
	bool operator()(const int &x, const int &y) {
		if (V[x].size()<V[y].size())
		return 1;
	if (V[x].size()>V[y].size())
		return 0;
	int i;
	for (i=0; i<V[x].size(); i++)
	{
		if (V[x][i]<V[y][i])
			return 1;
		if (V[x][i]>V[y][i])
			return 0;
	}
	if (x<y)
		return 1;
	return 0;
	}
};

int aib [nmax],C [nmax], order [nmax], i,op, N, n, j, poz, poz_a, idx;
char str [nmax];
int Q, nrQ;
struct lol {int a, b;};
lol B [nmax];
inline int comp (int a, int b) {
	if (V [a].size () != V [b].size ()) return 1;
	if (V [a].size () == V [b].size ())
		for (int i = 0; i < V [a].size (); i++)
			if (V [a][i] != V [b][i])
				return 1;		
	return 0;
}
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;
}
int cbin (int x) {
	int i, step = 1 << 17;
	for (i = 0; step; step >>= 1)
		if (i + step <= idx && query (i + step) < x)
			i += step;
	return ++i;
}
void debug (int A [nmax]) {
	for (int i = 1; i <= idx; i++)
		printf ("%d\n", A [i]);
	printf ("\n");
}
inline int cif (char x) {
	return x >= '0' && x <= '9';
}
int start, end;
int main () {
	freopen ("nums.in", "r", stdin);
	freopen ("nums.out", "w", stdout);
	scanf ("%d\n", &n);
	for (i = 1; i <= n; i++)
	{
		scanf ("%d", &op);
		if (op)
		{
			gets (str);
			++idx;
			start = 0;
			end = strlen (str) - 1;
			order [idx] = idx;
			while (!cif (str [start])) ++start;
			while (!cif (str [end])) --end;
			for (j = start; j <= end; j++)
				V [idx].pb (str [j]);
		}
		if (!op)
		{
			scanf ("%d\n", &Q);
			B [++nrQ].a = Q;
			B [nrQ].b = idx;
		}
	}
	sort (order + 1, order + idx + 1,cmp ());
	for (i = 1; i <= idx; i++) {
		if (i == 1) {
			C [order [i]] = i;
			continue;
		}
		if (comp (order [i], order [i - 1]))
			C [order [i]]= i;
		else C [order [i]] = INF;
	}//printf ("*******************************\n");
	//debug (C);
	for (i = 1; i <= idx; i++) {
		if (C [i] != INF)
			update (C [i]);
		while (B [poz + 1].b == i) {
			++poz;
			poz_a = cbin (B [poz].a);
			for (j = 0; j < V [order [poz_a]].size (); j++)
				printf ("%c", V [order [poz_a]][j]);
			printf ("\n");
		}
	}
	return 0;
}