Cod sursa(job #879054)

Utilizator cont_de_testeCont Teste cont_de_teste Data 14 februarie 2013 22:09:32
Problema Arbore Scor 100
Compilator cpp Status done
Runda Lista lui wefgef Marime 2.05 kb
#include <cstdio>
#include <vector>
#define MAXN 100001
#define ARBSIZE 262145
#define maxim(a,b) (a>b?a:b)
using std::vector;

int n, m, euler[MAXN], ep, start[MAXN], end[MAXN], x, y;
char color[MAXN];
bool found;

int max[ARBSIZE], inc[ARBSIZE];

vector<int> G[MAXN];

void dfs(int node)
{
    euler[++ep] = node;
    start[node] = ep;

    color[node] = 1;
    int len = G[node].size();
    for (int i = 0 ; i < len ; ++i)
        if (color[G[node][i]] == 0)
            dfs(G[node][i]);

    end[node] = ep;
}

void update(int node, int left, int right, int x, int y)
{
    if (start[x] <= left && right <= end[x])
        inc[node] += y;
    else
    {
        int mid = (left + right) >> 1;
        if (start[x] <= mid)
            update(node << 1, left, mid, x, y);
        if (end[x] > mid)
            update((node << 1) | 1, mid + 1, right, x, y);
    }

        max[node] = maxim(max[node << 1], max[(node << 1) | 1]) + inc[node];
}

int query(int node, int left, int right, int x)
{
    int ll = 0;
   ;

    if ( (x -= inc[node]) == 0)
    {
        printf("%d\n", euler[left]);
        return 1;
    }
    if (left != right && x > 0 && max[node] - inc[node] >= x)
    {
        int mid = (left + right) >> 1;

        if ((ll = query(node << 1, left, mid, x)) == 0)
            ll = query((node << 1) | 1, mid + 1, right, x);
    }

    x += inc[node];
    return ll;
}

int main()
{
    freopen("arbore.in", "r", stdin);
    freopen("arbore.out","w",stdout);

    scanf("%d %d", &n, &m);

    for (int i = 1 ; i < n ; ++i)
    {
        int a, b;scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }

    dfs(1);

    for (int i = 1 ; i <= m ; ++i)
    {
        int command; scanf("%d %d", &command, &x);
        if (command == 1)
        {
            scanf("%d", &y);
            update(1, 1, n, x, y);
        }
        else
        {



            if (!query(1, 1, n, x))
                printf("%d\n", -1);
        }
    }

    return 0;
}