Pagini recente » Cod sursa (job #2673834) | Cod sursa (job #1851021) | Cod sursa (job #875186) | Cod sursa (job #1671130) | Cod sursa (job #1667210)
#include <bits/stdc++.h>
using namespace std;
/// ////////////////////////////////
#define nmax 100003
struct T_NODE
{
T_NODE *fii[10];
int nr[10];
bool last;
T_NODE()
{
for(int i=0; i<10; ++i) fii[i]=0,nr[i]=0;
last=0;
}
};
T_NODE *tries[nmax];
int n;
char s[nmax],res[nmax],l;
int aib[nmax];
void trie_add(T_NODE *nod, char *p)
{
if(*p=='\0')
{
nod->last=1;
return;
}
int son=(*p)-48;
if(!nod->fii[son]) nod->fii[son]=new T_NODE();
++nod->nr[son];
trie_add(nod->fii[son],p+1);
}
void trie_query(T_NODE *nod,int k)
{
if(nod->last) return;
int son;
for(son=0; k>0; ++son) k-=nod->nr[son];
--son;
res[++l]=son+48;
trie_query(nod->fii[son],k+nod->nr[son]);
}
void update_aib(int pos)
{
for(; pos<nmax; pos+=(pos&(-pos))) ++aib[pos];
}
int query_aib(int &k)
{
int step,pos=0;
step=65536;
for(; step; step>>=1)
if(pos+step<nmax)
if(aib[pos+step]<k)
{
pos+=step;
k-=aib[pos];
}
return pos;
}
int main()
{
int i,tip,len,k,pos;
freopen("nums.in","r",stdin);
freopen("nums.out","w",stdout);
cin>>n;
for(i=1; i<=n; ++i)
{
cin>>tip;
if(tip==1)
{
cin>>s;
len=strlen(s);
if(!tries[len]) tries[len]=new T_NODE();
trie_add(tries[len],s);
update_aib(len);
}
else
{
cin>>k;
pos=query_aib(k);
++pos;
l=-1;
trie_query(tries[pos],k);
res[++l]='\0';
cout<<res<<'\n';
}
}
return 0;
}