Cod sursa(job #2416143)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 26 aprilie 2019 23:32:21
Problema Heavy Path Decomposition Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.52 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

const string file = "heavypath";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647, nmax = 100005;

int n, test, p, p2, c[nmax], sz[nmax], where[nmax], poschain[nmax], dep[nmax], e[nmax*2], first[nmax], tt[nmax];
vector<int> v[nmax], chain[nmax], aintchain[nmax];

void dfs(int nod, int pred = 0)
{
    tt[nod] = pred;
    sz[nod] = 1;
    dep[nod] = dep[pred]+1;
    e[++p2] = nod;
    first[nod] = p2;
    int mx = -1;
    for (auto x : v[nod])
        if(x != pred){
            dfs(x, nod);
            e[++p2] = nod;
            sz[nod] += sz[x];
            mx = max(mx, sz[x]);
        }
    if(sz[nod] == 1){
        ++p;
        chain[p].push_back(0);
        chain[p].push_back(nod);
        where[nod] = p;
        poschain[nod] = 1;
        return;
    }
    for (auto x : v[nod])
        if(sz[x] == mx && x != pred){
            where[nod] = where[x];
            poschain[nod] = chain[where[x]].size();
            chain[where[x]].push_back(nod);
            break;
        }
}

void buildchain(int nod, int st, int dr, int which)
{
    if(aintchain[which].empty())
        aintchain[which].resize(chain[which].size()*4);
    if(st == dr)
        aintchain[which][nod] = c[chain[which][st]];
    else{
        int mid = (st+dr)/2;
        buildchain(nod*2, st, mid, which);
        buildchain(nod*2+1, mid+1, dr, which);
        aintchain[which][nod] = max(aintchain[which][nod*2], aintchain[which][nod*2+1]);
    }
}

void updatechain(int nod, int st, int dr, int pos, int val, int which)
{
    if(st == dr)
        aintchain[which][nod] = val;
    else{
        int mid = (st+dr)/2;
        if(mid >= pos)
            updatechain(nod*2, st, mid, pos, val, which);
        else updatechain(nod*2+1, mid+1, dr, pos, val, which);
        aintchain[which][nod] = max(aintchain[which][nod*2], aintchain[which][nod*2+1]);
    }
}

int querychain(int nod, int st, int dr, int l, int r, int which)
{
    if(st == dr)
        return aintchain[which][nod];
    int mid = (st+dr)/2, ans = 0;
    if(mid >= l)
        ans = querychain(nod*2, st, mid, l, r, which);
    if(mid+1 <= r)
        ans = max(ans, querychain(nod*2+1, mid+1, dr, l, r, which));
    return ans;
}

int hpd(int a, int b)
{
    int ans = max(c[a], c[b]);
    while(a != b){
        if(dep[tt[chain[where[a]].back()]] < dep[tt[chain[where[b]].back()]])
            swap(a, b);
        if(where[a] == where[b]){
            if(dep[a] < dep[b])
                swap(a, b);
            ans = max(ans, querychain(1, 1, chain[where[a]].size()-1, poschain[a], poschain[b], where[a]));
            a = b;
        }else{
            ans = max(ans, querychain(1, 1, chain[where[a]].size()-1, poschain[a], chain[where[a]].size()-1, where[a]));
            a = tt[chain[where[a]].back()];
        }
    }
    return max(c[a], ans);
}

int main()
{
    ifstream fin (file+".in");
    ofstream fout (file+".out");
    fin >> n >> test;
    for (int i = 1; i <= n; ++i)
        fin >> c[i];
    for (int i = 1; i < n; ++i){
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1);
    for (int i = 1; i <= p; ++i)
        buildchain(1, 1, chain[i].size()-1, i);
    while(test--){
        int t, x, y;
        fin >> t >> x >> y;
        if(t == 0){
            updatechain(1, 1, chain[where[x]].size()-1, poschain[x], y, where[x]);
            c[x] = y;
        }else fout << hpd(x, y) << "\n";
    }
    return 0;
}