Cod sursa(job #402860)

Utilizator indestructiblecont de teste indestructible Data 24 februarie 2010 11:08:15
Problema Nums Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.06 kb
#include <stdio.h>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <ext/hash_map>
#define NMAX 100005
using namespace std;
using namespace __gnu_cxx;
int n,poz,r,nr,tip,ord[NMAX],t,C[NMAX],aib[NMAX],act,poz_a;
vector <char> A[NMAX];
char line[NMAX];
struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};
hash_map <const char *, int, hash <const char*>, eqstr> M;
string str;
struct query
{
	int a,b;
};
query B[NMAX];
inline int cif(char x)
{
	return x>='0' && x<='9';
}
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 0;
}
int lsb(int x)
{
	return x & -x;
}
void update(int poz,int val)
{
	int i;
	for (i=poz; i<=r; i+=lsb(i))
		aib[i]+=val;
}
int suma(int x)
{
	int i,s=0;
	for (i=x; i>=1; i-=lsb(i))
		s+=aib[i];
	return s;
}
int cbin(int val)
{
	int i,step=(1<<17);
	for (i=0; step; step>>=1)
		if (i+step<=r && suma(i+step)<val)
			i+=step;
	return ++i;
}
int main()
{
	freopen("nums.in","r",stdin);
	freopen("nums.out","w",stdout);
	scanf("%d\n",&n);
	int i,j,st;
	while (n)
	{
		scanf("%d",&tip);
		if (tip)
		{
			fgets(line+1,NMAX,stdin);
			poz=0;
			while (!cif(line[poz+1]))
				poz++;
			st=poz+1;
			str.clear();
			while (cif(line[poz+1]))
				poz++,str+=line[poz];
			const char *s = str.c_str();
			if (M.find(s)==M.end())
			{
				M[s]=++r;
				ord[r]=r;
				for (i=st; i<=poz; i++)
					A[r].push_back(line[i]);
			}
		}
		if (!tip)
		{
			scanf("%d\n",&nr);
			B[++t].a=nr; B[t].b=r;
		}
		n--;
	}
	sort(ord+1,ord+r+1,comp);
	for (i=1; i<=r; i++)
		C[ord[i]]=i;
	for (i=1; i<=r; i++)
	{
		update(C[i],1);
		while (B[act+1].b==i)
		{
			act++;
			poz_a=cbin(B[act].a);
			for (j=0; j<A[ord[poz_a]].size(); j++)
				printf("%c",A[ord[poz_a]][j]);
			printf("\n");
		}
	}
	return 0;
}