Cod sursa(job #1132447)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 3 martie 2014 12:43:32
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.4 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], depth[MAXN], heavy[MAXN], numPaths, Parent[MAXN], where[MAXN];
int pathPosition[MAXN];
vector <int> Path[MAXN], arb[MAXN];
Graph G;

void DFs(int Node, int Father, int actLevel) {
    depth[Node] = actLevel;
    heavy[Node] = 1;
    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)
            heaviest = *it;
        else if(heavy[heaviest] < heavy[*it])
            heaviest = *it;
    }
    if(heavy[Node] == 1) {
        where[Node] = ++ numPaths;
        Parent[numPaths] = Father;
        Path[numPaths].push_back(Node);
        return;
    }
    where[Node] = where[heaviest];
    Parent[where[Node]] = Father;
    Path[where[Node]].push_back(Node);
}

void Build(int Node, int st, int dr, int whichPath) {
    if(st == dr) {
        arb[whichPath][Node] = v[Path[whichPath][st]];
        return ;
    }
    int mid = ((st + dr) >> 1);
    Build(2*Node, st, mid, whichPath);
    Build(2*Node + 1, mid + 1, dr, whichPath);
    arb[whichPath][Node] = max(arb[whichPath][2*Node], arb[whichPath][2*Node + 1]);
}

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

int Query(int Node, int st, int dr, int a, int b, int whichPath) { /// return the max on the [a, b] interval
    if(a <= st && dr <= b)
        return arb[whichPath][Node];
    int mid = ((st + dr) >> 1);
    int actMax = -oo;
    if(a <= mid)
        actMax = max(actMax, Query(2*Node, st, mid, a, b, whichPath));
    if(mid < b)
        actMax = max(actMax, Query(2*Node + 1, mid + 1, dr, a, b, whichPath));
    return actMax;
}

int QueryHeavyPath(int x, int y) {
    if(where[x] == where[y]) {
        int pozmin = min(pathPosition[x], pathPosition[y]);
        int pozmax = max(pathPosition[x], pathPosition[y]);
        return Query(1, 0, Path[where[x]].size() - 1, pozmin, pozmax, where[x]);
    }
    if(depth[Parent[where[x]]] < depth[Parent[where[y]]])
        swap(x, y);
    int ret = Query(1, 0, Path[where[x]].size() - 1, 0, pathPosition[x], where[x]);
    return max(ret, QueryHeavyPath(Parent[where[x]], y));
}

int main() {
    fin >> N >> M;
    for(int i = 1 ; i <= N ; ++ i)
        fin >> v[i];
    for(int i = 1 ; i != N ; ++ 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 <= 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;
        fin >> op >> x >> y;
        switch(op) {
        case 0:
            v[x] = y;
            Update(1, 0, Path[where[x]].size() - 1, pathPosition[x], where[x]);
            break;
        case 1:
            fout << QueryHeavyPath(x, y) << '\n';
            break;
        }
    }
    fin.close();
    fout.close();
    return 0;
}