Cod sursa(job #3041994)

Utilizator lolismekAlex Jerpelea lolismek Data 3 aprilie 2023 14:14:09
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.89 kb
#include <iostream>
#include <fstream>
#include <vector>

#define pii pair <int, int>

using namespace std;

string filename = "heavypath";
#ifdef LOCAL
    ifstream fin("input.in");
    ofstream fout("output.out");
#else
    ifstream fin(filename + ".in");
    ofstream fout(filename + ".out");
#endif

const int NMAX = 1e5;

int n, q;
vector <int> adj[NMAX + 1];

int v[NMAX + 1];

int sz[NMAX + 1];
int level[NMAX + 1];
int parent[NMAX + 1];

int heavyChild[NMAX + 1];

void explore(int node, int Parent){
    sz[node] = 1;
    level[node] = level[Parent] + 1;
    parent[node] = Parent;

    for(int child : adj[node]){
        if(child != Parent){
            explore(child, node);
            sz[node] += sz[child];

            if(sz[child] > sz[heavyChild[node]]){
                heavyChild[node] = child;
            }
        }
    }
}

int lin[NMAX + 1];
pii pos[NMAX + 1];

int head[NMAX + 1];
int id[NMAX + 1];

int dfsTime = 0;
int Id = 0;

void heavyFirst(int node, int Parent){
    ++dfsTime;
    lin[dfsTime] = node;
    pos[node].first = dfsTime;

    if(heavyChild[Parent] == node){
        id[node] = id[Parent];
    }else{
        id[node] = ++Id;
        head[Id] = node;
    }


    if(heavyChild[node] != 0){
        heavyFirst(heavyChild[node], node);
    }

    for(int child : adj[node]){
        if(child != heavyChild[node] && child != Parent){
            heavyFirst(child, node);
        }
    }

    pos[node].second = dfsTime;
}

namespace AINT{
    int aint[4 * NMAX + 1];

    void build(int node, int left, int right){
        if(left == right){
            aint[node] = v[lin[left]];
            return;
        }

        int mid = (left + right) / 2;
        build(2 * node, left, mid);
        build(2 * node + 1, mid + 1, right);

        aint[node] = max(aint[2 * node], aint[2 * node + 1]);
    }

    void update(int node, int left, int right, int pos, int val){
        if(left == right){
            aint[node] = val;
            return;
        }

        int mid = (left + right) / 2;
        if(pos <= mid){
            update(2 * node, left, mid, pos, val);
        }else{
            update(2 * node + 1, mid + 1, right, pos, val);
        }

        aint[node] = max(aint[2 * node], aint[2 * node + 1]);
    }

    int query(int node, int left, int right, int qleft, int qright){
        if(qleft <= left && right <= qright){
            return aint[node];
        }

        int mid = (left + right) / 2, ans = 0;
        if(qleft <= mid){
            ans = max(ans, query(2 * node, left, mid, qleft, qright));
        }
        if(mid + 1 <= qright){
            ans = max(ans, query(2 * node + 1, mid + 1, right, qleft, qright));
        }

        return ans;
    }
}

int query(int a, int b){
    int ans = 0;
    while(id[a] != id[b]){
        if(level[head[id[a]]] > level[head[id[b]]]){
            ans = max(ans, AINT::query(1, 1, n, pos[head[id[a]]].first, pos[a].first));
            a = parent[head[id[a]]];
        }else{
            ans = max(ans, AINT::query(1, 1, n, pos[head[id[b]]].first, pos[b].first));
            b = parent[head[id[b]]];
        }
    }

    ans = max(ans, AINT::query(1, 1, n, min(pos[a].first, pos[b].first), max(pos[a].first, pos[b].first)));
    return ans;
}

int main(){

    fin >> n >> q;

    for(int i = 1; i <= n; i++){
        fin >> v[i];
    }

    for(int i = 1; i <= n - 1; i++){
        int a, b;
        fin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    explore(1, 0);
    heavyFirst(1, 0);

    AINT::build(1, 1, n);

    while(q--){
        int type, x, y;
        fin >> type >> x >> y;

        if(type == 0){
            AINT::update(1, 1, n, pos[x].first, y);
        }else{
            fout << query(x, y) << '\n';
        }
    }

    return 0;
}