Submission #940286

# Submission time Handle Problem Language Result Execution time Memory
940286 2024-03-07T07:32:56 Z GrandTiger1729 Passport (JOI23_passport) C++17
100 / 100
1078 ms 105796 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10, INF = 1e9 + 10;
struct SegTreeMin
{
    int l, r, mid;
    SegTreeMin *lc, *rc;
    int val = INF;
    SegTreeMin(int l_, int r_) : l(l_), r(r_)
    {
        mid = (l + r) / 2;
        if (l == r - 1)
        {
            return;
        }
        lc = new SegTreeMin(l, mid);
        rc = new SegTreeMin(mid, r);
    }
    ~SegTreeMin()
    {
        if (l == r - 1)
        {
            return;
        }
        delete lc;
        delete rc;
    }
    void update(int i, int u)
    {
        if (l == r - 1)
        {
            val = u;
            return;
        }
        if (i < mid)
        {
            lc->update(i, u);
        }
        else
        {
            rc->update(i, u);
        }
        val = min(lc->val, rc->val);
    }
    int query(int ll, int rr)
    {
        if (ll <= l && rr >= r)
        {
            return val;
        }
        int ret = INF;
        if (ll < mid)
        {
            ret = min(ret, lc->query(ll, rr));
        }
        if (mid < rr)
        {
            ret = min(ret, rc->query(ll, rr));
        }
        return ret;
    }
};
struct SegTree
{
    int l, r, mid;
    SegTree *lc, *rc;
    vector<int> val;
    SegTree(int l_, int r_) : l(l_), r(r_)
    {
        mid = (l + r) / 2;
        if (l == r - 1)
        {
            return;
        }
        lc = new SegTree(l, mid);
        rc = new SegTree(mid, r);
    }
    void add(int ll, int rr, int id)
    {
        if (ll <= l && rr >= r)
        {
            val.push_back(id);
            return;
        }
        if (ll < mid)
        {
            lc->add(ll, rr, id);
        }
        if (mid < rr)
        {
            rc->add(ll, rr, id);
        }
    }
    void query(int i, vector<int> &ans)
    {
        ans.insert(ans.end(), val.begin(), val.end());
        val.clear();
        if (l == r - 1)
        {
            return;
        }
        if (i < mid)
        {
            lc->query(i, ans);
        }
        else
        {
            rc->query(i, ans);
        }
    }
};

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; i++)
    {
        int l, r;
        cin >> l >> r;
        l--;
        a[i] = make_pair(l, r);
    }
    vector<int> dpl(n, INF), dpr(n, INF);
    SegTreeMin stl(0, n);
    stl.update(0, 0);
    dpl[0] = 0;
    for (int i = 1; i < n; i++)
    {
        dpl[i] = min(dpl[i], a[i].first > 0 ? 1 + stl.query(a[i].first, i) : 0);
        stl.update(i, dpl[i]);
    }
    SegTreeMin str(0, n);
    str.update(n - 1, 0);
    dpr[n - 1] = 0;
    for (int i = n - 2; i >= 0; i--)
    {
        dpr[i] = a[i].second < n ? 1 + str.query(i, a[i].second) : 0;
        str.update(i, dpr[i]);
    }
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
    for (int i = 0; i < n; i++)
    {
        pq.emplace(min(INF, min(dpl[i], 1 + stl.query(a[i].first, a[i].second)) + min(dpr[i], 1 + str.query(a[i].first, a[i].second))), i);
    }
    vector<int> ans(n, INF);
    SegTree st(0, n);
    for (int i = 0; i < n; i++)
    {
        st.add(a[i].first, a[i].second, i);
    }
    while (pq.size())
    {
        auto [d, i] = pq.top();
        pq.pop();
        if (d >= INF || ans[i] != INF)
        {
            continue;
        }
        ans[i] = d;
        vector<int> res;
        st.query(i, res);
        for (int j : res)
        {
            if (ans[j] == INF)
            {
                pq.emplace(d + 1, j);
            }
        }
    }
    int q;
    cin >> q;
    while (q--)
    {
            int i;
            cin >> i;
            i--;
            cout << (ans[i] == INF ? -1 : ans[i] + 1) << '\n';
    }
    return 0;
    }
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1010 ms 104204 KB Output is correct
5 Correct 361 ms 80600 KB Output is correct
6 Correct 251 ms 81320 KB Output is correct
7 Correct 169 ms 74692 KB Output is correct
8 Correct 109 ms 56356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 6 ms 1624 KB Output is correct
17 Correct 4 ms 1372 KB Output is correct
18 Correct 5 ms 1372 KB Output is correct
19 Correct 5 ms 1540 KB Output is correct
20 Correct 3 ms 1372 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 2 ms 1116 KB Output is correct
23 Correct 4 ms 1372 KB Output is correct
24 Correct 4 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 6 ms 1624 KB Output is correct
17 Correct 4 ms 1372 KB Output is correct
18 Correct 5 ms 1372 KB Output is correct
19 Correct 5 ms 1540 KB Output is correct
20 Correct 3 ms 1372 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 2 ms 1116 KB Output is correct
23 Correct 4 ms 1372 KB Output is correct
24 Correct 4 ms 1372 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 5 ms 1636 KB Output is correct
28 Correct 4 ms 1368 KB Output is correct
29 Correct 3 ms 1368 KB Output is correct
30 Correct 3 ms 1372 KB Output is correct
31 Correct 3 ms 1368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1010 ms 104204 KB Output is correct
5 Correct 361 ms 80600 KB Output is correct
6 Correct 251 ms 81320 KB Output is correct
7 Correct 169 ms 74692 KB Output is correct
8 Correct 109 ms 56356 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 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 0 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 6 ms 1624 KB Output is correct
25 Correct 4 ms 1372 KB Output is correct
26 Correct 5 ms 1372 KB Output is correct
27 Correct 5 ms 1540 KB Output is correct
28 Correct 3 ms 1372 KB Output is correct
29 Correct 3 ms 1372 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 4 ms 1372 KB Output is correct
32 Correct 4 ms 1372 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 5 ms 1636 KB Output is correct
36 Correct 4 ms 1368 KB Output is correct
37 Correct 3 ms 1368 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 3 ms 1368 KB Output is correct
40 Correct 1078 ms 105796 KB Output is correct
41 Correct 398 ms 84020 KB Output is correct
42 Correct 482 ms 94212 KB Output is correct
43 Correct 574 ms 99140 KB Output is correct
44 Correct 271 ms 83908 KB Output is correct
45 Correct 301 ms 84928 KB Output is correct
46 Correct 119 ms 32088 KB Output is correct
47 Correct 387 ms 90816 KB Output is correct
48 Correct 451 ms 88024 KB Output is correct