Cod sursa(job #3303436)

Utilizator brianabucur11Briana Bucur brianabucur11 Data 15 iulie 2025 16:08:01
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.17 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax=1e5+5;

vector <int> g[nmax], hp[nmax], aint[nmax];
int n, m, k, v[nmax], weight[nmax], father[nmax], lvl[nmax], where[nmax], poz[nmax];

void hpd (int node, int t)
{
    weight[node]=1;
    father[node]=t;
    lvl[node]=lvl[t]+1;
    for (auto it:g[node])
    {
        if (it!=t)
        {
            hpd(it,node);
            weight[node]+=weight[it];
        }
    }
    if (weight[node]==1)
    {
        k++;
        where[node]=k;
        poz[node]=0;
        hp[k].push_back(node);
        return;
    }
    int best=0;
    for (auto it:g[node])
    {
        if (it!=t)
        {
            if (weight[it]>weight[best])
                best=it;
        }
    }
    where[node]=where[best];
    poz[node]=hp[where[node]].size();
    hp[where[node]].push_back(node);
}

void init (int nod, int poz, int st, int dr)
{
    if (st==dr)
    {
        aint[poz][nod]=v[hp[poz][st]];
        return;
    }
    int mij=(st+dr)/2;
    init(2*nod,poz,st,mij);
    init(2*nod+1,poz,mij+1,dr);
    aint[poz][nod]=max(aint[poz][nod*2],aint[poz][nod*2+1]);
}

void update (int nod, int pos, int st, int dr, int poz, int val)
{
    if (st==dr)
    {
        aint[pos][nod]=val;
        return;
    }
    int mij=(st+dr)/2;
    if (poz<=mij)
        update(2*nod,pos,st,mij,poz,val);
    else
        update(2*nod+1,pos,mij+1,dr,poz,val);
    aint[pos][nod]=max(aint[pos][2*nod],aint[pos][2*nod+1]);
}

int query (int nod, int poz, int st, int dr, int p, int q)
{
    if (p<=st && dr<=q)
        return aint[poz][nod];
    int cl=-1, cr=-1;
    int mij=(st+dr)/2;
    if (p<=mij)
        cl=query(2*nod,poz,st,mij,p,q);
    if (mij+1<=q)
        cr=query(2*nod+1,poz,mij+1,dr,p,q);
    return max(cl,cr);
}

int solve (int x, int y)
{
    int rez=-1;
    int lantx=where[x], lanty=where[y];
    if (lantx==lanty)
    {
        int a=min(poz[x],poz[y]);
        int b=max(poz[x],poz[y]);
        rez=max(rez,query(1,lantx,0,hp[lantx].size()-1,a,b));
    }
    else
    {
        if (lvl[hp[lantx][0]]<lvl[hp[lanty][0]])
        {
            swap(x,y);
            swap(lantx,lanty);
        }
        rez=max(rez,query(1,lantx,0,hp[lantx].size()-1,0,poz[x]));
        rez=max(rez,solve(father[hp[lantx][0]],y));
    }
    return rez;
}

signed main()
{
    fin >> n >> m;
    for (int i=1; i<=n; i++)
        fin >> v[i];
    for (int i=1; i<n; i++)
    {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    hpd(1,0);
    for (int i=1; i<=k; i++)
        reverse(hp[i].begin(),hp[i].end());
    for (int i=1; i<=n; i++)
        poz[i]=hp[where[i]].size()-1-poz[i];
    for (int i=1; i<=k; i++)
    {
        aint[i].resize(hp[i].size()*4);
        init(1,i,0,hp[i].size()-1);
    }
    for (int i=1; i<=m; i++)
    {
        int c, x, y;
        fin >> c >> x >> y;
        if (c==0)
            update(1,where[x],0,hp[where[x]].size()-1,poz[x],y);
        else
            fout << solve(x,y) << '\n';
    }
    return 0;
}