답안 #986624

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
986624 2024-05-20T23:44:58 Z 876pol Passport (JOI23_passport) C++17
100 / 100
806 ms 32860 KB
#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define dbg(...)
#define STRUCT_DBG(...)
#else
#include "lib/debug.h"
#endif

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T>
using vec = vector<T>;
using vll = vector<ll>;
using vpll = vector<pair<ll, ll>>;
using vvll = vector<vector<ll>>;
using vvpll = vector<vector<pair<ll, ll>>>;
template <class T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define FOR(i, s, e) for (ll i = (ll)s; i < (ll)e; i++)
#define CFOR(i, s, e) for (ll i = (ll)s; i <= (ll)e; i++)
#define RFOR(i, e, s) for (ll i = (ll)e - 1; i >= (ll)s; i--)
#define TRAV(a, c) for (const auto &a : c)
#define all(x) x.begin(), x.end()

#define MOD 1000000007
// #define MOD 998244353

#define FASTIO
#define PRECISION
// #define FILE "file"

#define SINGLE
// #define MULTIPLE
// #define GOOGLE

void solve() {
    ll n;
    cin >> n;
    vpll a(n + 1);
    CFOR(i, 1, n) cin >> a[i].first >> a[i].second;
    auto cal = [&](vll &dist, vll &fr) {
        vvpll ranges(n + 1);
        CFOR(i, 2, n) ranges[a[i].first].push_back({a[i].second, i});
        CFOR(i, 1, n) sort(all(ranges[i]));
        vpll seg(2 * n, {-1, -1});
#define lc(v) (v + 1)
#define rc(v) (v + 2 * (tm - tl + 1))
        function<void(ll, ll, ll)> build = [&](ll v, ll tl, ll tr) {
            if (tl == tr) {
                if (ranges[tl].size()) {
                    seg[v] = ranges[tl].back();
                    ranges[tl].pop_back();
                }
            } else {
                ll tm = (tl + tr) / 2;
                build(lc(v), tl, tm);
                build(rc(v), tm + 1, tr);
                seg[v] = max(seg[lc(v)], seg[rc(v)]);
            }
        };
        build(0, 1, n);
        function<pll(ll, ll, ll, ll)> query = [&](ll v, ll tl, ll tr,
                                                  ll ind) -> pll {
            if (tr <= ind) return seg[v];
            if (ind < tl) return {-1, -1};
            ll tm = (tl + tr) / 2;
            return max(query(lc(v), tl, tm, ind),
                       query(rc(v), tm + 1, tr, ind));
        };
        function<void(ll, ll, ll, ll)> erase = [&](ll v, ll tl, ll tr, ll ind) {
            if (tl == tr && tl == ind) {
                if (ranges[ind].size()) {
                    seg[v] = ranges[ind].back();
                    ranges[ind].pop_back();
                } else {
                    seg[v] = {-1, -1};
                }
            } else {
                ll tm = (tl + tr) / 2;
                if (ind <= tm) {
                    erase(lc(v), tl, tm, ind);
                } else {
                    erase(rc(v), tm + 1, tr, ind);
                }
                seg[v] = max(seg[lc(v)], seg[rc(v)]);
            }
        };
        deque<pll> qu = {{1, 1}};  // index, fr
        ll cdist = 0;
        while (qu.size()) {
            for (ll _ = 0, t = qu.size(); _ < t; _++) {
                auto node = qu.front();
                qu.pop_front();
                dist[node.first] = cdist;
                fr[node.first] = node.second;
                pll obj;
                while ((obj = query(0, 1, n, node.first)) != pll{-1, -1} &&
                       node.first <= obj.first) {
                    qu.push_back({obj.second, max(node.second, obj.first)});
                    erase(0, 1, n, a[obj.second].first);
                }
            }
            cdist++;
            sort(all(qu), [&](const pll &o1, const pll &o2) {
                return o1.second > o2.second;
            });
        }
    };

    vll t1(n + 1, INT_MAX), fr1(n + 1), t2(n + 1, INT_MAX), fr2(n + 1);
    cal(t1, fr1);
    reverse(a.begin() + 1, a.end());
    CFOR(i, 1, n) {
        a[i].first = n - a[i].first + 1;
        a[i].second = n - a[i].second + 1;
        swap(a[i].first, a[i].second);
    }
    cal(t2, fr2);
    reverse(t2.begin() + 1, t2.end());
    reverse(fr2.begin() + 1, fr2.end());
    CFOR(i, 1, n) if (fr2[i]) fr2[i] = n - fr2[i] + 1;
    vll mxt1(t1), mxt2(t2);
    RFOR(i, n, 1) mxt1[i] = min(mxt1[i], mxt1[i + 1]);
    CFOR(i, 2, n) mxt2[i] = min(mxt2[i], mxt2[i - 1]);
    // dbg(t1, fr1, t2, fr2, mxt1, mxt2);
    ll q;
    cin >> q;
    FOR(i, 0, q) {
        ll x;
        cin >> x;
        ll ans = min(t1[x] + mxt2[fr1[x]], t2[x] + mxt1[fr2[x]]);
        cout << (ans >= INT_MAX ? -1 : ans) << "\n";
        // cout << (t1[x] + t2[fr1[x]] >= INT_MAX ? -1 : t1[x] + t2[fr1[x]])
        //      << "\n";
    }
}

int main() {
#ifdef FASTIO
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#endif
#ifdef PRECISION
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
#endif
#ifdef FILE
    freopen((FILE + string(".in")).c_str(), "r", stdin);
    freopen((FILE + string(".out")).c_str(), "w", stdout);
#endif
#ifdef SINGLE
    solve();
#endif
#ifdef MULTIPLE
    ll t;
    cin >> t;
    for (ll i = 1; i <= t; i++) {
        solve();
    }
#endif
#ifdef GOOGLE
    ll t;
    cin >> t;
    for (ll i = 1; i <= t; i++) {
        cout << "Case #" << i << ": ";
        solve();
    }
#endif
}
# 결과 실행 시간 메모리 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 769 ms 30944 KB Output is correct
5 Correct 577 ms 28600 KB Output is correct
6 Correct 581 ms 29592 KB Output is correct
7 Correct 590 ms 29116 KB Output is correct
8 Correct 262 ms 22324 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 456 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 456 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 5 ms 604 KB Output is correct
17 Correct 5 ms 860 KB Output is correct
18 Correct 5 ms 604 KB Output is correct
19 Correct 5 ms 604 KB Output is correct
20 Correct 5 ms 604 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 3 ms 828 KB Output is correct
23 Correct 5 ms 828 KB Output is correct
24 Correct 4 ms 604 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 456 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 5 ms 604 KB Output is correct
17 Correct 5 ms 860 KB Output is correct
18 Correct 5 ms 604 KB Output is correct
19 Correct 5 ms 604 KB Output is correct
20 Correct 5 ms 604 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 3 ms 828 KB Output is correct
23 Correct 5 ms 828 KB Output is correct
24 Correct 4 ms 604 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 5 ms 604 KB Output is correct
28 Correct 5 ms 792 KB Output is correct
29 Correct 5 ms 824 KB Output is correct
30 Correct 4 ms 600 KB Output is correct
31 Correct 5 ms 820 KB Output is correct
# 결과 실행 시간 메모리 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 769 ms 30944 KB Output is correct
5 Correct 577 ms 28600 KB Output is correct
6 Correct 581 ms 29592 KB Output is correct
7 Correct 590 ms 29116 KB Output is correct
8 Correct 262 ms 22324 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 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 456 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 5 ms 604 KB Output is correct
25 Correct 5 ms 860 KB Output is correct
26 Correct 5 ms 604 KB Output is correct
27 Correct 5 ms 604 KB Output is correct
28 Correct 5 ms 604 KB Output is correct
29 Correct 4 ms 604 KB Output is correct
30 Correct 3 ms 828 KB Output is correct
31 Correct 5 ms 828 KB Output is correct
32 Correct 4 ms 604 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 5 ms 792 KB Output is correct
37 Correct 5 ms 824 KB Output is correct
38 Correct 4 ms 600 KB Output is correct
39 Correct 5 ms 820 KB Output is correct
40 Correct 806 ms 32860 KB Output is correct
41 Correct 608 ms 31112 KB Output is correct
42 Correct 615 ms 31068 KB Output is correct
43 Correct 639 ms 31900 KB Output is correct
44 Correct 606 ms 32200 KB Output is correct
45 Correct 489 ms 31628 KB Output is correct
46 Correct 195 ms 11760 KB Output is correct
47 Correct 628 ms 31620 KB Output is correct
48 Correct 408 ms 30416 KB Output is correct