Submission #1092665

# Submission time Handle Problem Language Result Execution time Memory
1092665 2024-09-24T17:43:26 Z Trisanu_Das Cat Exercise (JOI23_ho_t4) C++17
41 / 100
99 ms 23004 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second

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]);
}
 
signed main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    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';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 360 KB Output is correct
17 Correct 1 ms 360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 360 KB Output is correct
17 Correct 1 ms 360 KB Output is correct
18 Correct 2 ms 872 KB Output is correct
19 Correct 3 ms 1128 KB Output is correct
20 Correct 2 ms 984 KB Output is correct
21 Correct 2 ms 616 KB Output is correct
22 Correct 2 ms 616 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 360 KB Output is correct
17 Correct 1 ms 360 KB Output is correct
18 Correct 2 ms 872 KB Output is correct
19 Correct 3 ms 1128 KB Output is correct
20 Correct 2 ms 984 KB Output is correct
21 Correct 2 ms 616 KB Output is correct
22 Correct 2 ms 616 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 356 KB Output is correct
26 Incorrect 2 ms 612 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 360 KB Output is correct
17 Correct 1 ms 360 KB Output is correct
18 Correct 2 ms 872 KB Output is correct
19 Correct 3 ms 1128 KB Output is correct
20 Correct 2 ms 984 KB Output is correct
21 Correct 2 ms 616 KB Output is correct
22 Correct 2 ms 616 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 95 ms 23004 KB Output is correct
26 Correct 86 ms 22108 KB Output is correct
27 Correct 99 ms 21984 KB Output is correct
28 Correct 59 ms 13412 KB Output is correct
29 Correct 56 ms 13400 KB Output is correct
30 Correct 56 ms 13548 KB Output is correct
31 Correct 57 ms 13404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 464 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 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 360 KB Output is correct
17 Correct 1 ms 360 KB Output is correct
18 Correct 2 ms 872 KB Output is correct
19 Correct 3 ms 1128 KB Output is correct
20 Correct 2 ms 984 KB Output is correct
21 Correct 2 ms 616 KB Output is correct
22 Correct 2 ms 616 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 356 KB Output is correct
26 Incorrect 2 ms 612 KB Output isn't correct
27 Halted 0 ms 0 KB -