Cod sursa(job #3144340)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 7 august 2023 14:51:32
Problema Heavy Path Decomposition Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.98 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], pos[maxN], head[maxN], currPoz, parent[maxN];
vector <int> G[maxN];
int aint[4 * maxN];

void update(int poz, int val, int nod = 1, int st = 1, int dr = n) {
    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]);
}

int query(int qst, int qdr, int nod = 1, int st = 1, int dr = n) {
    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) {
    parent[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, int cap) {
    head[nod] = cap;
    pos[nod] = ++currPoz;
    int heavySon = 0;
    for (int vecin : G[nod]) {
        if (vecin == tata) {
           continue;
        }
        if (sz[vecin] > sz[heavySon])
            heavySon = vecin;
    }
    if (!heavySon)
        return;
    dfsHeavy(heavySon, nod, cap);
    for (int vecin : G[nod]) {
        if (vecin == tata || vecin == heavySon) {
           continue;
        }
        dfsHeavy(vecin, nod, vecin);
    }
}

int calc(int a, int b) {
    int res = 0;
    for (; head[a] != head[b]; b = parent[head[b]]) {
        if (depth[head[a]] > depth[head[b]])
            swap(a, b);
        int cur_heavy_path_max = query(pos[head[b]], pos[b]);
        res = max(res, cur_heavy_path_max);
    }
    if (depth[a] > depth[b])
        swap(a, b);
    int last_heavy_path_max = query(pos[a], pos[b]);
    res = max(res, last_heavy_path_max);
    return res;
}

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