Submission #1048320

# Submission time Handle Problem Language Result Execution time Memory
1048320 2024-08-08T06:45:17 Z KiaRez Cat Exercise (JOI23_ho_t4) C++17
54 / 100
235 ms 112152 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 5e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;

inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

int n, tim, rev[N], par[lg][N], p[N], q[N], tin[N], tout[N];
int h[N], lazy[2*N], mark[N];
pii seg[2*N];
vector<int> adj[N], tree[N];

int getPar(int v, int dist) {
    for(int i=0;i<lg;i++) if((dist>>i)%2==1) v=par[i][v];
    return v;
}

int LCA(int v, int u) {
    if(h[u] < h[v]) swap(v,u);
    u = getPar(u, h[u]-h[v]);
    if(v == u) return v;
    for(int i=lg-1; i>=0; i--) {
        if(par[i][v] != par[i][u]) {
            v=par[i][v], u=par[i][u];
        }
    }
    return par[0][v];
}

void init(int v, int p=0) {
    tin[v] = ++tim, h[v] = h[p] + 1, par[0][v] = p;
    rev[tim] = v;
    for(int i=1; i<lg; i++) par[i][v]=par[i-1][par[i-1][v]];
    for(auto u : adj[v]) {
        if(u == p) continue;
        init(u, v);
    }
    tout[v] = tim+1;
}

void build(int ind=1) {
    if(ind >= (1<<lg)) {
        seg[ind].S = p[rev[ind - (1<<lg) + 1]];
        return;
    }
    build(2*ind); build(2*ind+1);
    seg[ind] = max(seg[2*ind], seg[2*ind+1]);
}

void pushdown(int ind) {
    seg[ind].F += lazy[ind];
    (ind < (1<<lg)) && (lazy[2*ind] += lazy[ind]);
    (ind < (1<<lg)) && (lazy[2*ind+1] += lazy[ind]);
    lazy[ind] = 0;
}

void update(int l, int r, int x, int ind=1, int lc=1, int rc=(1<<lg)+1) {
    pushdown(ind);
    if(l >= rc || lc >= r) return;
    if(lc >= l && rc <= r) {
        lazy[ind] += x; pushdown(ind);
        return;
    }
    int mid = (lc+rc)/2;
    update(l, r, x, 2*ind, lc, mid);
    update(l, r, x, 2*ind+1, mid, rc);
    seg[ind] = max(seg[2*ind], seg[2*ind+1]);
}

pii query(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
    pushdown(ind);
    if(l >= rc || lc >= r) return Mp(0, 0);
    if(lc >=l && rc <= r) return seg[ind];
    int mid = (lc+rc)/2;
    return max(query(l,r,2*ind,lc,mid), query(l,r,2*ind+1,mid,rc));
}

void divide(int v) {
    mark[v] = 1;
    int p = 0;
    for(auto u : adj[v]) {
        if(mark[u] == 1) continue;
        if(u == par[0][v]) {
            p = u;
            continue;
        }
        update(1, n+1, -1);
        update(tin[u], tout[u], 1);
        int rt = q[query(tin[u], tout[u]).S];
        divide(rt);
        tree[v].pb(rt);
        // cout<<v<<' '<<rt<<endl;
        update(1, n+1, 1);
        update(tin[u], tout[u], -1);
    }

    if(p == 0) return;
    update(tin[v], tout[v], -1);
    int rt = q[query(1, n+1).S];
        // cout<<v<<' '<<rt<<endl;
    tree[v].pb(rt);
    divide(rt);
}

int dp[N];
void dfs(int v) {
    for(int u : tree[v]) {
        dfs(u);
        int lca = LCA(v, u);
        dp[v] = max(dp[v], dp[u]+h[v]+h[u]-2*h[lca]);
    }
    // fuck(v); fuck(dp[v]);
}

int main () {
	ios_base::sync_with_stdio(false), cin.tie(0);

	cin>>n;
    for(int i=1; i<=n; i++) cin>>p[i], q[p[i]]=i;
    for(int v,u,i=1; i<n; i++) {
        cin>>v>>u; adj[v].pb(u); adj[u].pb(v);
    }

    init(q[n], 0);
    build(1);

    divide(q[n]);

    dfs(q[n]);

    cout<<dp[q[n]]<<endl;

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 78428 KB Output is correct
2 Correct 9 ms 78288 KB Output is correct
3 Correct 9 ms 78428 KB Output is correct
4 Correct 9 ms 78428 KB Output is correct
5 Correct 10 ms 78428 KB Output is correct
6 Correct 9 ms 78428 KB Output is correct
7 Correct 8 ms 78428 KB Output is correct
8 Correct 8 ms 78428 KB Output is correct
9 Correct 9 ms 78304 KB Output is correct
10 Correct 9 ms 78492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 78428 KB Output is correct
2 Correct 9 ms 78288 KB Output is correct
3 Correct 9 ms 78428 KB Output is correct
4 Correct 9 ms 78428 KB Output is correct
5 Correct 10 ms 78428 KB Output is correct
6 Correct 9 ms 78428 KB Output is correct
7 Correct 8 ms 78428 KB Output is correct
8 Correct 8 ms 78428 KB Output is correct
9 Correct 9 ms 78304 KB Output is correct
10 Correct 9 ms 78492 KB Output is correct
11 Correct 9 ms 78424 KB Output is correct
12 Correct 8 ms 78300 KB Output is correct
13 Correct 8 ms 78460 KB Output is correct
14 Correct 9 ms 78424 KB Output is correct
15 Correct 9 ms 78428 KB Output is correct
16 Correct 9 ms 78428 KB Output is correct
17 Correct 9 ms 78428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 78428 KB Output is correct
2 Correct 9 ms 78288 KB Output is correct
3 Correct 9 ms 78428 KB Output is correct
4 Correct 9 ms 78428 KB Output is correct
5 Correct 10 ms 78428 KB Output is correct
6 Correct 9 ms 78428 KB Output is correct
7 Correct 8 ms 78428 KB Output is correct
8 Correct 8 ms 78428 KB Output is correct
9 Correct 9 ms 78304 KB Output is correct
10 Correct 9 ms 78492 KB Output is correct
11 Correct 9 ms 78424 KB Output is correct
12 Correct 8 ms 78300 KB Output is correct
13 Correct 8 ms 78460 KB Output is correct
14 Correct 9 ms 78424 KB Output is correct
15 Correct 9 ms 78428 KB Output is correct
16 Correct 9 ms 78428 KB Output is correct
17 Correct 9 ms 78428 KB Output is correct
18 Correct 17 ms 79192 KB Output is correct
19 Correct 14 ms 79192 KB Output is correct
20 Correct 13 ms 79052 KB Output is correct
21 Correct 13 ms 78812 KB Output is correct
22 Correct 13 ms 78936 KB Output is correct
23 Correct 13 ms 78940 KB Output is correct
24 Correct 13 ms 78788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 78428 KB Output is correct
2 Correct 9 ms 78288 KB Output is correct
3 Correct 9 ms 78428 KB Output is correct
4 Correct 9 ms 78428 KB Output is correct
5 Correct 10 ms 78428 KB Output is correct
6 Correct 9 ms 78428 KB Output is correct
7 Correct 8 ms 78428 KB Output is correct
8 Correct 8 ms 78428 KB Output is correct
9 Correct 9 ms 78304 KB Output is correct
10 Correct 9 ms 78492 KB Output is correct
11 Correct 9 ms 78424 KB Output is correct
12 Correct 8 ms 78300 KB Output is correct
13 Correct 8 ms 78460 KB Output is correct
14 Correct 9 ms 78424 KB Output is correct
15 Correct 9 ms 78428 KB Output is correct
16 Correct 9 ms 78428 KB Output is correct
17 Correct 9 ms 78428 KB Output is correct
18 Correct 17 ms 79192 KB Output is correct
19 Correct 14 ms 79192 KB Output is correct
20 Correct 13 ms 79052 KB Output is correct
21 Correct 13 ms 78812 KB Output is correct
22 Correct 13 ms 78936 KB Output is correct
23 Correct 13 ms 78940 KB Output is correct
24 Correct 13 ms 78788 KB Output is correct
25 Correct 8 ms 78428 KB Output is correct
26 Correct 15 ms 78940 KB Output is correct
27 Correct 14 ms 79048 KB Output is correct
28 Correct 15 ms 79072 KB Output is correct
29 Correct 14 ms 78940 KB Output is correct
30 Correct 13 ms 78620 KB Output is correct
31 Correct 13 ms 78684 KB Output is correct
32 Correct 15 ms 78832 KB Output is correct
33 Correct 14 ms 78684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 78428 KB Output is correct
2 Correct 9 ms 78288 KB Output is correct
3 Correct 9 ms 78428 KB Output is correct
4 Correct 9 ms 78428 KB Output is correct
5 Correct 10 ms 78428 KB Output is correct
6 Correct 9 ms 78428 KB Output is correct
7 Correct 8 ms 78428 KB Output is correct
8 Correct 8 ms 78428 KB Output is correct
9 Correct 9 ms 78304 KB Output is correct
10 Correct 9 ms 78492 KB Output is correct
11 Correct 9 ms 78424 KB Output is correct
12 Correct 8 ms 78300 KB Output is correct
13 Correct 8 ms 78460 KB Output is correct
14 Correct 9 ms 78424 KB Output is correct
15 Correct 9 ms 78428 KB Output is correct
16 Correct 9 ms 78428 KB Output is correct
17 Correct 9 ms 78428 KB Output is correct
18 Correct 17 ms 79192 KB Output is correct
19 Correct 14 ms 79192 KB Output is correct
20 Correct 13 ms 79052 KB Output is correct
21 Correct 13 ms 78812 KB Output is correct
22 Correct 13 ms 78936 KB Output is correct
23 Correct 13 ms 78940 KB Output is correct
24 Correct 13 ms 78788 KB Output is correct
25 Incorrect 235 ms 112152 KB Output isn't correct
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 78424 KB Output is correct
2 Correct 10 ms 78420 KB Output is correct
3 Correct 214 ms 92448 KB Output is correct
4 Correct 214 ms 92632 KB Output is correct
5 Correct 218 ms 92244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 78428 KB Output is correct
2 Correct 9 ms 78288 KB Output is correct
3 Correct 9 ms 78428 KB Output is correct
4 Correct 9 ms 78428 KB Output is correct
5 Correct 10 ms 78428 KB Output is correct
6 Correct 9 ms 78428 KB Output is correct
7 Correct 8 ms 78428 KB Output is correct
8 Correct 8 ms 78428 KB Output is correct
9 Correct 9 ms 78304 KB Output is correct
10 Correct 9 ms 78492 KB Output is correct
11 Correct 9 ms 78424 KB Output is correct
12 Correct 8 ms 78300 KB Output is correct
13 Correct 8 ms 78460 KB Output is correct
14 Correct 9 ms 78424 KB Output is correct
15 Correct 9 ms 78428 KB Output is correct
16 Correct 9 ms 78428 KB Output is correct
17 Correct 9 ms 78428 KB Output is correct
18 Correct 17 ms 79192 KB Output is correct
19 Correct 14 ms 79192 KB Output is correct
20 Correct 13 ms 79052 KB Output is correct
21 Correct 13 ms 78812 KB Output is correct
22 Correct 13 ms 78936 KB Output is correct
23 Correct 13 ms 78940 KB Output is correct
24 Correct 13 ms 78788 KB Output is correct
25 Correct 8 ms 78428 KB Output is correct
26 Correct 15 ms 78940 KB Output is correct
27 Correct 14 ms 79048 KB Output is correct
28 Correct 15 ms 79072 KB Output is correct
29 Correct 14 ms 78940 KB Output is correct
30 Correct 13 ms 78620 KB Output is correct
31 Correct 13 ms 78684 KB Output is correct
32 Correct 15 ms 78832 KB Output is correct
33 Correct 14 ms 78684 KB Output is correct
34 Incorrect 235 ms 112152 KB Output isn't correct
35 Halted 0 ms 0 KB -