Cod sursa(job #2374871)

Utilizator caesar2001Stoica Alexandru caesar2001 Data 7 martie 2019 21:01:01
Problema Heavy Path Decomposition Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.55 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <iomanip>
#define ll long long
#define lsb(x) (x & -x)

using namespace std;

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

const int NMAX = 100005;

vector<int> g[NMAX];
int v[NMAX], h[NMAX], dim[NMAX], dad[NMAX];
vector<int> path[NMAX];
int boss[NMAX], npaths, color[NMAX], pos[NMAX];

void dfs(int node, int d) {
    dim[node] = 1;
    int mx = 0, from = 0;

    for(auto it : g[node]) {
        if(it == d)
            continue;

        h[it] = h[node] + 1;
        dad[it] = node;
        dfs(it, node);
        dim[node] += dim[it];

        if(dim[it] > mx) {
            mx = dim[it];
            from = color[it];
        }
    }

    if(dim[node] > 1) {
        pos[node] = path[from].size();
        path[from].push_back(node);
        color[node] = from;
        boss[from] = node;
    } else {
        npaths ++;
        pos[node] = 0;
        path[npaths].push_back(node);
        boss[npaths] = node;
        color[node] = npaths;
    }
}

vector<vector<int>> aint;

void buildtree(int p, int node, int le, int ri) {
    if(le == ri)
        aint[p][node] = v[path[p][le]];
    else {
        int mid = (le + ri) / 2;
        buildtree(p, node * 2, le, mid);
        buildtree(p, node * 2 + 1, mid + 1, ri);

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

void update(int i, int pos, int val, int node, int le, int ri) {
    if(le == ri)
        aint[i][node] = val;
    else {
        int mid = (le + ri) / 2;
        if(pos <= mid)
            update(i, pos, val, node * 2, le, mid);
        else
            update(i, pos, val, node * 2 + 1, mid + 1, ri);
        aint[i][node] = max(aint[i][node * 2], aint[i][node * 2 + 1]);
    }
}

int query(int i, int from, int to, int node, int le, int ri) {
    if(from <= le && ri <= to)
        return aint[i][node];

    int mid = (le + ri) / 2;
    int ans = 0;
    if(from <= mid)
        ans = query(i, from, to, node * 2, le, mid);
    if(mid < to)
        ans = max(ans, query(i, from, to, node * 2 + 1, mid + 1, ri));

    return ans;
}

int main() {

    int n, m;
    in >> n >> m;
    for(int i = 1; i <= n; i ++)
        in >> v[i];
    for(int i = 1; i < n; i ++) {
        int x, y;
        in >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    h[1] = 1;
    dfs(1, 0);

    aint.resize(npaths + 1);
    for(int i = 1; i <= npaths; i ++) {
        aint[i].resize(4 * path[i].size() + 5);
        buildtree(i, 1, 0, path[i].size() - 1);
    }

    while(m --) {
        int type, x, y;
        in >> type >> x >> y;
        if(type == 0) {
            update(color[x], pos[x], y, 1, 0, path[color[x]].size() - 1);
        } else if(type == 1) {
            int ans = 0;

            while(color[x] != color[y]) {
                if(h[boss[color[x]]] < h[boss[color[y]]])
                    swap(x, y);

                ans = max(ans, query(color[x], pos[x], path[color[x]].size() - 1, 1, 0, path[color[x]].size() - 1));
                x = boss[color[x]];
                x = dad[x];
            }

            if(h[x] < h[y])
                swap(x, y);
            ans = max(ans, query(color[x], pos[x], pos[y], 1, 0, path[color[x]].size() - 1));

            out << ans << "\n";
        }
    }

    return 0;
}