Cod sursa(job #2039788)

Utilizator MaligMamaliga cu smantana Malig Data 14 octombrie 2017 22:24:40
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.94 kb
#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];

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);

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

    /*
    for (int i=1; i <= nrChain;++i) {
        for (int node : chain[i]) {
            cout<<query(1,1,chainDim[i],chainOffset[i],depth[node] - chainDepth[ chainOf[node] ],depth[node] - chainDepth[ chainOf[node] ])<<' ';
        }
    }
    cout<<'\n';
    //*/

    /*
    for (int i=1; i <= N;++i) {
        cout<<depth[i]<<' ';
    }
    cout<<'\n';
    //*/

    /*
    for (int i=1;i <= nrChain;++i) {
        cout<<chainOffset[i]<<' ';
    }
    cout<<'\n';
    //*/

    solveQueries();

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

void read() {

    in>>N>>M;
    for (int i=1;i <= N;++i) {
        in>>value[i];
        //cout<<i<<' ';
    }
    /*
    cout<<'\n';

    for (int i=1;i <= N;++i) {
        cout<<val[i]<<' ';
    }
    cout<<"\n\n";
    //*/

    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());

        chainOffset[i] = chainOffset[i-1] + 4 * chainDim[i-1];
        build(1,1,chainDim[i],i);
    }
}

void dfs(int node) {
    //depth[node] = depth[dad]+1;
    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]) {
            heavy = nxt;
        }
    }

    //cout<<node<<' '<<heavy<<'\n';

    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] ],chainOf[x], depth[x] - depth[ chainOf[x] ], y);
            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;
}