Submission #881701

# Submission time Handle Problem Language Result Execution time Memory
881701 2023-12-01T18:40:30 Z Alcabel Cat Exercise (JOI23_ho_t4) C++17
54 / 100
2000 ms 251572 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5, maxlog = 18;
const long long inf = 1e16;
int a[maxn];
vector<int> g[maxn];
int sz[maxn], level[maxn], centPar[maxn][maxlog];
int dist[maxn][maxlog], mx[maxn][maxlog], idxNxt[maxn][maxlog];
vector<int> toutList;

void dfsSz(int v, int p) {
    sz[v] = 1;
    for (const int& u : g[v]) {
        if (u != p && level[u] == -1) {
            dfsSz(u, v);
            sz[v] += sz[u];
        }
    }
    toutList.emplace_back(v);
}

void dfsCalc(int v, int p, int j) {
    int i = 0;
    for (const int& u : g[v]) {
        if (u != p && level[u] == -1) {
            dist[u][j] = dist[v][j] + 1;
            mx[u][j] = max(mx[v][j], a[u]);
            centPar[u][j] = centPar[v][j];
            if (p != -1) {
                idxNxt[u][j] = idxNxt[v][j];
            } else {
                idxNxt[u][j] = i;
            }
            dfsCalc(u, v, j);
        }
        ++i;
    }
}

void findCentroid(int v, int j) {
    toutList.clear();
    dfsSz(v, -1);
    for (const int& c : toutList) {
        if (2 * sz[c] >= sz[v]) {
            v = c;
            break;
        }
    }
    // cerr << "j: " << j << ", v: " << v << '\n';
    level[v] = j;
    dist[v][j] = 0;
    mx[v][j] = a[v];
    centPar[v][j] = v;
    dfsCalc(v, -1, j);
    for (const int& u : g[v]) {
        if (level[u] == -1) {
            findCentroid(u, j + 1);
        }
    }
}

struct SegTree {
    int n, N;
    vector<long long> st;
    SegTree() {}
    SegTree(int _n) {
        n = _n;
        N = 1;
        while (N < n) {
            N <<= 1;
        }
        st.resize(2 * N, -inf);
    }
    void maxEq(int i, long long x) {
        int v = N + i;
        st[v] = max(st[v], x);
        v >>= 1;
        while (v > 0) {
            st[v] = max(st[2 * v], st[2 * v + 1]);
            v >>= 1;
        }
    }
    long long getMax(int l, int r) {
        int vl = N + l, vr = N + r;
        long long res = -inf;
        while (vl < vr) {
            if (vl & 1) {
                res = max(res, st[vl]);
            }
            if ((vr ^ 1) & 1) {
                res = max(res, st[vr]);
            }
            vl = (vl + 1) >> 1;
            vr = (vr - 1) >> 1;
        }
        if (vl == vr) {
            res = max(res, st[vl]);
        }
        return res;
    }
    ~SegTree() {}
};

SegTree st[maxn];

void solve() {
    int n;
    cin >> n;
    vector<int> posOf(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        posOf[a[i]] = i;
        level[i] = -1;
    }
    for (int i = 0; i < n - 1; ++i) {
        int v, u;
        cin >> v >> u;
        --v, --u;
        g[v].emplace_back(u);
        g[u].emplace_back(v);
    }
    for (int v = 0; v < n; ++v) {
        st[v] = SegTree(g[v].size());
    }
    findCentroid(0, 0);
    vector<long long> dp(n);
    set<tuple<int, int, int>> s;
    for (int i = 0; i < n; ++i) {
        while (!s.empty() && get<0>(*s.begin()) <= i) {
            int v = get<1>(*s.begin()), j = get<2>(*s.begin());
            s.erase(s.begin());
            st[centPar[v][j]].maxEq(idxNxt[v][j], dp[a[v]] + dist[v][j]);
        }
        int v = posOf[i];
        for (int j = level[v]; j >= 0; --j) {
            if (mx[v][j] <= i) {
                int u = centPar[v][j];
                long long val;
                if (j < level[v]) {
                    val = max(st[u].getMax(0, idxNxt[v][j] - 1), st[u].getMax(idxNxt[v][j] + 1, (int)g[u].size() - 1));
                    val = max(val, dp[a[u]]);
                } else {
                    val = max(val, st[u].getMax(0, (int)g[u].size() - 1));
                }
                dp[i] = max(dp[i], val + dist[v][j]);
            }
        }
        // cerr << "i: " << i << ", dp: " << dp[i] << '\n';
        for (int j = level[v] - 1; j >= 0; --j) {
            s.emplace(mx[v][j], v, j);
        }
    }
    cout << dp[n - 1] << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
        cerr << "-----------\n";
        cout << "-----------\n";
    }
#else
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
#endif

    return 0;
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:140:27: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
  140 |                 long long val;
      |                           ^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20056 KB Output is correct
2 Correct 4 ms 20060 KB Output is correct
3 Correct 4 ms 20060 KB Output is correct
4 Correct 4 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 4 ms 20080 KB Output is correct
7 Correct 5 ms 19864 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20056 KB Output is correct
10 Correct 4 ms 20060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20056 KB Output is correct
2 Correct 4 ms 20060 KB Output is correct
3 Correct 4 ms 20060 KB Output is correct
4 Correct 4 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 4 ms 20080 KB Output is correct
7 Correct 5 ms 19864 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20056 KB Output is correct
10 Correct 4 ms 20060 KB Output is correct
11 Correct 4 ms 20060 KB Output is correct
12 Correct 4 ms 20124 KB Output is correct
13 Correct 4 ms 20060 KB Output is correct
14 Correct 5 ms 20060 KB Output is correct
15 Correct 4 ms 20060 KB Output is correct
16 Correct 4 ms 20060 KB Output is correct
17 Correct 4 ms 20060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20056 KB Output is correct
2 Correct 4 ms 20060 KB Output is correct
3 Correct 4 ms 20060 KB Output is correct
4 Correct 4 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 4 ms 20080 KB Output is correct
7 Correct 5 ms 19864 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20056 KB Output is correct
10 Correct 4 ms 20060 KB Output is correct
11 Correct 4 ms 20060 KB Output is correct
12 Correct 4 ms 20124 KB Output is correct
13 Correct 4 ms 20060 KB Output is correct
14 Correct 5 ms 20060 KB Output is correct
15 Correct 4 ms 20060 KB Output is correct
16 Correct 4 ms 20060 KB Output is correct
17 Correct 4 ms 20060 KB Output is correct
18 Correct 14 ms 25160 KB Output is correct
19 Correct 13 ms 25180 KB Output is correct
20 Correct 13 ms 25180 KB Output is correct
21 Correct 18 ms 26972 KB Output is correct
22 Correct 19 ms 26972 KB Output is correct
23 Correct 18 ms 27016 KB Output is correct
24 Correct 18 ms 26972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20056 KB Output is correct
2 Correct 4 ms 20060 KB Output is correct
3 Correct 4 ms 20060 KB Output is correct
4 Correct 4 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 4 ms 20080 KB Output is correct
7 Correct 5 ms 19864 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20056 KB Output is correct
10 Correct 4 ms 20060 KB Output is correct
11 Correct 4 ms 20060 KB Output is correct
12 Correct 4 ms 20124 KB Output is correct
13 Correct 4 ms 20060 KB Output is correct
14 Correct 5 ms 20060 KB Output is correct
15 Correct 4 ms 20060 KB Output is correct
16 Correct 4 ms 20060 KB Output is correct
17 Correct 4 ms 20060 KB Output is correct
18 Correct 14 ms 25160 KB Output is correct
19 Correct 13 ms 25180 KB Output is correct
20 Correct 13 ms 25180 KB Output is correct
21 Correct 18 ms 26972 KB Output is correct
22 Correct 19 ms 26972 KB Output is correct
23 Correct 18 ms 27016 KB Output is correct
24 Correct 18 ms 26972 KB Output is correct
25 Correct 4 ms 20060 KB Output is correct
26 Correct 15 ms 25376 KB Output is correct
27 Correct 15 ms 25436 KB Output is correct
28 Correct 15 ms 25520 KB Output is correct
29 Correct 15 ms 25436 KB Output is correct
30 Correct 16 ms 26036 KB Output is correct
31 Correct 16 ms 25984 KB Output is correct
32 Correct 16 ms 25944 KB Output is correct
33 Correct 16 ms 25948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20056 KB Output is correct
2 Correct 4 ms 20060 KB Output is correct
3 Correct 4 ms 20060 KB Output is correct
4 Correct 4 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 4 ms 20080 KB Output is correct
7 Correct 5 ms 19864 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20056 KB Output is correct
10 Correct 4 ms 20060 KB Output is correct
11 Correct 4 ms 20060 KB Output is correct
12 Correct 4 ms 20124 KB Output is correct
13 Correct 4 ms 20060 KB Output is correct
14 Correct 5 ms 20060 KB Output is correct
15 Correct 4 ms 20060 KB Output is correct
16 Correct 4 ms 20060 KB Output is correct
17 Correct 4 ms 20060 KB Output is correct
18 Correct 14 ms 25160 KB Output is correct
19 Correct 13 ms 25180 KB Output is correct
20 Correct 13 ms 25180 KB Output is correct
21 Correct 18 ms 26972 KB Output is correct
22 Correct 19 ms 26972 KB Output is correct
23 Correct 18 ms 27016 KB Output is correct
24 Correct 18 ms 26972 KB Output is correct
25 Correct 606 ms 107696 KB Output is correct
26 Correct 606 ms 125544 KB Output is correct
27 Correct 594 ms 125284 KB Output is correct
28 Execution timed out 2107 ms 251572 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20056 KB Output is correct
2 Correct 4 ms 20060 KB Output is correct
3 Correct 1320 ms 201752 KB Output is correct
4 Correct 1270 ms 200824 KB Output is correct
5 Correct 1268 ms 201076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20056 KB Output is correct
2 Correct 4 ms 20060 KB Output is correct
3 Correct 4 ms 20060 KB Output is correct
4 Correct 4 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 4 ms 20080 KB Output is correct
7 Correct 5 ms 19864 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20056 KB Output is correct
10 Correct 4 ms 20060 KB Output is correct
11 Correct 4 ms 20060 KB Output is correct
12 Correct 4 ms 20124 KB Output is correct
13 Correct 4 ms 20060 KB Output is correct
14 Correct 5 ms 20060 KB Output is correct
15 Correct 4 ms 20060 KB Output is correct
16 Correct 4 ms 20060 KB Output is correct
17 Correct 4 ms 20060 KB Output is correct
18 Correct 14 ms 25160 KB Output is correct
19 Correct 13 ms 25180 KB Output is correct
20 Correct 13 ms 25180 KB Output is correct
21 Correct 18 ms 26972 KB Output is correct
22 Correct 19 ms 26972 KB Output is correct
23 Correct 18 ms 27016 KB Output is correct
24 Correct 18 ms 26972 KB Output is correct
25 Correct 4 ms 20060 KB Output is correct
26 Correct 15 ms 25376 KB Output is correct
27 Correct 15 ms 25436 KB Output is correct
28 Correct 15 ms 25520 KB Output is correct
29 Correct 15 ms 25436 KB Output is correct
30 Correct 16 ms 26036 KB Output is correct
31 Correct 16 ms 25984 KB Output is correct
32 Correct 16 ms 25944 KB Output is correct
33 Correct 16 ms 25948 KB Output is correct
34 Correct 606 ms 107696 KB Output is correct
35 Correct 606 ms 125544 KB Output is correct
36 Correct 594 ms 125284 KB Output is correct
37 Execution timed out 2107 ms 251572 KB Time limit exceeded
38 Halted 0 ms 0 KB -