Cod sursa(job #1367723)

Utilizator Eugen01Vasilescu Eugen Eugen01 Data 2 martie 2015 01:31:13
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.83 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>

#define Nmax 100005

using namespace std;

ifstream in("heavypath.in");
ofstream out("heavypath.out");

int chainNumber;
int value[Nmax], level[Nmax], children[Nmax], AI[4 * Nmax];
int chainSize[Nmax], chain[Nmax], chainParent[Nmax], chainLevel[Nmax], chainOffset[Nmax];
bool visited[Nmax];
vector<int> G[Nmax], chains[Nmax];

void DFS(int node, int L)
{
    bool isLeaf = true;
    int biggestChain = -1, maxChildren = 0;

    visited[node] = true;
    level[node] = L;
    children[node] = 1;

    for (int i = 0; i < G[node].size(); i++)
    {
        int nextNode = G[node][i];

        if (visited[nextNode]) continue;
        isLeaf = false;

        DFS(nextNode, L + 1);

        children[node] += children[nextNode];

        if (biggestChain == -1)
        {
            biggestChain = chain[nextNode];
            maxChildren = children[nextNode];
        }
        else if (children[nextNode] > maxChildren)
        {
            biggestChain = chain[nextNode];
            maxChildren = children[nextNode];
        }
    }

    if (isLeaf)
    {
        chainNumber++;
        chainSize[chainNumber] = 1;
        chain[node] = chainNumber;

        chains[chainNumber].push_back(node);
        return;
    }

    chainSize[biggestChain]++;
    chains[biggestChain].push_back(node);
    chain[node] = biggestChain;

    for (int i = 0; i < G[node].size(); i++)
    {
        int nextNode = G[node][i];

        if (chain[node] == chain[nextNode] || level[nextNode] < level[node]) continue;

        chainParent[chain[nextNode]] = node;
        chainLevel[chain[nextNode]] = level[node];
    }
}

void build(int node, int left, int right, int currentChain, int offset)
{
    int middle = (left + right) / 2;

    if (left == right)
    {
        AI[node + offset] = value[chains[currentChain][left - 1]];
        return;
    }

    build(node * 2, left, middle, currentChain, offset);
    build(node * 2 + 1, middle + 1, right, currentChain, offset);

    AI[node + offset] = max(AI[node * 2 + offset], AI[node * 2 + 1 + offset]);
}

void update(int node, int left, int right, int position, int offset, int value)
{
    int middle = (left + right) / 2;

    if (left == right)
    {
        AI[node + offset] = value;
        return;
    }

    if (middle >= position) update(node * 2, left, middle, position, offset, value);
    if (middle < position) update(node * 2 + 1, middle + 1, right, position, offset, value);

    AI[node + offset] = max(AI[node * 2 + offset], AI[node * 2 + 1 + offset]);
}

int query(int node, int left, int right, int a, int b, int offset)
{
    int middle = (left + right) / 2;
    int result = 0;

    if (a <= left && b >= right)
    {
        return AI[node + offset];
    }

    if (middle >= a) result = max(result, query(node * 2, left, middle, a, b, offset));
    if (middle < b) result = max(result, query(node * 2 + 1, middle + 1, right, a, b, offset));

    return result;
}

int main()
{
    int n, t;
    in >> n >> t;

    for (int i = 1; i <= n; i++)
        in >> value[i];

    for (int i = 1; i < n; i++)
    {
        int x, y;
        in >> x >> y;

        G[x].push_back(y);
        G[y].push_back(x);
    }

    DFS(1, 1);
    for (int i = 1; i <= chainNumber; i++)
        reverse(chains[i].begin(), chains[i].end());

    chainOffset[1] = 0;
    for (int i = 1; i <= chainNumber; i++)
    {
        build(1, 1, chainSize[i], i, chainOffset[i]);

        chainOffset[i + 1] = chainOffset[i] + 4 * chainSize[i];
    }

    while (t--)
    {
        int type, x, y;
        in >> type >> x >> y;

        if (type == 0)
        {
            update(1, 1, chainSize[chain[x]], level[x] - chainLevel[chain[x]], chainOffset[chain[x]], y);
        }
        else
        {
            int result = 0;

            while (1)
            {
                if (chain[x] == chain[y])
                {
                    if (level[x] > level[y])
                    {
                        int aux = x;
                        x = y;
                        y = aux;
                    }

                    int value = query(1, 1, chainSize[chain[x]], level[x] - chainLevel[chain[x]], level[y] - chainLevel[chain[x]], chainOffset[chain[x]]);
                    result = max(result, value);

                    out << result << "\n";
                    break;
                }
                else
                {
                    if (chainLevel[chain[x]] < chainLevel[chain[y]])
                    {
                        int aux = x;
                        x = y;
                        y = aux;
                    }

                    int value = query(1, 1, chainSize[chain[x]], 1, level[x] - chainLevel[chain[x]], chainOffset[chain[x]]);
                    result = max(result, value);

                    x = chainParent[chain[x]];
                }
            }
        }
    }
}