锰锌刚学OI,对顶multiset做法,马蜂优良,求助WA了9个点
查看原帖
锰锌刚学OI,对顶multiset做法,马蜂优良,求助WA了9个点
778022
yhylivedream楼主2024/6/28 15:05
//author : yhy
#include <bits/stdc++.h> 

using namespace std;
using LL = long long;

const LL kMaxN = 2e5 + 5, kInf = 1e9;

LL n, a[kMaxN], f[kMaxN];
vector<LL> e[kMaxN];

struct Median {
  multiset<LL> l, g;
  void init() {
    l.clear(), g.clear(), l.insert(-kInf), g.insert(kInf);
  }
  void F() {
    for (; l.size() > g.size() + 1;) {
      auto it = (--l.end());
      g.insert(*it), l.erase(it);
    }
    for (; g.size() > l.size();) {
      auto it = g.begin();
      l.insert(*it), g.erase(it);
    }
  }
  void A(LL val) {
    (val <= *g.begin() ? l.insert(val) : g.insert(val)), F();
  }
  void D(LL val) {
    auto it = l.lower_bound(val);
    if (it != l.end()) {
      l.erase(it);
    } else {
      it = g.lower_bound(val), it != g.end() && (g.erase(it), 0);
    }
    F();
  }
  LL Calc() {
    if ((l.size() + g.size() - 2) % 2 == 0) {
      return (*l.rbegin() + *g.begin()) / 2;
    }
    return *l.rbegin();
  }
} s;

void S(LL x, LL fa, LL col) {
  s.A(a[x]), f[x] = (!col ? -kInf : kInf);
  for (LL nxt : e[x]) {
    if (nxt != fa) {
      S(nxt, x, col ^ 1);
      f[x] = (!col ? max(f[x], f[nxt]) : min(f[x], f[nxt]));
    }
  }
  (x != 1 && e[x].size() == 1) && (f[x] = s.Calc()), s.D(a[x]);
}

signed main() {
  cin >> n;
  for (LL i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (LL i = 1, x, y; i < n; i++) {
    cin >> x >> y, e[x].push_back(y), e[y].push_back(x);
  }
  S(1, 0, 0), cout << f[1];
}
2024/6/28 15:05
加载中...