Cod sursa(job #2416148)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 26 aprilie 2019 23:44:57
Problema Heavy Path Decomposition Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 5.21 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

const string file = "heavypath";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647, nmax = 100005;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if(vu32 == -1){
            write_ch('-');
            write_ch('1');
        } else if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};


int n, test, p, p2, c[nmax], sz[nmax], where[nmax], poschain[nmax], dep[nmax], tt[nmax];
vector<int> v[nmax], chain[nmax], aintchain[nmax];

void dfs(int nod, int pred = 0)
{
    tt[nod] = pred;
    sz[nod] = 1;
    dep[nod] = dep[pred]+1;
    int mx = -1;
    for (auto x : v[nod])
        if(x != pred){
            dfs(x, nod);
            sz[nod] += sz[x];
            mx = max(mx, sz[x]);
        }
    if(sz[nod] == 1){
        ++p;
        chain[p].push_back(0);
        chain[p].push_back(nod);
        where[nod] = p;
        poschain[nod] = 1;
        return;
    }
    for (auto x : v[nod])
        if(sz[x] == mx && x != pred){
            where[nod] = where[x];
            poschain[nod] = chain[where[x]].size();
            chain[where[x]].push_back(nod);
            break;
        }
}

void buildchain(int nod, int st, int dr, int which)
{
    if(aintchain[which].empty())
        aintchain[which].resize(chain[which].size()*4);
    if(st == dr)
        aintchain[which][nod] = c[chain[which][st]];
    else{
        int mid = (st+dr)/2;
        buildchain(nod*2, st, mid, which);
        buildchain(nod*2+1, mid+1, dr, which);
        aintchain[which][nod] = max(aintchain[which][nod*2], aintchain[which][nod*2+1]);
    }
}

void updatechain(int nod, int st, int dr, int pos, int val, int which)
{
    if(st == dr)
        aintchain[which][nod] = val;
    else{
        int mid = (st+dr)/2;
        if(mid >= pos)
            updatechain(nod*2, st, mid, pos, val, which);
        else updatechain(nod*2+1, mid+1, dr, pos, val, which);
        aintchain[which][nod] = max(aintchain[which][nod*2], aintchain[which][nod*2+1]);
    }
}

int querychain(int nod, int st, int dr, int l, int r, int which)
{
    if(st >= l && dr <= r)
        return aintchain[which][nod];
    int mid = (st+dr)/2, ans = 0;
    if(mid >= l)
        ans = querychain(nod*2, st, mid, l, r, which);
    if(mid+1 <= r)
        ans = max(ans, querychain(nod*2+1, mid+1, dr, l, r, which));
    return ans;
}

int hpd(int a, int b)
{
    int ans = max(c[a], c[b]);
    while(a != b){
        if(dep[tt[chain[where[a]].back()]] < dep[tt[chain[where[b]].back()]])
            swap(a, b);
        if(where[a] == where[b]){
            if(dep[a] < dep[b])
                swap(a, b);
            ans = max(ans, querychain(1, 1, chain[where[a]].size()-1, poschain[a], poschain[b], where[a]));
            a = b;
        }else{
            ans = max(ans, querychain(1, 1, chain[where[a]].size()-1, poschain[a], chain[where[a]].size()-1, where[a]));
            a = tt[chain[where[a]].back()];
        }
    }
    return max(c[a], ans);
}

int main()
{
    InParser fin ("heavypath.in");
    OutParser fout ("heavypath.out");
    fin >> n >> test;
    for (int i = 1; i <= n; ++i)
        fin >> c[i];
    for (int i = 1; i < n; ++i){
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1);
    for (int i = 1; i <= p; ++i)
        buildchain(1, 1, chain[i].size()-1, i);
    while(test--){
        int t, x, y;
        fin >> t >> x >> y;
        if(t == 0){
            updatechain(1, 1, chain[where[x]].size()-1, poschain[x], y, where[x]);
            c[x] = y;
        }else fout << hpd(x, y) << "\n";
    }
    return 0;
}