Cod sursa(job #2600040)

Utilizator retrogradLucian Bicsi retrograd Data 11 aprilie 2020 21:48:56
Problema Heavy Path Decomposition Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.13 kb
#include <bits/stdc++.h>

using namespace std;


struct LinkCut {
  struct Node {
    int p = 0, c[2] = {0, 0}, pp = 0;
    bool flip = 0;
    int val = 0, dp = 0;
  };
  vector<Node> T;

  LinkCut(int n) : T(n + 1) {}

  // SPLAY TREE OPERATIONS START
  int dir(int x, int y) { return T[x].c[1] == y; }
 
  void set(int x, int d, int y) {
    if (x) { 
      int& oy = T[x].c[d];
      if (T[oy].p == x) T[oy].p = 0;
      oy = y; pull(x);
    }
    if (y) T[y].p = x;
  }
 
  void pull(int x) {
    if (!x) return;
    int &l = T[x].c[0], &r = T[x].c[1];
    T[x].dp = max({T[x].val, T[l].dp, T[r].dp});
  }
 
  void push(int x) {
    return;
    if (!x || !T[x].flip) return;
    int &l = T[x].c[0], &r = T[x].c[1];
    swap(l, r); T[l].flip ^= 1; T[r].flip ^= 1;
    T[x].flip = 0;
  }
 
  void rotate(int x, int d) { 
    int y = T[x].p, z = T[y].p, w = T[x].c[d];
    swap(T[x].pp, T[y].pp);
    set(y, !d, w);
    set(x, d, y);
    set(z, dir(z, y), x);
  }

  void refresh(int x) {
    if (!x) return;
    refresh(T[x].p);
    push(x);
  }
 
  void splay(int x) { 
    // refresh(x);
    for (push(x); T[x].p;) {
      int y = T[x].p, z = T[y].p;
      push(z); push(y); push(x);
      int dx = dir(y, x), dy = dir(z, y);
      if (!z) {
        rotate(x, !dx); 
      }
      else if (dx == dy) 
        rotate(y, !dx), rotate(x, !dx); 
      else
        rotate(x, dy), rotate(x, dx);
    }
  }


  // SPLAY TREE OPERATIONS END

  void Link(int u, int v) { 
  	// cerr << "Link: " << u << " " << v << endl;
    MakeRoot(v);
    T[v].pp = u;
  }

  void dump2() {
	for (int i = 1; i < (int)T.size(); ++i) {
    	cerr << i << ": " << T[i].c[0] << " " << T[i].c[1] << " " 
    	<< T[i].p << " " << T[i].pp << endl;
    }
  }

  /// Move u to root of represented tree.
  void MakeRoot(int u) {
  	// cerr << "MakeRoot: " << u << endl;
    Access(u);
    assert(T[u].p == 0);
    // cerr << "REVERSING: "; dump(T[u].c[0]); cerr << endl;
    int l = T[u].c[0];
    T[l].flip ^= 1; T[l].pp = u;
    set(u, 0, 0);
    // cerr << "----------" << endl;
  }

  void Access(int _u) {
  	// cerr << "Access: " << _u << endl;
    for (int v = 0, u = _u; u; u = T[v = u].pp) {
      // cerr << "relinking: " << u << " to " << v << endl;
      splay(u); splay(v);
      // dump(u); cerr << " | "; dump(v); cerr << endl;
      if (T[u].c[1])
        T[T[u].c[1]].pp = u;
      T[v].pp = 0;
      set(u, 1, v);
    }
    splay(_u);

    // Dump();
    // cerr << "------------" << endl;
  }

  void Update(int u, int val) {
  	// cerr << "Update: " << u << " " << val << endl;
    splay(u);
    T[u].val = val;
    pull(u);
  	// Dump();
  }

  int Query(int u, int v) {
    Access(u);
    Access(v);

    int ans = 0;

    splay(u);
    while (T[u].pp) {
      ans = max({ans, T[u].val, T[T[u].c[0]].dp});
      u = T[u].pp;
      splay(u);
    }
    if (u == v) {
      ans = max(ans, T[u].val);
      return ans;
    }

    for (int it = 0; it < 2; ++it) {
      Access(u);
      splay(v);
      if (T[u].p != v) {
        swap(u, v);
        continue;
      }
      if (u == T[v].c[1]) {
        ans = max({ans, T[u].val, T[v].val, T[T[u].c[0]].dp});
      } else {
        assert(u == T[v].c[0]);
        ans = max({ans, T[u].val, T[v].val, T[T[u].c[1]].dp});
      }
      return ans;
    }

    assert(false);
  }

  // void Dump() {
  // 	for (int i = 1; i < (int)T.size(); ++i) {
  // 	  if (T[i].p) continue;
  // 	  cerr << "(" << i << "): "; dump(i); 
  // 	  cerr << " --> " << T[i].pp << endl;
  // 	}
  // }
  void dump(int x) {
    if (!x) return;
    if (T[x].p) assert(!T[x].pp);
    push(x);
    dump(T[x].c[0]);
    cerr << x << " ";
    dump(T[x].c[1]);
  }
  void Dump() {
    for (int i = 1; i < (int)T.size(); ++i) {
      if (T[i].p) continue;
      cerr << T[i].pp <<  " <-- ";
      dump(i);
      cerr << "(dp: " << T[i].dp << ")";
      cerr << endl;
    }
    cerr << endl;
  }
};

struct Brut {
  vector<set<int>> graph;
  vector<int> val;

  Brut(int n) : graph(n + 1), val(n + 1) {};

  void Link(int a, int b) {
    graph[a].insert(b);
    graph[b].insert(a);
  }

  int Query(int a, int b) {
    vector<int> dp(graph.size(), -1);
    vector<int> q;
    auto push = [&](int node, int x) {
      if (dp[node] != -1) return;
      dp[node] = x;
      q.push_back(node);
    };
    push(a, val[a]);
    for (int i = 0; i < (int)q.size(); ++i) {
      for (auto vec : graph[q[i]])
        push(vec, max(val[vec], dp[q[i]]));
    }
    return dp[b];
  }

  void Update(int node, int x) { val[node] = x; }
};

void Test() {
  int N = 6, V = 10;
  while (true) {
    int n = rand() % N + 1;
    LinkCut lc(n);
    Brut brut(n);

    for (int i = 1; i < n; ++i) {
      int p = rand() % i;
      lc.Link(p + 1, i + 1);
      brut.Link(p + 1, i + 1);
    }
    vector<int> val(n);
    for (int i = 0; i < n; ++i) {
      val[i] = rand() % V + 1;
      lc.T[i + 1].val = val[i];
      brut.val[i + 1] = val[i];
    }

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cerr << "_____________________________________" << endl;
        cerr << "QUERY: " << i + 1 << " " << j + 1 << endl;
        int lcq = lc.Query(i + 1, j + 1);
        int brutq = brut.Query(i + 1, j + 1);
        if (lcq != brutq) {
          cerr << "ERROR" << endl;
          cerr << n << endl;
          for (int i = 0; i < n; ++i) {
            cerr << val[i] << " ";
          }
          cerr << endl;
          for (int i = 1; i <= n; ++i) {
            for (auto vec : brut.graph[i]) {
              if (i <= vec) {
                cout << i << " " << vec << endl;
              }
            }
          }
          cerr << endl;
          cerr << i + 1 << " " << j + 1 << endl;
          cerr << "BRUT: " << brutq << endl;
          cerr << "LC: " << lcq << endl;
          exit(-1);
        }
      }
    }
  }
}

int main() {
  // Test();
  ifstream cin("heavypath.in");
  ofstream cout("heavypath.out");

  int n, m; cin >> n >> m;
  LinkCut lc(n);
  for (int i = 1; i <= n; ++i) {
    cin >> lc.T[i].val;
  }

  for (int i = 1; i < n; ++i) {
    int a, b; cin >> a >> b;
    lc.Link(a, b);
  }

  for (int i = 0; i < m; ++i) {
    int t, a, b; cin >> t >> a >> b;
    if (t == 0) lc.Update(a, b);
    else cout << lc.Query(a, b) << '\n';
  }
  return 0;
}