Cod sursa(job #3156746)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 12 octombrie 2023 20:33:28
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.49 kb
///head[i] -> nodul cu nivelul cel mai mic din lant
///heavy[i] -> fiul cu subarborele maximal
///id_node[i] -> lantul din care face parte i

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

using namespace std;

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

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

vector <int> g[N + 1];

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

void dfs (int node, int parent)
{
    siz[node] = 1;
    tata[node] = parent;
    dep[node] = dep[parent] + 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_in_segtree[timp] = node;
    poz[node] = timp;
    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 * 4 + 1];
    segtree ()
    {
        memset (v, 0, sizeof(v));
    }
    void build (int node, int l, int r)
    {
        if (l == r)
        {
            v[node] = a[poz_in_segtree[l]];
            return;
        }
        int mid = (l + r) >> 1;
        build (node << 1, l, mid);
        build (node << 1 | 1, mid + 1, r);
        v[node] = max (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] = max (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 max (a, b);
    }
} arb;

int query_ans (int x, int y)
{
    int ans = 0;
    while(id_node[x] != id_node[y]){
        if(dep[head[id_node[x]]] > dep[head[id_node[y]]]){
            ans = max(ans, arb.query(1, 1, n, poz[head[id_node[x]]], poz[x]));
            x = tata[head[id_node[x]]];
        }else{
            ans = max(ans, arb.query(1, 1, n, poz[head[id_node[y]]], poz[y]));
            y = tata[head[id_node[y]]];
        }
    }
    int a = poz[x];
    int b = poz[y];
    ans = max(ans, arb.query(1, 1, n, min(a, b), max(a, b)));
    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; ++i)
    {
        cin >> cer >> x >> y;
        if (!cer)
            arb.update(1, 1, n, poz[x], y);
        else
            cout << query_ans (x, y) << '\n';
    }
    return 0;
}