Cod sursa(job #3144322)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 7 august 2023 13:11:17
Problema Heavy Path Decomposition Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.23 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int maxN = 100005;
int n, q, v[maxN], sz[maxN], depth[maxN], lant[maxN], poz[maxN], cap[maxN], currPoz, currLant, p[maxN], ord[maxN];
vector <int> G[maxN];
int aint[4 * maxN];

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

void build(int nod, int st, int dr) {
    if (st == dr) {
        aint[nod] = v[ord[st]];
        return;
    }
    int med = (st + dr) / 2;
    build(2 * nod, st, med);
    build(2 * nod + 1, med + 1, dr);
    aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}

int query(int qst, int qdr, int nod, int st, int dr) {
    if (st == dr) {
        return aint[nod];
    }
    int med = (st + dr) / 2, maxi = 0;
    if (qst <= med) {
        maxi = max(maxi, query(qst, qdr, 2 * nod, st, med));
    }
    if (med < qdr) {
        maxi = max(maxi, query(qst, qdr, 2 * nod + 1, med + 1, dr));
    }
    return maxi;
}

void dfs(int nod, int tata) {
    p[nod] = tata;
    sz[nod] = 1;
    depth[nod] = depth[tata] + 1;
    for (int vecin : G[nod]) {
        if (vecin == tata) {
            continue;
        }
        dfs(vecin, nod);
        sz[nod] += sz[vecin];
    }
}

void dfsHeavy(int nod, int tata, bool heavy) {
    if (heavy) {
        lant[nod] = lant[tata];
    }
    else {
        lant[nod] = ++currLant;
        cap[currLant] = nod;
    }
    poz[nod] = ++currPoz;
    ord[currPoz] = nod;
    int heavySon = 0;
    for (int vecin : G[nod]) {
        if (vecin == tata) {
           continue;
        }
        if (sz[vecin] > sz[heavySon])
            heavySon = vecin;
    }
    if (heavySon)
        dfsHeavy(heavySon, nod, 1);
    for (int vecin : G[nod]) {
        if (vecin == tata) {
           continue;
        }
        if (vecin == heavySon) {
            continue;
        }
        dfsHeavy(vecin, nod, 0);
    }
}

int calc(int x, int y) {
    if (lant[x] == lant[y]) {
        if (poz[x] > poz[y])
            swap(x, y);
        return query(poz[x], poz[y], 1, 1, n);
    }
    if (depth[cap[lant[x]]] < depth[cap[lant[y]]])
        swap(x, y);
    return max(query(poz[cap[lant[x]]], poz[x], 1, 1, n), calc(p[cap[lant[x]]], y));
}

int main() {
    fin >> n >> q;
    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);
    }
    dfs(1, 0);
    dfsHeavy(1, 0, 1);
    build(1, 1, n);
    for (int i = 1; i <= q; i++) {
        int op;
        fin >> op;
        if (op == 0) {
            int nod, val;
            fin >> nod >> val;
            update(poz[nod], val, 1, 1, n);
        }
        if (op == 1) {
            int x, y;
            fin >> x >> y;
            fout << calc(x, y) << '\n';
        }
    }
    return 0;
}