Cod sursa(job #3194220)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 17 ianuarie 2024 12:42:48
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.33 kb
#include <fstream>
#include <vector>
#include <string.h>

using namespace std;

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

const int N = 1e5;
int v[N + 1], heavy[N + 1], head[N + 1], nivel[N + 1], poz[N + 1], lant[N + 1], poz_lant[N + 1], tata[N + 1], siz[N + 1], poz_in_aint[N + 1];

vector <int> g[N + 1];

int n, m, nr_lant, timp, x, y, cer;

struct aint
{
    int arb[3 * N + 1];
    aint ()
    {
        memset (arb, 0, sizeof (arb));
    }
    void build (int node, int l, int r)
    {
         if (l == r)
        {
            arb[node] = v[poz_in_aint[l]];
            return;
        }
        int mid = (l + r) >> 1;
        build (node << 1, l, mid);
        build (node << 1 | 1, mid + 1, r);
        arb[node] = max (arb[node << 1], arb[node << 1 | 1]);
    }
    void update (int node, int l, int r, int pos, int val)
    {
        if (l == r)
        {
            arb[node] = val;
            return;
        }
        int mid = (l + r) >> 1;
        if (pos <= mid)update (node << 1, l, mid, pos, val);
        else update (node << 1 | 1, mid + 1, r, pos, val);
        arb[node] = max (arb[node << 1], arb[node << 1 | 1]);
    }
    int query (int node, int l, int r, int st, int dr)
    {
        if (l >= st && r <= dr)
            return arb[node];
        int mid = (l + r) >> 1;
        int a = 0, b = 0;
        if (st <= mid) a = query (node << 1, l, mid, st, dr);
        if (dr > mid) b = query (node << 1 | 1, mid + 1, r, st, dr);
        return max (a, b);
    }

} a;

void dfs (int node, int parent)
{
    tata[node] = parent;
    siz[node] = 1;
    nivel[node] = nivel[parent] + 1;
    for (auto it : g[node])
        if (it != parent)
        {
            dfs (it, node);
            if (siz[heavy[node]] < siz[it])
                heavy[node] = it;
            siz[node] += siz[it];
        }
}

void heavy_first (int node, int parent)
{
    ++timp;
    poz_in_aint[timp] = node;
    poz[node] = timp;
    if (heavy[parent] != node)
    {
        ++nr_lant;
        head[nr_lant] = node;
        lant[node] = nr_lant;
    }
    else
        lant[node] = lant[parent];
    if (heavy[node])
        heavy_first (heavy[node], node);
    for (auto it : g[node])
        if (it != parent && it != heavy[node])
            heavy_first (it, node);
}

void query (int x, int y)
{
    int ans = 0;
    while (lant[x] != lant[y])
    {
        if (nivel[head[lant[x]]] < nivel[head[lant[y]]])
        {
            ans = max (ans, a.query (1, 1, n, poz[head[lant[y]]], poz[y]));
            y = tata[head[lant[y]]];
        }
        else
        {
            ans = max (ans, a.query (1, 1, n, poz[head[lant[x]]], poz[x]));
            x = tata[head[lant[x]]];
        }

    }
    ans = max (ans, a.query (1, 1, n, min (poz[x], poz[y]), max (poz[x], poz[y])));
    cout << ans << '\n';
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> v[i];
    for (int i = 1; i < n && cin >> x >> y; ++i)
        g[x].push_back(y), g[y].push_back(x);
    dfs (1, 0);
    heavy_first(1, 0);
    a.build (1, 1, n);
    for (int i = 1; i <= m && cin >> cer >> x >> y; ++i)
    {
        if (!cer)
            a.update (1, 1, n, poz[x], y);
        else
            query (x, y);
    }
    return 0;

}