Cod sursa(job #1165526)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 aprilie 2014 19:00:02
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.67 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "heavypath.in";
const char outfile[] = "heavypath.out";

ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

const int lim = (1 << 20);
char buff[lim];
int pos;

inline void get(int &x) {
    x = 0;
    while(!('0' <= buff[pos] && buff[pos] <= '9'))
        if(++ pos == lim) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    }
}

int N, M, v[MAXN];
int numPaths, pathWhere[MAXN], pathFather[MAXN], pathPosition[MAXN];
int depth[MAXN], heavy[MAXN];
Graph G, arb, Path;

inline void DFs(int Node, int Father, int actLevel) {
    heavy[Node] = 1;
    depth[Node] = actLevel;
    int heaviest = -1;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
        if(*it == Father)
            continue;
        DFs(*it, Node, actLevel + 1);
        heavy[Node] += heavy[*it];
        if(heaviest == -1 || heavy[*it] > heavy[heaviest])
            heaviest = *it;
    }
    if(heaviest == -1) {
        pathWhere[Node] = ++ numPaths;
        Path[pathWhere[Node]].push_back(Node);
        pathFather[pathWhere[Node]] = Father;
        return;
    }
    pathWhere[Node] = pathWhere[heaviest];
    Path[pathWhere[Node]].push_back(Node);
    pathFather[pathWhere[Node]] = Father;
}

inline void Build(int Node, int st, int dr, int where) {
    if(st == dr) {
        arb[where][Node] = v[Path[where][st]];
        return;
    }
    int mid = ((st + dr) >> 1);
    Build(Node << 1, st, mid, where);
    Build((Node << 1) | 1, mid+1, dr, where);
    arb[where][Node] = max(arb[where][Node << 1], arb[where][(Node << 1) | 1]);
}


inline void Update(int Node, int st, int dr, int pos, int where) {
    if(st == dr) {
        arb[where][Node] = v[Path[where][st]];
        return;
    }
    int mid = ((st + dr) >> 1);
    if(pos <= mid)
        Update(Node << 1, st, mid, pos, where);
    else
        Update((Node << 1) | 1, mid+1, dr, pos, where);
    arb[where][Node] = max(arb[where][Node << 1], arb[where][(Node << 1) | 1]);
}

inline int Query(int Node, int st, int dr, int a, int b, int where) {
    if(a <= st && dr <= b)
        return arb[where][Node];
    int mid = ((st + dr) >> 1);
    int ans = -oo;
    if(a <= mid)
        ans = max(ans, Query(Node << 1, st, mid, a, b, where));
    if(mid < b)
        ans = max(ans, Query((Node << 1) | 1, mid+1, dr, a, b, where));
    return ans;
}

inline int QueryHeavyPath(int x, int y) {
    if(pathWhere[x] == pathWhere[y]) {
        if(pathPosition[x] > pathPosition[y])
            swap(x, y);
        return Query(1, 0, Path[pathWhere[x]].size() - 1, pathPosition[x], pathPosition[y], pathWhere[x]);
    }
    if(depth[pathFather[pathWhere[x]]] < depth[pathFather[pathWhere[y]]])
        swap(x, y);
    int act = Query(1, 0, Path[pathWhere[x]].size() - 1, 0, pathPosition[x], pathWhere[x]);
    return max(act, QueryHeavyPath(pathFather[pathWhere[x]], y));
}

int main() {
    freopen(infile, "r", stdin);
    get(N); get(M);
    for(int i = 1 ; i <= N ; ++ i)
        get(v[i]);
    for(int i = 1 ; i < N ; ++ i) {
        int x, y;
        get(x);
        get(y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFs(1, 0, 1);
    for(int i = 1 ; i <= numPaths ; ++ i) {
        reverse(Path[i].begin(), Path[i].end());
        for(int j = 0 ; j < int(Path[i].size()) ; ++ j)
            pathPosition[Path[i][j]] = j;
        arb[i].resize(4*Path[i].size());
        Build(1, 0, Path[i].size() - 1, i);
    }
    for(int i = 1 ; i <= M ; ++ i) {
        int op, x, y;
        get(op);
        get(x);
        get(y);
        switch(op) {
            case 0:
                v[x] = y;
                Update(1, 0, Path[pathWhere[x]].size() - 1, pathPosition[x], pathWhere[x]);
                break;
            case 1:
                fout << QueryHeavyPath(x, y) << '\n';
                break;
        }
    }
    fout.close();
    return 0;
}