Submission #769252

# Submission time Handle Problem Language Result Execution time Memory
769252 2023-06-29T10:51:46 Z danikoynov Cat Exercise (JOI23_ho_t4) C++14
74 / 100
2000 ms 59956 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 2e5 + 10;

int n, a[maxn], used[maxn], pos[maxn];
vector < int > adj[maxn];
ll dp[maxn];
int rec(int val)
{
    if (used[val])
        return dp[val];
    used[val] = 1;
    int left = 0, right = n + 1;
    for (int i = n; i > val; i --)
    {
        if (pos[i] > pos[val])
        {
            right = min(right, pos[i]);
        }
        else
        {
            left = max(left, pos[i]);
        }
    }

    int mx = -1;
    for (int i = pos[val] + 1; i < right; i ++)
    {
        if (a[i] > mx)
        {
            mx = a[i];
            dp[val] = max(dp[val], (ll)(i - pos[val] + rec(a[i])));
        }
    }
    mx = -1;
    for (int i = pos[val] - 1; i > left; i --)
    {
        if (a[i] > mx)
        {
            mx = a[i];
            dp[val] = max(dp[val], (ll)(pos[val] - i + rec(a[i])));
        }
    }
    return dp[val];
}

int dfs(int v, int p, int mx, int dis, int val)
{
    if (mx > val)
        return 0;

    int ans = 0;
    if (a[v] == mx)
        ans = max((ll)ans, dis + dp[v]);

    for (int u : adj[v])
    {
        if (u == p)
            continue;
        ans = max(ans, dfs(u, v, max(mx, a[u]), dis + 1, val));

    }

    return ans;
}

int bef[maxn], aft[maxn];

pair < int, int > tree[4 * maxn];

void build_tree(int root, int left, int right)
{
    if (left == right)
    {
        tree[root] = {a[left], left};
        return;
    }

    int mid = (left + right) / 2;
    build_tree(root * 2, left, mid);
    build_tree(root * 2 + 1, mid + 1, right);

    tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
}

pair < int, int > query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
        return {-1, -1};

    if (left >= qleft && right <= qright)
        return tree[root];

    int mid = (left + right) / 2;
    return max(query(root * 2, left, mid, qleft, qright),
               query(root * 2 + 1, mid + 1, right, qleft, qright));
}

int best[maxn], par[maxn], depth[maxn];
int tin[maxn], tout[maxn], timer;
int occ[2 * maxn];

void trav(int v)
{
    occ[++ timer] = v;
    tin[v] = timer;

    for (int u : adj[v])
    {
        if (u == par[v])
            continue;
        depth[u] = depth[v] + 1;
        par[u] = v;
        trav(u);
        occ[++ timer] = v;
    }
    tout[v] = timer;
}

const int maxlog = 20;
int fp[maxlog][2 * maxn], lg[2 * maxn];

void build_sparse_table()
{
    for (int i = 1; i <= timer; i ++)
    {
        lg[i] = lg[i / 2] + 1;
        fp[0][i] = occ[i];
    }

    for (int j = 1; j < lg[timer]; j ++)
        for (int i = 1; i <= timer - (1 << j) + 1; i ++)
        {
            fp[j][i] = fp[j - 1][i + (1 << (j - 1))];
            if (depth[fp[j - 1][i]] < depth[fp[j][i]])
                fp[j][i] = fp[j - 1][i];
        }
}

int lca_query(int v, int u)
{
    int l = tin[v], r = tin[u];
    if (l > r)
        swap(l, r);
    int len = lg[r - l + 1] - 1, lca = fp[len][r - (1 << len) + 1];
    if (depth[fp[len][l]] < depth[lca])
        lca = fp[len][l];
    return lca;
}

int distance(int v, int u)
{
    return depth[v] + depth[u] - 2 * depth[lca_query(v, u)];
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        pos[a[i]] = i;
    }


    bool chain = true;
    for (int i = 1; i < n; i ++)
    {
        int v, u;
        cin >> v >> u;
        if (!(v == i && u == i + 1))
            chain = false;

        adj[v].push_back(u);
        adj[u].push_back(v);
    }



    if (chain)
    {
        stack < int > st;
        for (int i = 1; i <= n; i ++)
        {
            while(!st.empty() && a[st.top()] < a[i])
                st.pop();

            if (!st.empty())
                bef[i] = st.top();
            ///cout << "stack " << i << " " << st.top() << endl;
            st.push(i);
        }

        while(!st.empty())
            st.pop();

        for (int i = n; i > 0; i --)
        {
            while(!st.empty() && a[st.top()] < a[i])
                st.pop();
            if (!st.empty())
                aft[i] = st.top();
            else
                aft[i] = n + 1;
            st.push(i);
        }
        build_tree(1, 1, n);
        for (int i = 1; i <= n; i ++)
        {
            int v = pos[i];
            ///cout << "now " << v << " " << bef[v] << " " <<aft[v] << endl;
            pair < int, int > lf = query(1, 1, n, bef[v] + 1, v - 1);
            pair < int, int > rf = query(1, 1, n, v + 1, aft[v] - 1);
            ///cout << v << " " << lf.second << " " << rf.second << endl;
            if (lf.first != -1)
            {
                ///cout << "here " << dp[lf.first] << " " << lf.first << endl;
                dp[v] = max(dp[v], dp[lf.second] + (ll)(v - lf.second));
            }
            if (rf.first != -1)
                dp[v] = max(dp[v], dp[rf.second] + (ll)(rf.second - v));
            //cout << dp[v] << endl;
        }
        cout << dp[pos[n]] << endl;
    }
    else
    {
        trav(1);
        build_sparse_table();
        for (int i = 1; i <= n; i ++)
        {

            int v = pos[i];
            //cout << "step" << endl;
            //cout << v << endl;
            for (int u : adj[v])
            {
                ///cout << u << endl;
                if (u == par[v])
                    continue;
                ///cout << "fine" << endl;
                if (best[u] != 0)
                {
                    ///cout << "here " << best[u] << endl;
                    dp[v] = max(dp[v], dp[pos[best[u]]] + distance(v, pos[best[u]]));
                }
            }



            int ver = par[v];
            while(ver != 0)
            {
                if (a[ver] > i)
                    break;
                //cout << "check " << v << " " << ver << " " << best[ver] << " " << pos[best[ver]] << " " << dp[best[ver]] << endl;
                dp[v] = max(dp[v], dp[pos[best[ver]]] + distance(v, pos[best[ver]]));
                ver = par[ver];
            }

            ver = v;
            while(ver != 0)
            {
                if (a[ver] > i)
                    break;
                best[ver] = i;
                ver = par[ver];
            }
            ///cout << "dp " << v << " " << dp[v] << endl;
        }

        cout << dp[pos[n]] << endl;
    }


}

int main()
{
    solve();
    return 0;
}
/**
5
5 3 2 1 4
1 2
2 3
3 4
4 5
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 7 ms 5332 KB Output is correct
19 Correct 8 ms 5376 KB Output is correct
20 Correct 9 ms 5332 KB Output is correct
21 Correct 7 ms 5332 KB Output is correct
22 Correct 7 ms 5436 KB Output is correct
23 Correct 7 ms 5332 KB Output is correct
24 Correct 7 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 7 ms 5332 KB Output is correct
19 Correct 8 ms 5376 KB Output is correct
20 Correct 9 ms 5332 KB Output is correct
21 Correct 7 ms 5332 KB Output is correct
22 Correct 7 ms 5436 KB Output is correct
23 Correct 7 ms 5332 KB Output is correct
24 Correct 7 ms 5332 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 43 ms 6100 KB Output is correct
27 Correct 59 ms 6236 KB Output is correct
28 Correct 43 ms 6192 KB Output is correct
29 Correct 41 ms 6204 KB Output is correct
30 Correct 10 ms 5972 KB Output is correct
31 Correct 7 ms 5972 KB Output is correct
32 Correct 6 ms 5972 KB Output is correct
33 Correct 9 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 7 ms 5332 KB Output is correct
19 Correct 8 ms 5376 KB Output is correct
20 Correct 9 ms 5332 KB Output is correct
21 Correct 7 ms 5332 KB Output is correct
22 Correct 7 ms 5436 KB Output is correct
23 Correct 7 ms 5332 KB Output is correct
24 Correct 7 ms 5332 KB Output is correct
25 Correct 221 ms 20480 KB Output is correct
26 Correct 239 ms 20412 KB Output is correct
27 Correct 242 ms 20444 KB Output is correct
28 Correct 228 ms 20112 KB Output is correct
29 Correct 254 ms 20064 KB Output is correct
30 Correct 240 ms 19972 KB Output is correct
31 Correct 230 ms 20064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 214 ms 49172 KB Output is correct
4 Correct 261 ms 52872 KB Output is correct
5 Correct 231 ms 52764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 7 ms 5332 KB Output is correct
19 Correct 8 ms 5376 KB Output is correct
20 Correct 9 ms 5332 KB Output is correct
21 Correct 7 ms 5332 KB Output is correct
22 Correct 7 ms 5436 KB Output is correct
23 Correct 7 ms 5332 KB Output is correct
24 Correct 7 ms 5332 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 43 ms 6100 KB Output is correct
27 Correct 59 ms 6236 KB Output is correct
28 Correct 43 ms 6192 KB Output is correct
29 Correct 41 ms 6204 KB Output is correct
30 Correct 10 ms 5972 KB Output is correct
31 Correct 7 ms 5972 KB Output is correct
32 Correct 6 ms 5972 KB Output is correct
33 Correct 9 ms 5972 KB Output is correct
34 Correct 221 ms 20480 KB Output is correct
35 Correct 239 ms 20412 KB Output is correct
36 Correct 242 ms 20444 KB Output is correct
37 Correct 228 ms 20112 KB Output is correct
38 Correct 254 ms 20064 KB Output is correct
39 Correct 240 ms 19972 KB Output is correct
40 Correct 230 ms 20064 KB Output is correct
41 Correct 2 ms 5076 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 214 ms 49172 KB Output is correct
44 Correct 261 ms 52872 KB Output is correct
45 Correct 231 ms 52764 KB Output is correct
46 Execution timed out 2072 ms 59956 KB Time limit exceeded
47 Halted 0 ms 0 KB -