Cod sursa(job #1239141)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 8 octombrie 2014 13:24:19
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.08 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";

ifstream fin(infile);
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; }

int N, M, v[MAXN], heavy[MAXN], paths, pathFather[MAXN], pathPos[MAXN], Level[MAXN], pathWhere[MAXN];
vector <int> path[MAXN], arb[MAXN];
Graph g;

void dfs(int node, int father, int actlevel) {
    Level[node] = actlevel;
    heavy[node] = 1;
    int heaviest = -1;
    for(It it = g[node].begin(), fin = g[node].end(); it != fin ; ++ it)
        if(!Level[*it]) {
            dfs(*it, node, actlevel + 1);
            heavy[node] += heavy[*it];
            if(heaviest == -1 || heavy[heaviest] < heavy[*it])
                heaviest = *it;
        }
    if(heaviest == -1) {
        pathWhere[node] = ++ paths;
        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 index) {
    if(st == dr) {
        arb[index][node] = v[path[index][st]];
        return;
    }
    int mid = ((st + dr) >> 1);
    Build(node << 1, st, mid, index);
    Build((node << 1)| 1, mid + 1, dr, index);
    arb[index][node] = max(arb[index][node << 1], arb[index][(node << 1) | 1]);
}

inline void Update(int node, int st, int dr, int a, int index) {
    if(st == dr) {
        arb[index][node] = v[path[index][st]];
        return;
    }
    int mid = ((st + dr) >> 1);
    if(a <= mid)
        Update(node << 1, st, mid, a, index);
    else
        Update((node << 1)| 1, mid + 1, dr, a, index);
    arb[index][node] = max(arb[index][node << 1], arb[index][(node << 1) | 1]);
}

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

int QueryHeavyPath(int x, int y) {
    if(pathWhere[x] == pathWhere[y]) {
        if(pathPos[x] > pathPos[y])
            swap(x, y);
        return Query(1, 0, path[pathWhere[x]].size() - 1, pathPos[x], pathPos[y], pathWhere[x]);
    }
    if(Level[pathFather[pathWhere[x]]] < Level[pathFather[pathWhere[y]]])
        swap(x, y);
    return max(QueryHeavyPath(pathFather[pathWhere[x]], y), Query(1, 0, path[pathWhere[x]].size() - 1, 0, pathPos[x], pathWhere[x]));
}

int main() {
    fin >> N >> M;
    for(int i = 1 ; i <= N ; ++ i)
        fin >> v[i];
    for(int i = 1 ; i <= N - 1; ++ i) {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 0, 1);
    for(int i = 1 ; i <= paths ; ++ i) {
        reverse(path[i].begin(), path[i].end());
        for(int j = 0 ; j < path[i].size() ; ++ j)
            pathPos[path[i][j]] = j;
        arb[i].resize(path[i].size() << 2);
        Build(1, 0, path[i].size() - 1, i);
    }
    for(int i = 1 ; i <= M ; ++ i) {
        int t, x, y;
        fin >> t >> x >> y;
        if(t == 0) {
            v[x] = y;
            Update(1, 0, path[pathWhere[x]].size() - 1, pathPos[x], pathWhere[x]);
        }
        if(t == 1) {
            fout << QueryHeavyPath(x, y) << '\n';
        }

    }
    fin.close();
    fout.close();
    return 0;
}