Cod sursa(job #3168738)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 13 noiembrie 2023 10:23:40
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.57 kb
///heavy[nod] -> fiul care are subarborele maximal

#include <fstream>
#include <vector>
#include <string.h>

using namespace std;

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

const int N = 1e5;
int a[N + 1], heavy[N + 1], dep[N + 1], tata[N + 1], siz[N + 1], id_node[N + 1], head[N + 1], poz[N + 1], poz_in_aint[N + 1];

vector <int> g[N + 1];

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

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

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

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

int query (int x, int y)
{
    int ans = 0;
    while (id_node[x] != id_node[y])
    {
        int capx = head[id_node[x]];
        int capy = head[id_node[y]];
        if (dep[capx] < dep[capy])
        {
            int pos_cap = poz[capy];
            ans = max (ans, arb.query (1, 1, n, pos_cap, poz[y]));
            y = tata[capy];
        }
        else
        {
            int pos_cap = poz[capx];
            ans = max (ans, arb.query (1, 1, n, pos_cap, poz[x]));
            x = tata[capx];
        }
    }
    int posx = poz[x];
    int posy = poz[y];
    ans = max (ans, arb.query (1, 1, n, min (posx, posy), max (posx, posy)));
    return ans;
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> a[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);
    arb.build (1, 1, n);
    for (int i = 1; i <= m && cin >> cer >> x >> y; ++i)
    {
        if (!cer)
            arb.update (1, 1, n, poz[x], y);
        else
            cout << query (x, y) << '\n';
    }
    return 0;
}