Cod sursa(job #652633)

Utilizator indestructiblecont de teste indestructible Data 25 decembrie 2011 16:22:59
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#define NMAX 100005
#define LMAX (1<<18)
#define pb push_back
#define pii pair <int,int>
#define mp make_pair
#define f first
#define s second
using namespace std;
int t,op,ord[NMAX],C[NMAX],D[LMAX],r,poz,val[NMAX],F[NMAX],L[LMAX],R[LMAX],M[LMAX];
char line[NMAX],ap[NMAX];
vector <char> A[NMAX];
pii B[NMAX];
bool comp(int x,int y)
{
	if (A[x].size()<A[y].size())
		return 1;
	if (A[x].size()>A[y].size())
		return 0;
	int i;
	for (i=0; i<A[x].size(); i++)
	{
		if (A[x][i]<A[y][i])
			return 1;
		if (A[x][i]>A[y][i])
			return 0;
	}
	return 1;
}
inline int egal(int x,int y)
{
	if (A[x].size()!=A[y].size())
		return 0;
	int i;
	for (i=0; i<A[x].size(); i++)
		if (A[x][i]!=A[y][i])
			return 0;
	return 1;
}
void update()
{
	poz=F[poz];
	for ( ; poz; poz>>=1)
		D[poz]++;
}
void cauta(int st,int dr,int nod)
{
	if (st==dr)
	{
		int i;
		for (i=0; i<A[val[st]].size(); i++)
			printf("%c",A[val[st]][i]);
		printf("\n");
		return ;
	}
	int mij=M[nod];
	if (D[L[nod]]<poz)
	{
		poz-=D[L[nod]];
		cauta(mij+1,dr,R[nod]);
	}
	else
		cauta(st,mij,L[nod]);
}
inline int cif(char x)
{
	return x>='0' && x<='9';
}
void cstr(int st,int dr,int nod)
{
	if (st==dr)
	{
		F[st]=nod;
		return ;
	}
	int mij=(st+dr)>>1;
	M[nod]=mij; L[nod]=nod<<1; R[nod]=L[nod]+1;
	cstr(st,mij,L[nod]);
	cstr(mij+1,dr,R[nod]);
}
int main()
{
	freopen("nums.in","r",stdin);
	freopen("nums.out","w",stdout);
	scanf("%d\n",&t);
	int i,tip;
	for (i=1; i<=t; i++)
	{
		scanf("%d",&tip);
		if (tip==1)
		{
			fgets(line+1,NMAX,stdin);
			poz=0; op++;
			while (!cif(line[poz+1])) poz++;
			while (cif(line[poz+1])){ poz++; A[op].pb(line[poz]);}
			ord[op]=op;
			B[i]=mp(tip,op);
		}
		else
		{
			scanf("%d",&poz);
			B[i]=mp(tip,poz);
		}
	}
	sort(ord+1,ord+op+1,comp);
	C[ord[1]]=++r; val[1]=ord[1];
	for (i=2; i<=op; i++)
		if (egal(ord[i],ord[i-1]))
			C[ord[i]]=C[ord[i-1]];
		else
			C[ord[i]]=++r,val[r]=ord[i];
	cstr(1,r,1);
	return 0;
}