Cod sursa(job #940920)

Utilizator radustn92Radu Stancu radustn92 Data 17 aprilie 2013 14:35:43
Problema Nums Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.25 kb
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iostream>
#define NMAX 100005
#define pb push_back
using namespace std;
 
struct entry
{
    string nr;
    int left, right;
    int total, depth;
};
entry H[NMAX], aux;
int root = 1, r = 1;
int n, k, limit, nrs;
string curr;
vector <string> list;
 
inline int comp(string s1, string s2)
{
    if (s1.size() < s2.size())
        return 1;
    if (s1.size() > s2.size())
        return 0;
    return s1 < s2;
}
 
void add(int node)
{
    if (H[node].total == 0)
    {
        H[node].nr = curr;
		H[node].total = 1;
        H[node].depth = 1;
        nrs++;
        return ;
    }
     
    if ((H[node].nr).compare(curr) == 0)
        return ;
     
    if (!comp(H[node].nr, curr))
    {
        if (!H[node].left)
            H[node].left = ++r;
        add(r);
    }
    else
    {
        if (!H[node].right)
            H[node].right = ++r;
        add(r);
    }
     
    int sons = 1, maxDepth = 0;
    if (H[node].left)
    {
        sons += H[H[node].left].total;
        maxDepth = max(maxDepth, H[H[node].left].depth);
    }
    if (H[node].right)
    {
        sons += H[H[node].right].total;
        maxDepth = max(maxDepth, H[H[node].right].depth);
    }
    H[node].total = sons;
    H[node].depth = maxDepth + 1;
}
 
string search(int node)
{
    if (H[node].left)
    {
        if (H[H[node].left].total >= k)
            return search(H[node].left);
        k -= H[H[node].left].total;
    }
     
    if (k == 1)
        return H[node].nr;
    k--;
     
    return search(H[node].right);
}
 
void getNodes(int node)
{
    if (H[node].left)
        getNodes(H[node].left);
     
    list.pb(H[node].nr);
     
    if (H[node].right)
        getNodes(H[node].right);
	
	H[node] = aux; 
}
 
void remake(int left, int right, int node)
{
    int mid = (left + right) >> 1;
    H[node].nr = list[mid - 1];
    H[node].depth = 1;
    H[node].total = 1;
     
    if (mid > left)
    {
		H[node].left = ++r;
        remake(left, mid - 1, H[node].left);
    }
    if (mid < right)
    {
        H[node].right = ++r;
        remake(mid + 1, right, H[node].right);
    }
     
	int sons = 1, maxDepth = 0;
    if (H[node].left)
    {
        sons += H[H[node].left].total;
        maxDepth = max(maxDepth, H[H[node].left].depth);
    }
    if (H[node].right)
    {
        sons += H[H[node].right].total;
        maxDepth = max(maxDepth, H[H[node].right].depth);
    }
    H[node].total = sons;
    H[node].depth = maxDepth + 1;
}
 
int main()
{
    freopen("nums.in", "r", stdin);
    freopen("nums.out", "w", stdout);
    cin >> n;
	limit = (int)sqrt(n);
     
    int i, tip;
    for (i = 1; i <= n; i++)
    {
        cin >> tip;
        if (tip == 1)
        {
            cin >> curr;
            add(root);
             
            if (H[root].depth > limit)
            {
                list.clear();
                getNodes(root);
                r = 1;
                remake(1, list.size(), root);
            }
        }
        else
        {
            cin >> k;
            cout << search(root) << '\n';
        }
    }
    return 0;
}