Submission #881704

# Submission time Handle Problem Language Result Execution time Memory
881704 2023-12-01T18:44:24 Z Alcabel Cat Exercise (JOI23_ho_t4) C++17
54 / 100
2000 ms 251032 KB
#pragma GCC optimize("O3", "Ofast", "unroll-loops", "fast-math")
#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:141:27: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
  141 |                 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 20092 KB Output is correct
4 Correct 5 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 5 ms 20092 KB Output is correct
7 Correct 5 ms 20060 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20060 KB Output is correct
10 Correct 5 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 20092 KB Output is correct
4 Correct 5 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 5 ms 20092 KB Output is correct
7 Correct 5 ms 20060 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20060 KB Output is correct
10 Correct 5 ms 20060 KB Output is correct
11 Correct 6 ms 20068 KB Output is correct
12 Correct 5 ms 20040 KB Output is correct
13 Correct 5 ms 20056 KB Output is correct
14 Correct 6 ms 20060 KB Output is correct
15 Correct 5 ms 20056 KB Output is correct
16 Correct 5 ms 20220 KB Output is correct
17 Correct 5 ms 20056 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 20092 KB Output is correct
4 Correct 5 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 5 ms 20092 KB Output is correct
7 Correct 5 ms 20060 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20060 KB Output is correct
10 Correct 5 ms 20060 KB Output is correct
11 Correct 6 ms 20068 KB Output is correct
12 Correct 5 ms 20040 KB Output is correct
13 Correct 5 ms 20056 KB Output is correct
14 Correct 6 ms 20060 KB Output is correct
15 Correct 5 ms 20056 KB Output is correct
16 Correct 5 ms 20220 KB Output is correct
17 Correct 5 ms 20056 KB Output is correct
18 Correct 15 ms 24988 KB Output is correct
19 Correct 14 ms 25452 KB Output is correct
20 Correct 16 ms 25436 KB Output is correct
21 Correct 20 ms 27224 KB Output is correct
22 Correct 20 ms 27156 KB Output is correct
23 Correct 19 ms 27224 KB Output is correct
24 Correct 19 ms 27120 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 20092 KB Output is correct
4 Correct 5 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 5 ms 20092 KB Output is correct
7 Correct 5 ms 20060 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20060 KB Output is correct
10 Correct 5 ms 20060 KB Output is correct
11 Correct 6 ms 20068 KB Output is correct
12 Correct 5 ms 20040 KB Output is correct
13 Correct 5 ms 20056 KB Output is correct
14 Correct 6 ms 20060 KB Output is correct
15 Correct 5 ms 20056 KB Output is correct
16 Correct 5 ms 20220 KB Output is correct
17 Correct 5 ms 20056 KB Output is correct
18 Correct 15 ms 24988 KB Output is correct
19 Correct 14 ms 25452 KB Output is correct
20 Correct 16 ms 25436 KB Output is correct
21 Correct 20 ms 27224 KB Output is correct
22 Correct 20 ms 27156 KB Output is correct
23 Correct 19 ms 27224 KB Output is correct
24 Correct 19 ms 27120 KB Output is correct
25 Correct 4 ms 20056 KB Output is correct
26 Correct 16 ms 25436 KB Output is correct
27 Correct 16 ms 25436 KB Output is correct
28 Correct 17 ms 25544 KB Output is correct
29 Correct 16 ms 25436 KB Output is correct
30 Correct 17 ms 25948 KB Output is correct
31 Correct 17 ms 25848 KB Output is correct
32 Correct 17 ms 25948 KB Output is correct
33 Correct 17 ms 25992 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 20092 KB Output is correct
4 Correct 5 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 5 ms 20092 KB Output is correct
7 Correct 5 ms 20060 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20060 KB Output is correct
10 Correct 5 ms 20060 KB Output is correct
11 Correct 6 ms 20068 KB Output is correct
12 Correct 5 ms 20040 KB Output is correct
13 Correct 5 ms 20056 KB Output is correct
14 Correct 6 ms 20060 KB Output is correct
15 Correct 5 ms 20056 KB Output is correct
16 Correct 5 ms 20220 KB Output is correct
17 Correct 5 ms 20056 KB Output is correct
18 Correct 15 ms 24988 KB Output is correct
19 Correct 14 ms 25452 KB Output is correct
20 Correct 16 ms 25436 KB Output is correct
21 Correct 20 ms 27224 KB Output is correct
22 Correct 20 ms 27156 KB Output is correct
23 Correct 19 ms 27224 KB Output is correct
24 Correct 19 ms 27120 KB Output is correct
25 Correct 618 ms 110820 KB Output is correct
26 Correct 642 ms 124992 KB Output is correct
27 Correct 632 ms 124660 KB Output is correct
28 Execution timed out 2080 ms 251032 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 1352 ms 202008 KB Output is correct
4 Correct 1319 ms 201076 KB Output is correct
5 Correct 1411 ms 200844 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 20092 KB Output is correct
4 Correct 5 ms 20060 KB Output is correct
5 Correct 4 ms 20060 KB Output is correct
6 Correct 5 ms 20092 KB Output is correct
7 Correct 5 ms 20060 KB Output is correct
8 Correct 4 ms 20060 KB Output is correct
9 Correct 4 ms 20060 KB Output is correct
10 Correct 5 ms 20060 KB Output is correct
11 Correct 6 ms 20068 KB Output is correct
12 Correct 5 ms 20040 KB Output is correct
13 Correct 5 ms 20056 KB Output is correct
14 Correct 6 ms 20060 KB Output is correct
15 Correct 5 ms 20056 KB Output is correct
16 Correct 5 ms 20220 KB Output is correct
17 Correct 5 ms 20056 KB Output is correct
18 Correct 15 ms 24988 KB Output is correct
19 Correct 14 ms 25452 KB Output is correct
20 Correct 16 ms 25436 KB Output is correct
21 Correct 20 ms 27224 KB Output is correct
22 Correct 20 ms 27156 KB Output is correct
23 Correct 19 ms 27224 KB Output is correct
24 Correct 19 ms 27120 KB Output is correct
25 Correct 4 ms 20056 KB Output is correct
26 Correct 16 ms 25436 KB Output is correct
27 Correct 16 ms 25436 KB Output is correct
28 Correct 17 ms 25544 KB Output is correct
29 Correct 16 ms 25436 KB Output is correct
30 Correct 17 ms 25948 KB Output is correct
31 Correct 17 ms 25848 KB Output is correct
32 Correct 17 ms 25948 KB Output is correct
33 Correct 17 ms 25992 KB Output is correct
34 Correct 618 ms 110820 KB Output is correct
35 Correct 642 ms 124992 KB Output is correct
36 Correct 632 ms 124660 KB Output is correct
37 Execution timed out 2080 ms 251032 KB Time limit exceeded
38 Halted 0 ms 0 KB -