Submission #935506

# Submission time Handle Problem Language Result Execution time Memory
935506 2024-02-29T08:24:22 Z qwe1rt1yuiop1 Cat Exercise (JOI23_ho_t4) C++14
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

int n;
vector<int> seg, v, pos;

void build(int x, int l, int r)
{
    if (l + 1 == r)
    {
        seg[x] = v[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(x << 1, l, mid), build(x << 1 | 1, mid, r);
    seg[x] = max(seg[x << 1], seg[x << 1 | 1]);
}
int qry(int x, int l, int r, int ql, int qr)
{
    if (ql <= l && r <= qr)
        return seg[x];
    int mid = (l + r) >> 1;
    if (qr <= mid)
        return qry(x << 1, l, mid, ql, qr);
    if (ql >= mid)
        return qry(x << 1 | 1, mid, r, ql, qr);
    int retl = qry(x << 1, l, mid, ql, qr), retr = qry(x << 1 | 1, mid, r, ql, qr);
    return max(retl, retr);
}

int ans(int x, int l, int r)
{
    if (x == 0 || l + 1 == r)
        return 0;
    if (pos[x] == l)
    {
        int rx = qry(1, 0, n, pos[x] + 1, r);
        return ans(rx, pos[x] + 1, r) + 1;
    }
    if (pos[x] + 1 == r)
    {
        int lx = qry(1, 0, n, l, pos[x]);
        return ans(lx, l, pos[x]) + 1;
    }
    int lx = qry(1, 0, n, l, pos[x]), rx = qry(1, 0, n, pos[x] + 1, r);
    return max(ans(lx, l, pos[x]), ans(rx, pos[x] + 1, r)) + 1;
}
/*
4
3 4 1 2
1 2
2 3
3 4
*/

void solve()
{
    cin >> n;
    v.resize(n);
    for (int &i : v)
        cin >> i, --i;
    for (int i = 1, a, b; i < n; ++i)
        cin >> a >> b;
    pos.resize(n);
    for (int i = 0; i < n; ++i)
        pos[v[i]] = i;
    seg.assign(n * 4 + 10, 0);
    build(1, 0, n);
    cout << ans(n - 1, 0, n) + 1 << '\n';
}

/*
 */

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -