Pagini recente » Cod sursa (job #2124062) | Cod sursa (job #2770583) | Cod sursa (job #1359726) | Cod sursa (job #216312) | Cod sursa (job #591163)
Cod sursa(job #591163)
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#define MAX 100010
#define bit(x) (x & (x ^ (x - 1)))
#define pb push_back
using namespace std;
int n, ns, nr, x;
int sel[MAX], nra[MAX];
char strCit[MAX];
string str[MAX];
vector <string> vctStr;
int aIntAp[3 * MAX];
inline void addAInt(int nod, int fr, int ls, int loc)
{
if (fr == ls)
aIntAp[nod] = 1;
else
{
int med = (fr + ls) >> 1;
if (loc <= med)
addAInt(nod << 1, fr, med, loc);
if (med < loc)
addAInt((nod << 1) + 1, med + 1, ls, loc);
aIntAp[nod]++;
}
}
inline int searchAInt(int nod, int fr, int ls)
{
if (fr == ls)
return fr;
else
{
int med = (fr + ls) >> 1;
if (aIntAp[nod << 1] >= nr)
return searchAInt(nod << 1, fr, med);
nr -= aIntAp[nod << 1];
return searchAInt((nod << 1) + 1, med + 1, ls);
}
}
inline bool cmp(const string &a, const string &b)
{
if (a.size() != b.size())
return a.size() < b.size();
else return a < b;
}
int main()
{
freopen("nums.in", "r", stdin);
freopen("nums.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d ", &x);
fgets(strCit, MAX, stdin);
strCit[strlen(strCit) - 1] = 0;
str[i] = strCit;
if (x)
vctStr.pb(strCit);
else nra[i] = 1;
}
sort(vctStr.begin(), vctStr.end(), cmp);
ns = vctStr.size();
for (int i = 1; i <= n; i++)
if (!nra[i])
{
vector <string>::iterator it = lower_bound(vctStr.begin(), vctStr.end(), str[i], cmp);
int loc = int(it - vctStr.begin()) + 1;
if (!sel[loc])
addAInt(1, 1, ns, loc), sel[loc] = 1;
}
else
{
nr = atoi(str[i].c_str());
puts(vctStr[searchAInt(1, 1, ns) - 1].c_str());
}
fclose(stdin);
fclose(stdout);
return 0;
}