Submission #936574

# Submission time Handle Problem Language Result Execution time Memory
936574 2024-03-02T08:24:40 Z penguin133 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
511 ms 98900 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, P[200005], back[200050];
struct node{
	int s, e, m, val, pos, lz;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		if(s != e){
			l = new node(s, m), r = new node(m+1, e);
		}
		pos = s, val = lz = 0;
	}
	void prop(){
		if(lz){
			val = lz;
			if(s != e)l->lz = lz, r->lz = lz;
			lz = 0;
		}
	}
	void upd(int a, int b, int c){
		prop();
		if(s == a && b == e)lz = c;
		else{
			if(b <= m)l->upd(a, b, c);
			else if(a > m)r->upd(a, b, c);
			else l->upd(a, m, c), r->upd(m+1, b, c);
			l->prop(), r->prop();
			val = max(l->val, r->val);
			if(l->val >= r->val)pos = l->pos;
			else pos = r->pos;
		}
	}
	pi qry(int a, int b){
		prop();
		if(s == a && b == e)return {val, back[pos]};
		if(b <= m)return l->qry(a, b);
		if(a > m)return r->qry(a, b);
		return max(l->qry(a, m), r->qry(m+1, b));
	}
}*root;

int S[200005], E[200005], dep[200005], p[20][200005];
vector <int> adj[200050];
int cnt = 1;
void dfs(int x, int par, int d){
	S[x] = cnt++;
	p[0][x] = par;
	dep[x] = d;
	for(auto i : adj[x])if(i != par)dfs(i, x, d + 1);
	E[x] = cnt - 1;
}

int lca(int u, int v){
	if(dep[u] > dep[v])swap(u, v);
	int df = dep[v] - dep[u];
	for(int i =0 ; i <= 19; i++)if(df >> i & 1)v = p[i][v];
	if(u == v)return u;
	for(int i = 19; i >=0 ; i--){
		if(p[i][u] != p[i][v])u = p[i][u], v = p[i][v];
	}
	return p[0][u];
}

int grr(int x){
	if(S[x] == E[x])return 0;
	int ans = 0, cur = 0, prv = -1;
	while(1){
		pi tmp = root->qry(S[x], E[x]);
		if(tmp.fi <= 0)break;
		if(prv != -1)cur += dep[tmp.se] + dep[prv] - 2 * dep[lca(prv, tmp.se)];
		prv = tmp.se;
		//cout << x << ' ' << tmp.se << '\n';
		for(auto i : adj[tmp.se]){
			if(S[i] < S[tmp.se])continue;
			pi hah = root->qry(S[i], E[i]);
			if(hah.fi > 0)ans = max(ans, cur + dep[hah.se] - dep[tmp.se] + grr(i));
		}
		root->upd(S[tmp.se], E[tmp.se], -1);
	}
	//cout << x << ' ' << cur << ' ' << ans << '\n';
	ans = max(ans, cur);
	return ans;
}

void solve(){
	cin >> n;
	for(int i = 1; i <= n; i++)cin >> P[i];
	root = new node(1, n);
	for(int i = 1; i < n; i++){
		int a, b; cin >> a >> b;
		adj[a].push_back(b); adj[b].push_back(a);
	}
	dfs(1, -1, 0);
	for(int i = 1; i <= 19; i++)for(int j = 1; j <= n; j++)p[i][j] = p[i-1][p[i-1][j]];
	for(int i = 1; i <= n; i++)root->upd(S[i], S[i], P[i]);
	for(int i = 1; i <= n; i++)back[S[i]] = i;
	cout << grr(1);
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

Main.cpp:112:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  112 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 8 ms 43612 KB Output is correct
3 Correct 6 ms 43612 KB Output is correct
4 Correct 6 ms 43612 KB Output is correct
5 Correct 7 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 6 ms 43860 KB Output is correct
8 Correct 6 ms 43608 KB Output is correct
9 Correct 6 ms 43612 KB Output is correct
10 Correct 6 ms 43672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 8 ms 43612 KB Output is correct
3 Correct 6 ms 43612 KB Output is correct
4 Correct 6 ms 43612 KB Output is correct
5 Correct 7 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 6 ms 43860 KB Output is correct
8 Correct 6 ms 43608 KB Output is correct
9 Correct 6 ms 43612 KB Output is correct
10 Correct 6 ms 43672 KB Output is correct
11 Correct 7 ms 43612 KB Output is correct
12 Correct 6 ms 43612 KB Output is correct
13 Correct 6 ms 43612 KB Output is correct
14 Correct 6 ms 43656 KB Output is correct
15 Correct 8 ms 43612 KB Output is correct
16 Correct 6 ms 43612 KB Output is correct
17 Correct 6 ms 43608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 8 ms 43612 KB Output is correct
3 Correct 6 ms 43612 KB Output is correct
4 Correct 6 ms 43612 KB Output is correct
5 Correct 7 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 6 ms 43860 KB Output is correct
8 Correct 6 ms 43608 KB Output is correct
9 Correct 6 ms 43612 KB Output is correct
10 Correct 6 ms 43672 KB Output is correct
11 Correct 7 ms 43612 KB Output is correct
12 Correct 6 ms 43612 KB Output is correct
13 Correct 6 ms 43612 KB Output is correct
14 Correct 6 ms 43656 KB Output is correct
15 Correct 8 ms 43612 KB Output is correct
16 Correct 6 ms 43612 KB Output is correct
17 Correct 6 ms 43608 KB Output is correct
18 Correct 10 ms 44892 KB Output is correct
19 Correct 10 ms 44916 KB Output is correct
20 Correct 12 ms 44892 KB Output is correct
21 Correct 10 ms 44636 KB Output is correct
22 Correct 10 ms 44892 KB Output is correct
23 Correct 10 ms 44888 KB Output is correct
24 Correct 11 ms 44888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 8 ms 43612 KB Output is correct
3 Correct 6 ms 43612 KB Output is correct
4 Correct 6 ms 43612 KB Output is correct
5 Correct 7 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 6 ms 43860 KB Output is correct
8 Correct 6 ms 43608 KB Output is correct
9 Correct 6 ms 43612 KB Output is correct
10 Correct 6 ms 43672 KB Output is correct
11 Correct 7 ms 43612 KB Output is correct
12 Correct 6 ms 43612 KB Output is correct
13 Correct 6 ms 43612 KB Output is correct
14 Correct 6 ms 43656 KB Output is correct
15 Correct 8 ms 43612 KB Output is correct
16 Correct 6 ms 43612 KB Output is correct
17 Correct 6 ms 43608 KB Output is correct
18 Correct 10 ms 44892 KB Output is correct
19 Correct 10 ms 44916 KB Output is correct
20 Correct 12 ms 44892 KB Output is correct
21 Correct 10 ms 44636 KB Output is correct
22 Correct 10 ms 44892 KB Output is correct
23 Correct 10 ms 44888 KB Output is correct
24 Correct 11 ms 44888 KB Output is correct
25 Correct 7 ms 43612 KB Output is correct
26 Correct 11 ms 44704 KB Output is correct
27 Correct 12 ms 44988 KB Output is correct
28 Correct 11 ms 44636 KB Output is correct
29 Correct 12 ms 44888 KB Output is correct
30 Correct 12 ms 44636 KB Output is correct
31 Correct 12 ms 44636 KB Output is correct
32 Correct 12 ms 44760 KB Output is correct
33 Correct 13 ms 44768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 8 ms 43612 KB Output is correct
3 Correct 6 ms 43612 KB Output is correct
4 Correct 6 ms 43612 KB Output is correct
5 Correct 7 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 6 ms 43860 KB Output is correct
8 Correct 6 ms 43608 KB Output is correct
9 Correct 6 ms 43612 KB Output is correct
10 Correct 6 ms 43672 KB Output is correct
11 Correct 7 ms 43612 KB Output is correct
12 Correct 6 ms 43612 KB Output is correct
13 Correct 6 ms 43612 KB Output is correct
14 Correct 6 ms 43656 KB Output is correct
15 Correct 8 ms 43612 KB Output is correct
16 Correct 6 ms 43612 KB Output is correct
17 Correct 6 ms 43608 KB Output is correct
18 Correct 10 ms 44892 KB Output is correct
19 Correct 10 ms 44916 KB Output is correct
20 Correct 12 ms 44892 KB Output is correct
21 Correct 10 ms 44636 KB Output is correct
22 Correct 10 ms 44892 KB Output is correct
23 Correct 10 ms 44888 KB Output is correct
24 Correct 11 ms 44888 KB Output is correct
25 Correct 219 ms 98900 KB Output is correct
26 Correct 213 ms 97364 KB Output is correct
27 Correct 205 ms 97420 KB Output is correct
28 Correct 204 ms 91240 KB Output is correct
29 Correct 200 ms 90992 KB Output is correct
30 Correct 199 ms 91084 KB Output is correct
31 Correct 200 ms 91236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 43612 KB Output is correct
2 Correct 7 ms 43756 KB Output is correct
3 Correct 313 ms 87088 KB Output is correct
4 Correct 311 ms 86868 KB Output is correct
5 Correct 298 ms 87056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 8 ms 43612 KB Output is correct
3 Correct 6 ms 43612 KB Output is correct
4 Correct 6 ms 43612 KB Output is correct
5 Correct 7 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 6 ms 43860 KB Output is correct
8 Correct 6 ms 43608 KB Output is correct
9 Correct 6 ms 43612 KB Output is correct
10 Correct 6 ms 43672 KB Output is correct
11 Correct 7 ms 43612 KB Output is correct
12 Correct 6 ms 43612 KB Output is correct
13 Correct 6 ms 43612 KB Output is correct
14 Correct 6 ms 43656 KB Output is correct
15 Correct 8 ms 43612 KB Output is correct
16 Correct 6 ms 43612 KB Output is correct
17 Correct 6 ms 43608 KB Output is correct
18 Correct 10 ms 44892 KB Output is correct
19 Correct 10 ms 44916 KB Output is correct
20 Correct 12 ms 44892 KB Output is correct
21 Correct 10 ms 44636 KB Output is correct
22 Correct 10 ms 44892 KB Output is correct
23 Correct 10 ms 44888 KB Output is correct
24 Correct 11 ms 44888 KB Output is correct
25 Correct 7 ms 43612 KB Output is correct
26 Correct 11 ms 44704 KB Output is correct
27 Correct 12 ms 44988 KB Output is correct
28 Correct 11 ms 44636 KB Output is correct
29 Correct 12 ms 44888 KB Output is correct
30 Correct 12 ms 44636 KB Output is correct
31 Correct 12 ms 44636 KB Output is correct
32 Correct 12 ms 44760 KB Output is correct
33 Correct 13 ms 44768 KB Output is correct
34 Correct 219 ms 98900 KB Output is correct
35 Correct 213 ms 97364 KB Output is correct
36 Correct 205 ms 97420 KB Output is correct
37 Correct 204 ms 91240 KB Output is correct
38 Correct 200 ms 90992 KB Output is correct
39 Correct 199 ms 91084 KB Output is correct
40 Correct 200 ms 91236 KB Output is correct
41 Correct 6 ms 43612 KB Output is correct
42 Correct 7 ms 43756 KB Output is correct
43 Correct 313 ms 87088 KB Output is correct
44 Correct 311 ms 86868 KB Output is correct
45 Correct 298 ms 87056 KB Output is correct
46 Correct 488 ms 91216 KB Output is correct
47 Correct 491 ms 90468 KB Output is correct
48 Correct 458 ms 94528 KB Output is correct
49 Correct 504 ms 90448 KB Output is correct
50 Correct 497 ms 86668 KB Output is correct
51 Correct 509 ms 86612 KB Output is correct
52 Correct 499 ms 86640 KB Output is correct
53 Correct 511 ms 86896 KB Output is correct