Cod sursa(job #444397)

Utilizator indestructiblecont de teste indestructible Data 20 aprilie 2010 13:16:01
Problema Nums Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <stdio.h>
#include <algorithm>
#include <vector>
#define NMAX 101005
#define pb push_back
using namespace std;
int n,r,t,L[NMAX],ord[NMAX],C[NMAX],aib[NMAX];
char line[NMAX];
vector <char> A[NMAX];
struct query
{
	int a,b;
};
query B[NMAX];
inline int cif(char x)
{
	return x>='0' && x<='9';
}
void read()
{
	scanf("%d\n",&n);
	int i,tip,poz;
	for (i=1; i<=n; i++)
	{
		scanf("%d",&tip);
		if (!tip)
		{
			B[++r].a=t;
			scanf("%d\n",&B[r].b);
		}
		if (tip)
		{
			fgets(line+1,NMAX,stdin);
			t++; poz=0;
			while (!cif(line[poz+1]))
				poz++;
			while (cif(line[poz+1]))
			{
				poz++;
				A[t].pb(line[poz]);
				L[t]++;
			}
		}
	}
}
void init()
{
	int i;
	for (i=1; i<=t; i++)
		ord[i]=i;
}
bool comp(int x,int y)
{
	if (L[x]<L[y])
		return 1;
	if (L[x]>L[y])
		return 0;
	int i;
	for (i=0; i<L[x]; i++)
	{
		if (A[x][i]<A[y][i])
			return 1;
		if (A[x][i]>A[y][i])
			return 0;
	}
	return 1;
}
void normalizare()
{
	int i;
	for (i=1; i<=t; i++)
		C[ord[i]]=i;
}
int lsb(int x)
{
	return x & -x;
}
void update(int poz)
{
	int i;
	for (i=poz; i<=n; i+=lsb(i))
		aib[i]++;
}
int suma(int poz)
{
	int i,rez=0;
	for (i=poz; i>0 ;i-=lsb(i))
		rez+=aib[i];
	return rez;
}
int cbin(int val)
{
	int i,step=1<<16;
	for (i=0; step; step>>=1)
		if (i+step<=t && suma(i+step)<=val)
			i+=step;
	return i;
}
void solve()
{
	int i,j,act=0,poz;
	for (i=1; i<=t; i++)
	{
		update(C[i]);
		while (B[act+1].a==i)
		{
			act++;
			poz=cbin(B[act].b);
			for (j=0; j<L[ord[poz]]; j++)
				printf("%c",A[ord[poz]][j]);
			printf("\n");
		}
	}
}
int main()
{
	freopen("nums.in","r",stdin);
	freopen("nums.out","w",stdout);
	read();
	init();
	sort(ord+1,ord+t+1,comp);
	normalizare();
	solve();
	return 0;
}