Submission #781971

# Submission time Handle Problem Language Result Execution time Memory
781971 2023-07-13T14:19:00 Z TeaTime Cat Exercise (JOI23_ho_t4) C++17
100 / 100
291 ms 67576 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

const ll SZ = 200'100, LG = 18;

ll n;
vector<ll> vec;
vector<vector<ll>> gr;

ll up[SZ][LG], tin[SZ], tout[SZ], t = 0;
ll seg[SZ * 8], val[SZ], dp[SZ];

void upd(int v, int l, int r, int pos, int val) {
    if (l == r - 1) {
        seg[v] = val;
    } else {
        int mid = (l + r) / 2;
        if (pos < mid) {
            upd(v * 2 + 1, l, mid, pos, val);
        } else {
            upd(v * 2 + 2, mid, r, pos, val);
        }
        seg[v] = seg[v * 2 + 1] + seg[v * 2 + 2];
    }
}

ll ask(int v, int l, int r, int askl, int askr) {
    if (l >= askr || r <= askl) return 0;

    if (askl <= l && r <= askr) return seg[v];

    int mid = (l + r) / 2;
    return ask(v * 2 + 1, l, mid, askl, askr) + ask(v * 2 + 2, mid, r, askl, askr);
}

void dfs(int v, int par = -1) {
    for (int i = 1; i < LG; i++) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }

    t++;
    tin[v] = t;

    for (auto to : gr[v]) {
        if (to != par) {
            dp[to] = dp[v] + 1;
            up[to][0] = v;
            dfs(to, v);
        }
    }

    t++;
    tout[v] = t;
}

bool is_par(int v, int u) {
    return tin[v] <= tin[u] && tout[u] <= tout[v];
}

ll lca(int v, int u) {
    if (is_par(v, u)) return v;
    if (is_par(u, v)) return u;

    for (int i = LG - 1; i >= 0; i--) {
        if (!is_par(up[u][i], v)) u = up[u][i];
    }

    return up[u][0];
}

ll dist(int v, int u) {
    ll lc = lca(u, v);
    return dp[u] + dp[v] - 2 * dp[lc];
}

ll sum(int v, int u) {
    ll lc = lca(u, v);
    return ask(0, 0, SZ * 2, 0, tin[u] + 1) + ask(0, 0, SZ * 2, 0, tin[v] + 1) - 2 * ask(0, 0, SZ * 2, 0, tin[lc] + 1) + val[lc];
}

void toggle(int v) {
    val[v] = 1;
    upd(0, 0, 2 * SZ, tin[v], 1);
    upd(0, 0, 2 * SZ, tout[v], -1);
}

ll dsu[SZ], opt[SZ];

ll find(int v) {
    if (dsu[v] == v) return v;
    return dsu[v] = find(dsu[v]);
}

void uni(int v, int u) {
    v = find(v);
    u = find(u);
    if (v != u) {
        dsu[u] = v;
    }
}

int main() {
    fastInp;

    cin >> n;

    vec.resize(n);
    for (auto &c : vec) cin >> c;
    vector<pair<ll, ll>> srt;
    for (int i = 0; i < n; i++) {
        srt.push_back({vec[i], i});
        dsu[i] = i;
    }

    sort(srt.rbegin(), srt.rend());

    gr.resize(n);
    for (int i = 0; i < n - 1; i++) {
        ll u, v;
        cin >> u >> v;
        u--; v--;
        gr[u].push_back(v);
        gr[v].push_back(u);
    }

    dfs(0);

    ll prev = srt[0].second, ans = 0;
    for (int ii = n - 1; ii >= 0; ii--) {
        int i = srt[ii].second;
        for (auto to : gr[i]) {
            if (vec[to] < vec[i]) {
                opt[i] = max(opt[i], opt[find(to)] + dist(find(to), i));
                
                uni(i, to);
            }
        }
    }

    ans = opt[srt[0].second];
    //for (auto c : srt) cout << c.first << " " << c.second << "\n";

    cout << ans;
    return 0;
    toggle(prev);

    srt.erase(srt.begin());
    for (auto c : srt) {
        int ind = c.second;

        cerr << prev << " " << ind << " " << sum(prev, ind) << " " << dist(prev, ind) << "\n";
        if (sum(prev, ind) == 1) {
            ans += dist(prev, ind);
            toggle(ind);
            prev = ind;
        }
    }

    cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 456 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 456 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1976 KB Output is correct
19 Correct 4 ms 2004 KB Output is correct
20 Correct 3 ms 2000 KB Output is correct
21 Correct 4 ms 1940 KB Output is correct
22 Correct 3 ms 2004 KB Output is correct
23 Correct 4 ms 1952 KB Output is correct
24 Correct 3 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 456 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1976 KB Output is correct
19 Correct 4 ms 2004 KB Output is correct
20 Correct 3 ms 2000 KB Output is correct
21 Correct 4 ms 1940 KB Output is correct
22 Correct 3 ms 2004 KB Output is correct
23 Correct 4 ms 1952 KB Output is correct
24 Correct 3 ms 2004 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 4 ms 1876 KB Output is correct
27 Correct 3 ms 1896 KB Output is correct
28 Correct 3 ms 1872 KB Output is correct
29 Correct 5 ms 1884 KB Output is correct
30 Correct 4 ms 1716 KB Output is correct
31 Correct 4 ms 1720 KB Output is correct
32 Correct 4 ms 1744 KB Output is correct
33 Correct 3 ms 1748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 456 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1976 KB Output is correct
19 Correct 4 ms 2004 KB Output is correct
20 Correct 3 ms 2000 KB Output is correct
21 Correct 4 ms 1940 KB Output is correct
22 Correct 3 ms 2004 KB Output is correct
23 Correct 4 ms 1952 KB Output is correct
24 Correct 3 ms 2004 KB Output is correct
25 Correct 132 ms 67576 KB Output is correct
26 Correct 120 ms 66304 KB Output is correct
27 Correct 117 ms 66272 KB Output is correct
28 Correct 142 ms 66304 KB Output is correct
29 Correct 143 ms 66248 KB Output is correct
30 Correct 145 ms 66296 KB Output is correct
31 Correct 142 ms 66256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 160 ms 57136 KB Output is correct
4 Correct 172 ms 57244 KB Output is correct
5 Correct 173 ms 57160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 456 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1976 KB Output is correct
19 Correct 4 ms 2004 KB Output is correct
20 Correct 3 ms 2000 KB Output is correct
21 Correct 4 ms 1940 KB Output is correct
22 Correct 3 ms 2004 KB Output is correct
23 Correct 4 ms 1952 KB Output is correct
24 Correct 3 ms 2004 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 4 ms 1876 KB Output is correct
27 Correct 3 ms 1896 KB Output is correct
28 Correct 3 ms 1872 KB Output is correct
29 Correct 5 ms 1884 KB Output is correct
30 Correct 4 ms 1716 KB Output is correct
31 Correct 4 ms 1720 KB Output is correct
32 Correct 4 ms 1744 KB Output is correct
33 Correct 3 ms 1748 KB Output is correct
34 Correct 132 ms 67576 KB Output is correct
35 Correct 120 ms 66304 KB Output is correct
36 Correct 117 ms 66272 KB Output is correct
37 Correct 142 ms 66304 KB Output is correct
38 Correct 143 ms 66248 KB Output is correct
39 Correct 145 ms 66296 KB Output is correct
40 Correct 142 ms 66256 KB Output is correct
41 Correct 0 ms 328 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 160 ms 57136 KB Output is correct
44 Correct 172 ms 57244 KB Output is correct
45 Correct 173 ms 57160 KB Output is correct
46 Correct 267 ms 63180 KB Output is correct
47 Correct 270 ms 62052 KB Output is correct
48 Correct 291 ms 63732 KB Output is correct
49 Correct 273 ms 62092 KB Output is correct
50 Correct 269 ms 56724 KB Output is correct
51 Correct 251 ms 56712 KB Output is correct
52 Correct 244 ms 56800 KB Output is correct
53 Correct 247 ms 56832 KB Output is correct