Cod sursa(job #2039960)

Utilizator BogdanisarBurcea Bogdan Madalin Bogdanisar Data 15 octombrie 2017 11:31:32
Problema Heavy Path Decomposition Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 5.43 kb
//Longest Path Decomposition, O(N * sqrt(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], dist[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;
// dist[i] - lungime (in noduri) a celui mai lung drum care porneste din nodul i si se termina intr-o frunza;
// 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;

void read();
void getChains();
void doOperations();
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();
    doOperations();

    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) {
    bool leaf = true;
    int son = 0;

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

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

        leaf = false;
        if (!son) {
            son = nxt;
        }
        else if (dist[son] < dist[nxt]) { // se cauta lantul maxim;
            son = nxt;
        }
    }

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

        return;
    }

    dist[node] = dist[son] + 1;
    chain[ chainOf[son] ].pb(node);
    chainOf[node] = chainOf[son];
    ++chainDim[ chainOf[node] ];

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

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

void doOperations() {
    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;
}