Cod sursa(job #2799201)

Utilizator verde.cristian2005Verde Flaviu-Cristian verde.cristian2005 Data 12 noiembrie 2021 17:12:54
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.65 kb
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
vector <int> v[100001];
int vec[100001];
int mar[100001];
int heavyhead[100001];
int aint[400001];
int tata[100001];
int nivel[100001];
int poz[100001];
struct mazan
{
    int first,second;
} de_sortat[100001];
bool cmp(mazan a,mazan b)
{
    if(a.first<b.first)
        return 1;
    if(a.first>b.first)
        return 0;
    if(nivel[a.second]<nivel[b.second])
        return 1;
    return 0;
}
void dfs(int nod,int papa)
{
    mar[nod]=1;
    tata[nod]=papa;
    nivel[nod]=nivel[papa]+1;
    for(auto it:v[nod])
        if(it!=papa)
        {
            dfs(it,nod);
            mar[nod]+=mar[it];
        }
}
void dfs2(int nod,int papa)
{
    int max1=0,ind=0;
    for(auto it:v[nod])
        if(it!=papa&&mar[it]>max1)
        {
            max1=mar[it];
            ind=it;
        }
    heavyhead[ind]=heavyhead[nod];
    for(auto it:v[nod])
        if(it!=papa&&it!=ind)
            heavyhead[it]=it;
    for(auto it:v[nod])
        if(it!=papa)
            dfs2(it,nod);
}
void build(int nod,int st,int dr)
{
    if(st==dr)
    {
        aint[nod]=vec[de_sortat[st].second];
        return;
    }
    int mid=(st+dr)/2;
    build(2*nod,st,mid);
    build(2*nod+1,mid+1,dr);
    aint[nod]=max(aint[2*nod],aint[2*nod+1]);
}
void update(int nod,int st,int dr,int poz,int val)
{
    if(st==dr)
    {
        aint[nod]=val;
        return;
    }
    int mid=(st+dr)/2;
    if(poz<=mid)
        update(2*nod,st,mid,poz,val);
    else
        update(2*nod+1,mid+1,dr,poz,val);
    aint[nod]=max(aint[2*nod],aint[2*nod+1]);
}
int query(int nod,int st,int dr,int ua,int ub)
{
    if(dr<ua||st>ub)
        return 0;
    if(ua<=st&&dr<=ub)
        return aint[nod];
    int mid=(st+dr)/2;
    return max(query(2*nod,st,mid,ua,ub),query(2*nod+1,mid+1,dr,ua,ub));
}
int n;
int dela(int x,int y)
{
    int max1=0;
    while(1)
    {
        if(nivel[heavyhead[x]]>nivel[y])
        {
            max1=max(max1,query(1,1,n,poz[heavyhead[x]],poz[x]));
            x=tata[heavyhead[x]];
        }
        else
        {
            max1=max(max1,query(1,1,n,poz[y],poz[x]));
            break;
        }
    }
    return max1;
}
int process(int x,int y)
{
    int max1=0;
    while(1)
    {
        if(heavyhead[x]==heavyhead[y])
        {
            max1=max(max1,query(1,1,n,min(poz[x],poz[y]),max(poz[x],poz[y])));
            return max1;
        }
        else
        {
            if(nivel[heavyhead[x]]<nivel[heavyhead[y]])
            {
                max1=max(max1,query(1,1,n,poz[heavyhead[y]],poz[y]));
                y=tata[heavyhead[y]];
            }
            else
            {
                max1=max(max1,query(1,1,n,poz[heavyhead[x]],poz[x]));
                x=tata[heavyhead[x]];
            }
        }
    }
}
int main()
{
    ifstream cin("heavypath.in");
    ofstream cout("heavypath.out");
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int m,i,a,b,t;
    cin>>n>>m;
    for(i=1; i<=n; i++)
        cin>>vec[i];
    for(i=1; i<n; i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    heavyhead[1]=1;
    dfs(1,0);
    dfs2(1,0);
    for(i=1; i<=n; i++)
        de_sortat[i]= {heavyhead[i],i};
    sort(de_sortat+1,de_sortat+n+1,cmp);
    for(i=1; i<=n; i++)
        poz[de_sortat[i].second]=i;
    build(1,1,n);
    for(i=1; i<=m; i++)
    {
        cin>>t>>a>>b;
        if(t==0)
            update(1,1,n,poz[a],b);
        else
            cout<<process(a,b)<<'\n';
    }
    return 0;
}