Submission #936574

#TimeUsernameProblemLanguageResultExecution timeMemory
936574penguin133Cat Exercise (JOI23_ho_t4)C++17
100 / 100
511 ms98900 KiB
#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 (stderr)

Main.cpp:112:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  112 | main(){
      | ^~~~
#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...