제출 #857069

#제출 시각아이디문제언어결과실행 시간메모리
857069jmyszka2007Cat Exercise (JOI23_ho_t4)C++17
54 / 100
2031 ms86260 KiB
#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#define check(x) if (!(x)) { cerr << "Check failed: " << #x << " in line " << __LINE__ << endl; exit(1); }
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#define check(x) 
#endif
typedef long long ll;
#define pi pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
constexpr int LIM = 2e5;
constexpr int base = (1 << 18);
int tri[2 * base];
int lazy[2 * base];
vi g[LIM + 10];
int tab[LIM + 10];
ll dp[LIM + 10];
pi prep[LIM + 10];
int nxt[LIM + 10][19];
pi mx[LIM + 10][19];
int pre[LIM + 10];
int siz[LIM + 10];
int oj[LIM + 10];
int post[LIM + 10];
int aktpre = 1;
int aktpost = 1;
void cntsiz(int v, int o) {
	siz[v] = 1;
	for(auto x : g[v]) {
		if(x != o) {
			cntsiz(x, v);
			siz[v] += siz[x];
		}
	}
}
void makehld(int v, int o) {
	pi mx = {0, -1};
	for(int i = 0; i < sz(g[v]); i++) {
		if(g[v][i] != o) {
			mx = max(mx, make_pair(siz[g[v][i]], i));
		}
	}
	if(mx.nd >= 0) {
		swap(g[v][0], g[v][mx.nd]);
		oj[g[v][0]] = v;
		makehld(g[v][0], v);
		for(int i = 1; i < sz(g[v]); i++) {
			if(g[v][i] != o) {
				oj[g[v][i]] = g[v][i];
				makehld(g[v][i], v);
			}
		}
	}
}
void dfs(int v, int o) {
	pre[v] = aktpre++;
	nxt[v][0] = o;
	mx[v][0] = max(make_pair(tab[v], v), make_pair(tab[o], o));
	for(int i = 1; i <= 18; i++) {
		nxt[v][i] = nxt[nxt[v][i - 1]][i - 1];
		mx[v][i] = max(mx[v][i - 1], mx[nxt[v][i - 1]][i - 1]);
	}
	for(auto x : g[v]) {
		if(x != o) {
			dfs(x, v);
		}
	}
	post[v] = aktpost++;
}
bool czy(int a, int b) {
	return pre[a] <= pre[b] && post[a] >= post[b];
}
int dist(int v, int o) {
	if(v == o) {
		return 0;
	}
	if(pre[v] < pre[o]) {
		swap(v, o);
	}
	int ans = 0;
	for(int i = 18; i >= 0; i--) {
		if(!czy(nxt[v][i], o)) {
			ans += (1 << i);
			v = nxt[v][i];
		}
	}
	if(!czy(v, o)) {
		ans++;
		v = nxt[v][0];
	}
	for(int i = 18; i >= 0; i--) {
		if(!czy(nxt[o][i], v)) {
			ans += (1 << i);
			o = nxt[o][i];
		}
	}
	if(!czy(o, v)) {
		ans++;
		o = nxt[o][0];
	}
	return ans;
}
void spl(int v) {
	tri[2 * v] = max(tri[2 * v], lazy[v]);
	tri[2 * v + 1] = max(tri[2 * v + 1], lazy[v]);
	lazy[2 * v] = max(lazy[2 * v], lazy[v]);
	lazy[2 * v + 1] = max(lazy[2 * v + 1], lazy[v]);
	lazy[v] = 0;
}
void update(int v, int l, int r, int a, int b, int x)  {
	if(l > b || r < a) {
		return;
	}
	if(a <= l && r <= b) {
		tri[v] = max(tri[v], x);
		lazy[v] = max(lazy[v], x);
		return;
	}
	spl(v);
	int mid = (l + r) / 2;
	update(2 * v, l, mid, a, b, x);
	update(2 * v + 1, mid + 1, r, a, b, x);
	tri[v] = max(tri[v], max(tri[2 * v], tri[2 * v + 1]));
}
int query(int v, int l, int r, int a, int b)  {
	if(l > b || r < a) {
		return 0;
	}
	if(a <= l && r <= b) {
		return tri[v];
	}
	spl(v);
	int mid = (l + r) / 2;
	return max(query(2 * v, l, mid, a, b), query(2 * v + 1, mid + 1, r, a, b));
}
void upd(int v, int o, int x) {
	while(pre[v] >= pre[o]) {
		if(pre[oj[v]] >= pre[o]) {
			update(1, 1, base, pre[oj[v]], pre[v], x);
			if(oj[v] == 1) {
				break;
			}
			v = nxt[oj[v]][0];
		}
		else {
			update(1, 1, base, pre[o], pre[v], x);
			break;
		}
	}
}
int que(int v, int o) {
	int ans = 0;
	while(pre[v] >= pre[o]) {
		if(pre[oj[v]] >= pre[o]) {
			ans = max(ans, query(1, 1, base, pre[oj[v]], pre[v]));
			if(oj[v] == 1) {
				break;
			}
			v = nxt[oj[v]][0];
		}
		else {
			ans = max(ans, query(1, 1, base, pre[o], pre[v]));
			break;
		}
	}
	return ans;
}
void solve() {
	//ifstream cin("nazwa.in");
	//ofstream cout("nazwa.out");
	int n;
	cin >> n;
	vector<pi>vs;
	vi pos(n + 1, 0);
	for(int i = 1; i <= n; i++) {
		cin >> tab[i];
		vs.eb(tab[i], i);
		pos[tab[i]] = i;
	}
	sort(all(vs));
	for(int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		g[a].eb(b);
		g[b].eb(a);
	}
	cntsiz(1, 1);
	makehld(1, 1);
	dfs(1, 1);
	for(auto [val, v] : vs) {
		int a = nxt[v][0];
		pi mx2 = {tab[a], a};
		for(int i = 18; i >= 0; i--) {
			if(mx[a][i].st < val) {
				mx2 = max(mx2, mx[a][i]);
				a = nxt[a][i];
			}
		}
		//debug(v, a, que(v, a));
		if(tab[a] < val) {
			int kt = pos[que(v, a)];
			dp[v] = max(dp[v], dp[kt] + dist(v, kt));
			upd(v, a, tab[v]);
			a = nxt[a][0];
		}
		else {
			upd(v, v, tab[v]);
		}
		dp[a] = max(dp[a], dp[v] + dist(v, a));
		//debug(v, dp[v]);
		if(val == n) {
			cout << dp[v] << '\n';
			return;
		}
	}
}
int main() {
	fastio();
	int t = 1;
	//cin >> t;
	while(t--) {
		solve();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...