답안 #935511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
935511 2024-02-29T08:28:12 Z qwe1rt1yuiop1 Cat Exercise (JOI23_ho_t4) C++14
21 / 100
99 ms 18248 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)
{
    assert(qry(1, 0, n, l, r) == x);
    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) + pos[rx] - pos[x];
    }
    if (pos[x] + 1 == r)
    {
        int lx = qry(1, 0, n, l, pos[x]);
        return ans(lx, l, pos[x]) + pos[x] - pos[lx];
    }
    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]) + pos[x] - pos[lx], ans(rx, pos[x] + 1, r) + pos[rx] - pos[x]);
}
/*
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) << '\n';
}

/*
 */

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

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 2 ms 800 KB Output is correct
20 Correct 2 ms 724 KB Output is correct
21 Correct 2 ms 544 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 2 ms 600 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 2 ms 800 KB Output is correct
20 Correct 2 ms 724 KB Output is correct
21 Correct 2 ms 544 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 2 ms 600 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Incorrect 2 ms 604 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 2 ms 800 KB Output is correct
20 Correct 2 ms 724 KB Output is correct
21 Correct 2 ms 544 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 2 ms 600 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Incorrect 99 ms 18248 KB Output isn't correct
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 404 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 2 ms 800 KB Output is correct
20 Correct 2 ms 724 KB Output is correct
21 Correct 2 ms 544 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 2 ms 600 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Incorrect 2 ms 604 KB Output isn't correct
27 Halted 0 ms 0 KB -