Cod sursa(job #1243519)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 15 octombrie 2014 23:40:56
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.14 kb
#include <bits/stdc++.h>

using namespace std;

struct SegmentTree
{
public:

    SegmentTree(const vector<int>& value)
    {
        N = (int)value.size();
        A = vector<int>( 4 * N, 0 );

        build( 1, 0, N - 1, value );
    }

    void update( int pos, int val )
    {
        update( 1, 0, N - 1, pos, val );
    }

    int query( int x, int y )
    {
        return query( 1, 0, N - 1, x, y );
    }

private:

    vector <int> A;
    int N;

    void build( int nod, int st, int dr, const vector<int>& value )
    {
        if ( st == dr )
            A[nod] = value[st];
        else
        {
            int m = ( st + dr ) / 2;

            build( 2 * nod, st, m, value );
            build( 2 * nod + 1, m + 1, dr, value );

            A[nod] = max( A[2 * nod], A[2 * nod + 1] );
        }
    }

    void update( int nod, int st, int dr, int pos, int val )
    {
        if ( st == dr )
            A[nod] = val;
        else
        {
            int m = ( st + dr ) / 2;

            if ( pos <= m )
                update( 2 * nod, st, m, pos, val );
            else
                update( 2 * nod + 1, m + 1, dr, pos, val );

            A[nod] = max( A[2 * nod], A[2 * nod + 1] );
        }
    }

    int query( int nod, int st, int dr, int st_q, int dr_q )
    {
        if ( st_q <= st && dr <= dr_q )
            return A[nod];
        else
        {
            int sol = 0;
            int m = ( st + dr ) / 2;

            if ( st_q <= m )
                sol = max( sol, query( 2 * nod, st, m, st_q, dr_q ) );

            if ( m < dr_q )
                sol = max( sol, query( 2 * nod + 1, m + 1, dr, st_q, dr_q ) );

            return sol;
        }
    }
};

const int Nmax = 1e5 + 1;

vector <SegmentTree> ST;
vector <int> G[Nmax];
int path[Nmax], pos_in_path[Nmax], length_path[Nmax], start_node[Nmax];
int father[Nmax], size[Nmax], depth[Nmax], value[Nmax];

int N, Q, NumberPaths;

void DFS( int nod )
{
    int hson = 0;
    size[nod] = 1;

    for ( auto x: G[nod] )
    {
        if ( father[x] == 0 )
        {
            father[x] = nod;
            depth[x] = depth[nod] + 1;
            DFS( x );

            size[nod] += size[x];

            if ( size[x] > size[hson] )
                hson = x;
        }
    }

    if ( hson == 0 )
        path[nod] = NumberPaths++;
    else
        path[nod] = path[hson];

    pos_in_path[nod] = length_path[ path[nod] ]++;
}

void build_heavy_paths()
{
    father[1] = 1;
    depth[1] = 0;
    DFS( 1 );

    for ( int i = 1; i <= N; ++i )
    {
        pos_in_path[i] = length_path[ path[i] ] - pos_in_path[i] - 1;

        if ( pos_in_path[i] == 0 )
            start_node[ path[i] ] = i;
    }
}

void build_segment_trees()
{
    vector < vector<int> > pathValues = vector < vector<int> >( NumberPaths );

    for ( int i = 0; i < NumberPaths; ++i )
        pathValues[i] = vector<int>( length_path[i] );

    for ( int i = 1; i <= N; ++i )
        pathValues[ path[i] ][ pos_in_path[i] ] = value[i];

    for ( int i = 0; i < NumberPaths; ++i )
        ST.push_back( SegmentTree( pathValues[i] ) );
}

void update( int node, int val )
{
    value[node] = val;
    ST[ path[node] ].update( pos_in_path[node], val );
}

int query( int x, int y )
{
    if ( depth[ start_node[ path[x] ] ] < depth[ start_node[ path[y] ] ] )
        swap( x, y );

    if ( path[x] == path[y] )
        return ST[ path[x] ].query( min( pos_in_path[x], pos_in_path[y] ), max( pos_in_path[x], pos_in_path[y] ) );
    else
        return max( ST[ path[x] ].query( 0, pos_in_path[x] ), query( father[ start_node[ path[x] ] ], y ) );
}

int main()
{
    ifstream in("heavypath.in");
    ofstream out("heavypath.out");

    in.sync_with_stdio( false );

    in >> N >> Q;

    for ( int i = 1; i <= N; ++i )
        in >> value[i];

    for ( int i = 1, a, b; i < N; ++i )
    {
        in >> a >> b;

        G[a].push_back( b );
        G[b].push_back( a );
    }

    build_heavy_paths();
    build_segment_trees();

    for ( int i = 1, tip, a, b; i <= Q; ++i )
    {
        in >> tip >> a >> b;

        if ( tip == 0 )
        {
            update( a, b );
        }
        else
        {
            out << query( a, b ) << "\n";
        }
    }

    return 0;
}