Cod sursa(job #2039971)

Utilizator BogdanisarBurcea Bogdan Madalin Bogdanisar Data 15 octombrie 2017 11:37:02
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.48 kb
//Heavy Path Decomposition, O(N * log N * log N);

#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;
ifstream in("heavypath.in");
ofstream out("heavypath.out");

#define ll long long
#define ull unsigned long long
#define pb push_back
const int NMax = 1e5 + 5;
const int arbMax = 4*NMax;

int N,M,nrChain;
int value[NMax],depth[NMax], sub[NMax], chainOf[NMax],
    chainOffset[NMax], chainDim[NMax], chainDad[NMax], chainDepth[NMax],
    aint[arbMax];
vector<int> v[NMax],chain[NMax];
// value[i] - valoarea initiala a nodului i;
// depth[i] - adancimea nodului i in arbore;
// sub[i] - numarul de noduri din subarborele nodului i;
// chainOf[i] - indexul lantului in care se afla nodul i;
// chainOffset[i] - un offset pentru lantul cu index i,
// important pentru a putea reprezenta arborele de intervale al fiecarui lant in vectorul aint;
// chainDim[i] - numarul de noduri in lantul i;
// chainDad[i] - nodul tata pentru lantul i;
// chainDepth[i] - adancimea lantului (a nodului tata) pentru lantul i;
// aint - vector in care se reprezinta arborii de intervale pentru fiecare lant;
// v[i] - lista de adiacenta a nodului i;
// chain[i] - lista cu nodurile din lantul i;

void read();
void getChains();
void solveQueries();
void dfs(int);
void build(int,int,int,int);
void update(int,int,int,int,int,int);
int query(int,int,int,int,int,int);
// read - citeste valorile din input;
// dfs - creeaza lanturile printr-o parcurgere in adancime;
// getChains - finiseaza lanturile obtinute prin functia dfs;
// doOperations() - realizeaza fiecare din cele M operatii;
// build(node,st,dr,id) - initializeaza valorea nodurilor din aint corespunzatoare lantului id;
// update(node,st,dr,off,pos,val) - schimba valorea unui nod frunza din aint;
// query(node,st,dr,off,a,b) - return-eaza maximul pe un interval de noduri din acelasi lant;

int main() {
    read();
    getChains();
    solveQueries();

    in.close();out.close();
    return 0;
}

void read() {
    in>>N>>M;

    for (int i=1;i <= N;++i) {
        in>>value[i];
    }

    for (int i=1;i < N;++i) {
        int x,y;
        in>>x>>y;

        v[x].pb(y);
        v[y].pb(x);
    }

}

void getChains() {
    depth[1] = 1;
    dfs(1);

    for (int i=1;i <= nrChain;++i) {
        reverse(chain[i].begin(),chain[i].end());

        // 4 * dim[i] - asigura suficient spatiu pentru a reprezenta lantul i;
        chainOffset[i] = chainOffset[i-1] + 4 * chainDim[i-1];
        build(1,1,chainDim[i],i);
    }
}

void dfs(int node) {
    sub[node] = 1;
    bool leaf = true;
    int heavy = -1;

    for (int nxt : v[node]) {
        if (depth[nxt]) {
            continue;
        }

        depth[nxt] = depth[node] + 1;
        dfs(nxt);
        sub[node] += sub[nxt];

        leaf = false;
        if (!heavy) {
            heavy = nxt;
        }
        else if (sub[heavy] < sub[nxt]) { // se cauta nodul cu subarborele maximal;
            heavy = nxt;
        }
    }

    if (leaf) {
        chain[++nrChain].pb(node);
        chainOf[node] = nrChain;
        chainDim[nrChain] = 1;
        return;
    }

    chain[ chainOf[heavy] ].pb(node);
    chainOf[node] = chainOf[heavy];
    ++chainDim[ chainOf[node] ];

    for (int nxt : v[node]) {
        if (depth[nxt] < depth[node] || nxt == heavy) {
            continue;
        }

        chainDad[ chainOf[nxt] ] = node;
        chainDepth[ chainOf[nxt] ] = depth[node];
    }
}

void solveQueries() {
    while (M--) {
        int tip,x,y;
        in>>tip>>x>>y;

        if (!tip) {
            update(1,1,chainDim[ chainOf[x] ],chainOffset[ chainOf[x] ], depth[x] - chainDepth[ chainOf[x] ], y);
        }
        else {
            int mx = -1;

            while (chainOf[x] != chainOf[y]) {
                if (chainDepth[ chainOf[x] ] < chainDepth[ chainOf[y] ]) {
                    swap(x,y);
                }

                mx = max(mx, query(1,1,chainDim[ chainOf[x] ], chainOffset[ chainOf[x] ], 1, depth[x] - chainDepth[ chainOf[x] ]));
                x = chainDad[ chainOf[x] ];
            }

            if (depth[x] > depth[y]) {
                swap(x,y);
            }

            mx = max(mx, query(1,1,chainDim[ chainOf[x] ], chainOffset[ chainOf[x] ], depth[x] - chainDepth[ chainOf[x] ], depth[y] - chainDepth[ chainOf[x] ]));
            out<<mx<<'\n';
        }
    }
}

#define mij ((st+dr)>>1)
#define fs (node<<1)
#define ss (fs+1)
void build(int node,int st,int dr,int id) {
    if (st == dr) {
        aint[node + chainOffset[id]] = value[ chain[id][st-1] ];
        return;
    }

    build(fs,st,mij,id);
    build(ss,mij+1,dr,id);

    aint[node + chainOffset[id]] = max(aint[fs + chainOffset[id]],aint[ss + chainOffset[id]]);
}

void update(int node,int st,int dr,int off,int pos,int value) {
    if (st == dr) {
        aint[node + off] = value;
        return;
    }

    if (pos <= mij) {
        update(fs,st,mij,off,pos,value);
    }
    else {
        update(ss,mij+1,dr,off,pos,value);
    }

    aint[node + off] = max(aint[fs + off],aint[ss + off]);
}

int query(int node,int st,int dr,int off,int a,int b) {
    if (a <= st && dr <= b) {
        return aint[node + off];
    }

    int ret = -1;
    if (a <= mij) {
        ret = query(fs,st,mij,off,a,b);
    }
    if (mij+1 <= b) {
        ret = max(ret,query(ss,mij+1,dr,off,a,b));
    }

    return ret;
}