Cod sursa(job #864998)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 25 ianuarie 2013 22:15:17
Problema Nums Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

#define MAX 100005
#define PIS pair<int, string>
#define f first
#define s second
#define leftSon (nod << 1)
#define rightSon ((nod << 1) | 1)
#define pb push_back

using namespace std;

int N, poz[MAX];
int aInt[MAX << 2];
PIS V[MAX];
vector<int> A;

bool cmp(int A, int B)
{
    if(V[A].s.length() == V[B].s.length()) return V[A].s < V[B].s;
    return V[A].s.length() < V[B].s.length();
}

void update(int nod, int L, int R, int poz)
{
    if(L == R)
    {
        aInt[nod] = 1;
        return;
    }
    int M = (L + R) >> 1;
    if(poz <= M) update(leftSon, L, M, poz);
    else update(rightSon, M + 1, R, poz);
    aInt[nod] = aInt[leftSon] + aInt[rightSon];
}

int query(int nod, int L, int R, int X)
{
    if(L == R) return L;
    int M = (L + R) >> 1;
    if(aInt[leftSon] >= X) return query(leftSon, L, M, X);
    else return query(rightSon, M + 1, R, X - aInt[leftSon]);
}

int main()
{
    ifstream in("nums.in"); in>>N;
    for(int i = 1; i <= N; i++)
    {
        in>>V[i].f>>V[i].s;
        if(V[i].f) A.pb(i);
    }
    sort(A.begin(), A.end(), cmp);
    for(size_t i = 0; i < A.size(); i++) poz[A[i]] = i;
    ofstream out("nums.out");
    for(int i = 1; i <= N; i++)
    {
        if(V[i].f)
            update(1, 0, A.size() - 1, poz[i]);
        else
        {
            istringstream str(V[i].s);
            int val;
            str >> val;
            out<<V[A[query(1, 0, A.size() - 1, val)]].s<<"\n";
        }
    } out.close();
    return 0;
}