답안 #1048325

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1048325 2024-08-08T06:46:45 Z KiaRez Cat Exercise (JOI23_ho_t4) C++17
100 / 100
408 ms 163664 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 = 6e5+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;
}

ll n, tim, rev[N], par[lg][N], p[N], q[N], tin[N], tout[N];
ll 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);
}

ll 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 84572 KB Output is correct
2 Correct 9 ms 84416 KB Output is correct
3 Correct 9 ms 84544 KB Output is correct
4 Correct 9 ms 84592 KB Output is correct
5 Correct 9 ms 84572 KB Output is correct
6 Correct 10 ms 84824 KB Output is correct
7 Correct 9 ms 84572 KB Output is correct
8 Correct 9 ms 84572 KB Output is correct
9 Correct 10 ms 84572 KB Output is correct
10 Correct 9 ms 84540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 84572 KB Output is correct
2 Correct 9 ms 84416 KB Output is correct
3 Correct 9 ms 84544 KB Output is correct
4 Correct 9 ms 84592 KB Output is correct
5 Correct 9 ms 84572 KB Output is correct
6 Correct 10 ms 84824 KB Output is correct
7 Correct 9 ms 84572 KB Output is correct
8 Correct 9 ms 84572 KB Output is correct
9 Correct 10 ms 84572 KB Output is correct
10 Correct 9 ms 84540 KB Output is correct
11 Correct 10 ms 84680 KB Output is correct
12 Correct 11 ms 84572 KB Output is correct
13 Correct 9 ms 84572 KB Output is correct
14 Correct 10 ms 84572 KB Output is correct
15 Correct 9 ms 84572 KB Output is correct
16 Correct 9 ms 84572 KB Output is correct
17 Correct 9 ms 84572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 84572 KB Output is correct
2 Correct 9 ms 84416 KB Output is correct
3 Correct 9 ms 84544 KB Output is correct
4 Correct 9 ms 84592 KB Output is correct
5 Correct 9 ms 84572 KB Output is correct
6 Correct 10 ms 84824 KB Output is correct
7 Correct 9 ms 84572 KB Output is correct
8 Correct 9 ms 84572 KB Output is correct
9 Correct 10 ms 84572 KB Output is correct
10 Correct 9 ms 84540 KB Output is correct
11 Correct 10 ms 84680 KB Output is correct
12 Correct 11 ms 84572 KB Output is correct
13 Correct 9 ms 84572 KB Output is correct
14 Correct 10 ms 84572 KB Output is correct
15 Correct 9 ms 84572 KB Output is correct
16 Correct 9 ms 84572 KB Output is correct
17 Correct 9 ms 84572 KB Output is correct
18 Correct 14 ms 85340 KB Output is correct
19 Correct 14 ms 85216 KB Output is correct
20 Correct 15 ms 85340 KB Output is correct
21 Correct 15 ms 85168 KB Output is correct
22 Correct 13 ms 85084 KB Output is correct
23 Correct 14 ms 85084 KB Output is correct
24 Correct 14 ms 85084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 84572 KB Output is correct
2 Correct 9 ms 84416 KB Output is correct
3 Correct 9 ms 84544 KB Output is correct
4 Correct 9 ms 84592 KB Output is correct
5 Correct 9 ms 84572 KB Output is correct
6 Correct 10 ms 84824 KB Output is correct
7 Correct 9 ms 84572 KB Output is correct
8 Correct 9 ms 84572 KB Output is correct
9 Correct 10 ms 84572 KB Output is correct
10 Correct 9 ms 84540 KB Output is correct
11 Correct 10 ms 84680 KB Output is correct
12 Correct 11 ms 84572 KB Output is correct
13 Correct 9 ms 84572 KB Output is correct
14 Correct 10 ms 84572 KB Output is correct
15 Correct 9 ms 84572 KB Output is correct
16 Correct 9 ms 84572 KB Output is correct
17 Correct 9 ms 84572 KB Output is correct
18 Correct 14 ms 85340 KB Output is correct
19 Correct 14 ms 85216 KB Output is correct
20 Correct 15 ms 85340 KB Output is correct
21 Correct 15 ms 85168 KB Output is correct
22 Correct 13 ms 85084 KB Output is correct
23 Correct 14 ms 85084 KB Output is correct
24 Correct 14 ms 85084 KB Output is correct
25 Correct 9 ms 84572 KB Output is correct
26 Correct 15 ms 85272 KB Output is correct
27 Correct 14 ms 85256 KB Output is correct
28 Correct 15 ms 85340 KB Output is correct
29 Correct 15 ms 85348 KB Output is correct
30 Correct 15 ms 84828 KB Output is correct
31 Correct 17 ms 84824 KB Output is correct
32 Correct 14 ms 84756 KB Output is correct
33 Correct 14 ms 84828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 84572 KB Output is correct
2 Correct 9 ms 84416 KB Output is correct
3 Correct 9 ms 84544 KB Output is correct
4 Correct 9 ms 84592 KB Output is correct
5 Correct 9 ms 84572 KB Output is correct
6 Correct 10 ms 84824 KB Output is correct
7 Correct 9 ms 84572 KB Output is correct
8 Correct 9 ms 84572 KB Output is correct
9 Correct 10 ms 84572 KB Output is correct
10 Correct 9 ms 84540 KB Output is correct
11 Correct 10 ms 84680 KB Output is correct
12 Correct 11 ms 84572 KB Output is correct
13 Correct 9 ms 84572 KB Output is correct
14 Correct 10 ms 84572 KB Output is correct
15 Correct 9 ms 84572 KB Output is correct
16 Correct 9 ms 84572 KB Output is correct
17 Correct 9 ms 84572 KB Output is correct
18 Correct 14 ms 85340 KB Output is correct
19 Correct 14 ms 85216 KB Output is correct
20 Correct 15 ms 85340 KB Output is correct
21 Correct 15 ms 85168 KB Output is correct
22 Correct 13 ms 85084 KB Output is correct
23 Correct 14 ms 85084 KB Output is correct
24 Correct 14 ms 85084 KB Output is correct
25 Correct 233 ms 162040 KB Output is correct
26 Correct 240 ms 163664 KB Output is correct
27 Correct 227 ms 147324 KB Output is correct
28 Correct 246 ms 144912 KB Output is correct
29 Correct 232 ms 151376 KB Output is correct
30 Correct 234 ms 151136 KB Output is correct
31 Correct 241 ms 151380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 84572 KB Output is correct
2 Correct 9 ms 84572 KB Output is correct
3 Correct 235 ms 126292 KB Output is correct
4 Correct 224 ms 135388 KB Output is correct
5 Correct 218 ms 129620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 84572 KB Output is correct
2 Correct 9 ms 84416 KB Output is correct
3 Correct 9 ms 84544 KB Output is correct
4 Correct 9 ms 84592 KB Output is correct
5 Correct 9 ms 84572 KB Output is correct
6 Correct 10 ms 84824 KB Output is correct
7 Correct 9 ms 84572 KB Output is correct
8 Correct 9 ms 84572 KB Output is correct
9 Correct 10 ms 84572 KB Output is correct
10 Correct 9 ms 84540 KB Output is correct
11 Correct 10 ms 84680 KB Output is correct
12 Correct 11 ms 84572 KB Output is correct
13 Correct 9 ms 84572 KB Output is correct
14 Correct 10 ms 84572 KB Output is correct
15 Correct 9 ms 84572 KB Output is correct
16 Correct 9 ms 84572 KB Output is correct
17 Correct 9 ms 84572 KB Output is correct
18 Correct 14 ms 85340 KB Output is correct
19 Correct 14 ms 85216 KB Output is correct
20 Correct 15 ms 85340 KB Output is correct
21 Correct 15 ms 85168 KB Output is correct
22 Correct 13 ms 85084 KB Output is correct
23 Correct 14 ms 85084 KB Output is correct
24 Correct 14 ms 85084 KB Output is correct
25 Correct 9 ms 84572 KB Output is correct
26 Correct 15 ms 85272 KB Output is correct
27 Correct 14 ms 85256 KB Output is correct
28 Correct 15 ms 85340 KB Output is correct
29 Correct 15 ms 85348 KB Output is correct
30 Correct 15 ms 84828 KB Output is correct
31 Correct 17 ms 84824 KB Output is correct
32 Correct 14 ms 84756 KB Output is correct
33 Correct 14 ms 84828 KB Output is correct
34 Correct 233 ms 162040 KB Output is correct
35 Correct 240 ms 163664 KB Output is correct
36 Correct 227 ms 147324 KB Output is correct
37 Correct 246 ms 144912 KB Output is correct
38 Correct 232 ms 151376 KB Output is correct
39 Correct 234 ms 151136 KB Output is correct
40 Correct 241 ms 151380 KB Output is correct
41 Correct 9 ms 84572 KB Output is correct
42 Correct 9 ms 84572 KB Output is correct
43 Correct 235 ms 126292 KB Output is correct
44 Correct 224 ms 135388 KB Output is correct
45 Correct 218 ms 129620 KB Output is correct
46 Correct 370 ms 144856 KB Output is correct
47 Correct 408 ms 155800 KB Output is correct
48 Correct 360 ms 145488 KB Output is correct
49 Correct 358 ms 147744 KB Output is correct
50 Correct 327 ms 131412 KB Output is correct
51 Correct 285 ms 131836 KB Output is correct
52 Correct 290 ms 136276 KB Output is correct
53 Correct 294 ms 131324 KB Output is correct