Cod sursa(job #3155948)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 10 octombrie 2023 12:07:17
Problema Heavy Path Decomposition Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.04 kb
#include <fstream>

std::ifstream cin("heavypath.in");
std::ofstream cout("heavypath.out");

const int MAXN = 1e5 + 1;

class SegmenTree {
protected:
    std::vector<int> tree;
    int root, Left, Right;

public:
    void init(const int& size) {
        tree.resize(2 * size);

        root = 1;

        Left = 0;
        Right = size - 1;
    }

    void update(const int& poz, const int& val) {
        updatePoz(root, Left, Right, poz, val);
    }

    int query(const int& l, const int& r) {
        return queryIntv(root, Left, Right, l, r);
    }

private:
    void updatePoz(int node, int left, int right, const int& poz, const int& val) {
        if(left == right) {
            tree[node] = val;
        } else {
            int mid = (left + right) >> 1;

            int L = node + 1;
            int R = node + 2 * (mid - left + 1);

            if(poz <= mid)
                updatePoz(L, left, mid, poz, val);
            else updatePoz(R, mid + 1, right, poz, val);

            tree[node] = std::max(tree[L], tree[R]);
        }
    }

    int queryIntv( int nod, int l, int r, int lq, int rq ) {
        if( r < lq || l > rq )
            return 0;
        if( lq <= l && r <= rq )
            return tree[ nod ];

        int med = ( l + r ) >> 1;
        int R = nod + 2 * ( med - l + 1 );
        int L = nod + 1;

        return std::max( queryIntv( L, l, med, lq, rq ), queryIntv( R, med + 1, r, lq, rq ) );
    }

};

class HeavyMax {
protected:
    int poz;
    SegmenTree maxTree;
    std::vector<int> pos, head;
    std::vector<int> heavy, depth;
    std::vector<int> parent, edges[MAXN];

public:
    void init(const int& size) {
        maxTree.init(size);
        pos.resize(size + 1);
        head.resize(size);
        heavy.resize(size);
        depth.resize(size);
        parent.resize(size);

        poz = 0;

        depth[0] = 0;
        dfs(0, -1);
        decompose(0, -1, 0);
    }

    void addEdge(const int& u, const int& v) {
        edges[u].push_back(v);
        edges[v].push_back(u);
    }

    void update(int u, int val) {
        maxTree.update(pos[u], val);
    }

    int query(const int& u, const int& v) {
        return queryHeavy(u, v);
    }
private:
    int dfs(int u, const int& p) {
        int sizeV;
        int sizeU = 1;
        int maxSize = 0;

        heavy[u] = -1;
        parent[u] = p;
        for(const int& v : edges[u]) 
            if(v != p) {
                depth[v] = depth[u] + 1;
                sizeV = dfs(v, u);
                sizeU += sizeV;

                if(sizeV > maxSize) {
                    maxSize = sizeV;
                    heavy[u] = v;
                }
            }
        return sizeU;
    }

    void decompose(int u, const int& p, int h) {
        head[u] = h;
        pos[u] = poz++;
        if(heavy[u] != -1)
            decompose(heavy[u], u, h);

        for(const int& v : edges[u]) 
            if(v != p && v != heavy[u])
                decompose(v, u, v);
    }

    int queryHeavy(int u, int v) {
        int ans = -1e9;
        while(head[u] != head[v]) {
            if(depth[head[u]] > depth[head[v]])
                std::swap(u, v);
            ans = std::max(ans, maxTree.query(pos[head[v]], pos[v]));
            v = parent[head[v]];
        }
        ans = std::max(ans, maxTree.query(std::min(pos[u], pos[v]), std::max(pos[u], pos[v])));
        return ans;
    }
};


HeavyMax maxy;
int val[MAXN];
int n, q;

void readAndSolveQueryAndPrint() {
    int u, v;
    int type;
    while(q--) {
        cin >> type;
        cin >> u >> v;

        if(type == 0)
            maxy.update(u - 1, v);
        else cout << maxy.query(u - 1, v - 1) << '\n';
    }
}

void readEdgesAndInitHeavy() {
    int u, v;
    for(int i = 1; i < n; i++) {
        cin >> u >> v;
        maxy.addEdge(u - 1, v - 1);
    }
    
    maxy.init(n);
    for( int u = 0; u < n; u++ )
       maxy.update( u, val[ u ] );
}

void readBaseInput() {
    cin >> n >> q;
    for(int i = 0; i < n; i++)
        cin >> val[i];
}

int main()
{
    readBaseInput();
    readEdgesAndInitHeavy();
    readAndSolveQueryAndPrint();

    return 0;
}