Cod sursa(job #3372298)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 3 noiembrie 2026 00:37:47
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.85 kb
#include <bits/stdc++.h>
using namespace std;

template<typename T, typename OpType>
class SegmentTree{
private:
    const int N;
    const OpType AGGREGATE;
    const T IDENTITY_ELEMENT;
    vector<T> tree;

    void update(int node, int l, int r, const int POS, const T VAL){
        if(l == r){
            tree[node] = VAL;
        }else{
            int mid = (l + r) / 2;
            if(POS <= mid){
                update(2 * node + 1, l, mid, POS, VAL);
            }else{
                update(2 * node + 2, mid + 1, r, POS, VAL);
            }
            tree[node] = AGGREGATE(tree[2 * node + 1], tree[2 * node + 2]);
        }
    }

    T query(int node, int l, int r, const int L, const int R){
        if(R < l || r < L){
            return IDENTITY_ELEMENT;
        }
        if(L <= l && r <= R){
            return tree[node];
        }
        int mid = (l + r) / 2;
        return AGGREGATE(query(2 * node + 1, l, mid, L, R),
                         query(2 * node + 2, mid + 1, r, L, R));
    }

public:
    SegmentTree(const int N, const OpType AGGREGATE, const T IDENTITY_ELEMENT):
                N(N), AGGREGATE(AGGREGATE), IDENTITY_ELEMENT(IDENTITY_ELEMENT){
        int minLeaves = 1;
        while(minLeaves < N){
            minLeaves *= 2;
        }
        tree.assign(2 * minLeaves, IDENTITY_ELEMENT);
    }

    void update(const int POS, const T VAL){
        update(0, 0, N - 1, POS, VAL);
    }

    T query(const int L, const int R){
        if(0 <= L && L <= R && R < N){
            return query(0, 0, N - 1, L, R);
        }
        return IDENTITY_ELEMENT;
    }
};

template<typename T, typename OpType>
class HLD{
private:
    const int N;
    const OpType AGGREGATE;
    const T IDENTITY_ELEMENT;
    SegmentTree<T, OpType> segTree;
    vector<int> parentOf;
    vector<int> heavyOf;
    vector<int> depthOf;
    vector<int> indexOf;
    vector<int> topOf;

    int dfs1(int parent, int node, const vector<vector<int>>& G){
        int subtreeSize = 1;
        int maxChildSize = 0;
        for(int child: G[node]){
            if(child != parent){
                parentOf[child] = node;
                depthOf[child] = depthOf[node] + 1;
                int childSize = dfs1(node, child, G);
                if(maxChildSize < childSize){
                    maxChildSize = childSize;
                    heavyOf[node] = child;
                }
                subtreeSize += childSize;
            }
        }
        if(subtreeSize == 1){
            heavyOf[node] = -1;
        }
        return subtreeSize;
    }

    void dfs2(int node, vector<int>& v, const vector<vector<int>>& G){
        indexOf[node] = v.size();
        v.push_back(node);
        if(heavyOf[node] != -1){
            dfs2(heavyOf[node], v, G);
        }
        for(int child: G[node]){
            if(child != heavyOf[node] && child != parentOf[node]){
                dfs2(child, v, G);
            }
        }
    }

public:
    HLD(const int N,  const OpType AGGREGATE, const T IDENTITY_ELEMENT, const vector<vector<int>>& G):
        N(N), AGGREGATE(AGGREGATE), IDENTITY_ELEMENT(IDENTITY_ELEMENT),
        segTree(N, AGGREGATE, IDENTITY_ELEMENT), parentOf(N), heavyOf(N), depthOf(N), indexOf(N), topOf(N){

        dfs1(-1, 0, G);

        vector<int> v;
        dfs2(0, v, G);

        int currTop = 0;
        for(int i = 0; i < N; ++i){
            if(i == 0 || heavyOf[v[i - 1]] != v[i]){
                currTop = v[i];
            }
            topOf[v[i]] = currTop;
        }
    }

    void update(int node, T val){
        segTree.update(indexOf[node], val);
    }

    T query(int a, int b){
        T res = IDENTITY_ELEMENT;
        while(topOf[a] != topOf[b]){
            if(depthOf[topOf[a]] < depthOf[topOf[b]]){
                swap(a, b);
            }
            res = AGGREGATE(res, segTree.query(indexOf[topOf[a]], indexOf[a]));
            a = parentOf[topOf[a]];
        }

        int l = min(indexOf[a], indexOf[b]);
        int r = max(indexOf[a], indexOf[b]);
        res = AGGREGATE(res, segTree.query(l, r));

        return res;
    }
};

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

    int N, M;
    cin >> N >> M;

    vector<int> v(N);
    for(int i = 0; i < N; ++i){
        cin >> v[i];
    }

    vector<vector<int>> G(N);
    for(int i = 0; i < N - 1; ++i){
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    auto maxFunction = [](int a, int b) -> int { return max(a, b); };
    HLD<int, decltype(maxFunction)> hld(N, maxFunction, 0, G);

    for(int i = 0; i < N; ++i){
        hld.update(i, v[i]);
    }

    for(int i = 0; i < M; ++i){
        int t, x, y;
        cin >> t >> x >> y;

        if(t == 0){
            hld.update(x - 1, y);
        }else{
            cout << hld.query(x - 1, y - 1) << '\n';
        }
    }

    cin.close();
    cout.close();
    return 0;
}