답안 #746009

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
746009 2023-05-21T10:33:06 Z vjudge1 Event Hopping (BOI22_events) C++17
100 / 100
245 ms 50524 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 1e5;
const int maxwidth = 2 * maxn;
const int inf = maxwidth + 1;
const int logn = 20;

void print_impossible() {
    cout << "impossible\n";
    return;
}

struct interval {
    int l, r, id;
    bool operator < (const interval & other) const {
        return l < other.l;
    }
};

const interval def = {inf, inf, 0};

vector<interval> v;
interval arr[1 + maxwidth];
interval tree[1 + 4 * maxwidth];

void reset() {
    for(int i = 0; i <= maxwidth; i++) {
        arr[i] = def;
    }
    for(int i = 0; i <= 4 * maxwidth; i++) {
        tree[i] = def;
    }
}

void build(int v, int vl, int vr) {
    // cout << v << " " << vl << " " << vr << endl;
    if(vl == vr) {
        tree[v] = arr[vl];
        return;
    }
    int mid = (vl + vr) / 2;
    build(2 * v, vl, mid);
    build(2 * v  + 1, mid + 1, vr);
    tree[v] = min(tree[2 * v], tree[2 * v + 1]);
}

interval query(int v, int vl, int vr, int ql, int qr) {
    if(vr < ql || vl > qr) {
        return def;
    }
    if(vl == ql && vr == qr) {
        return tree[v];
    }
    int mid = (vl + vr) / 2;
    return min(query(2 * v, vl, mid, ql, min(qr, mid)), query(2 * v + 1, mid + 1, vr, max(ql, mid + 1), qr));
}

int n, q;
vector<pii > queries;
int par[1 + maxn];
int lift[1 + maxn][logn];

void compress() {
    vector<int> comp;
    for(const interval &cur : v) {
        comp.pb(cur.l), comp.pb(cur.r);
    }
    sort(comp.begin(), comp.end());
    comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
    for(interval &cur : v) {
        cur.l = (lower_bound(comp.begin(), comp.end(), cur.l) - comp.begin()) + 1;
        cur.r = (lower_bound(comp.begin(), comp.end(), cur.r) - comp.begin()) + 1;
    }
    return;
}

bool can_step(const interval &a, const interval &b) {
    return (b.l <= a.r && a.r <= b.r);
}

int ans_query(int s, int e) {
    if(s == e) {
        return 0;
    }
    interval si = v[s - 1], ei = v[e - 1];
    if(si.r > ei.r) {
        return -1;
    }
    if(can_step(si, ei)) {
        return 1;
    }
    int cur = e, cnt = 0;
    for(int j = logn - 1; j >= 0; j--) {
        int possible_new_candidate_option = lift[cur][j];
        if(possible_new_candidate_option == 0) {
            continue;
        }
        interval other = v[possible_new_candidate_option - 1];
        if(other.l > si.r) {
            cur = possible_new_candidate_option;
            cnt += (1 << j);
        }
    }
    cur = par[cur];
    cnt++;
    if(!can_step(si, v[cur - 1])) {
        return -1;
    }
    cnt++;
    return cnt;
}

void solve() {
    cin >> n >> q;
    for(int i = 1; i <= n; i++) {
        int l, r;
        cin >> l >> r;
        v.pb({l, r, i});
    }
    for(int i = 1; i <= q; i++) {
        int s, e;
        cin >> s >> e;
        queries.pb({s, e});
    }
    compress();
    reset();
    for(const interval &cur : v) {
        arr[cur.r] = min(arr[cur.r], cur);
    }
    /*for(const interval &cur : v) {
        cout << cur.l << " " << cur.r << " " << cur.id << endl;
    }*/
    build(1, 1, maxwidth);
    //cout << "here" << endl;
    for(const interval &cur : v) {
        par[cur.id] = query(1, 1, maxwidth, cur.l, cur.r).id;
    }
    /*for(int i = 1; i <= n; i++) {
        cout << par[i] << " ";
    }
    cout << endl;*/
    for(int i = 1; i <= n; i++) {
        lift[i][0] = par[i];
    }
    for(int j = 1; j < logn; j++) {
        for(int i = 1; i <= n; i++) {
            lift[i][j] = lift[lift[i][j - 1]][j - 1];
        }
    }
    for(const pii &p : queries) {
        int s = p.fi, e = p.se;
        int res = ans_query(s, e);
        if(res == -1) {
            print_impossible();
        } else {
            cout << res << "\n";
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 155 ms 47324 KB Output is correct
3 Correct 172 ms 47356 KB Output is correct
4 Correct 218 ms 47296 KB Output is correct
5 Correct 171 ms 47384 KB Output is correct
6 Correct 176 ms 47448 KB Output is correct
7 Correct 179 ms 47492 KB Output is correct
8 Correct 157 ms 50336 KB Output is correct
9 Correct 155 ms 50524 KB Output is correct
10 Correct 210 ms 50372 KB Output is correct
11 Correct 176 ms 50296 KB Output is correct
12 Correct 115 ms 49600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 15 ms 23952 KB Output is correct
4 Correct 12 ms 24052 KB Output is correct
5 Correct 13 ms 24044 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 13 ms 24020 KB Output is correct
8 Correct 12 ms 24040 KB Output is correct
9 Correct 13 ms 24028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 15 ms 23952 KB Output is correct
4 Correct 12 ms 24052 KB Output is correct
5 Correct 13 ms 24044 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 13 ms 24020 KB Output is correct
8 Correct 12 ms 24040 KB Output is correct
9 Correct 13 ms 24028 KB Output is correct
10 Correct 11 ms 23708 KB Output is correct
11 Correct 14 ms 23764 KB Output is correct
12 Correct 13 ms 24020 KB Output is correct
13 Correct 15 ms 24020 KB Output is correct
14 Correct 14 ms 24024 KB Output is correct
15 Correct 15 ms 24020 KB Output is correct
16 Correct 15 ms 24008 KB Output is correct
17 Correct 14 ms 24020 KB Output is correct
18 Correct 16 ms 24020 KB Output is correct
19 Correct 44 ms 27812 KB Output is correct
20 Correct 45 ms 28076 KB Output is correct
21 Correct 48 ms 28240 KB Output is correct
22 Correct 38 ms 28368 KB Output is correct
23 Correct 37 ms 28168 KB Output is correct
24 Correct 37 ms 28164 KB Output is correct
25 Correct 40 ms 27792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 15 ms 23952 KB Output is correct
4 Correct 12 ms 24052 KB Output is correct
5 Correct 13 ms 24044 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 13 ms 24020 KB Output is correct
8 Correct 12 ms 24040 KB Output is correct
9 Correct 13 ms 24028 KB Output is correct
10 Correct 12 ms 23812 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 13 ms 24044 KB Output is correct
13 Correct 13 ms 24028 KB Output is correct
14 Correct 13 ms 24028 KB Output is correct
15 Correct 17 ms 23952 KB Output is correct
16 Correct 15 ms 24032 KB Output is correct
17 Correct 12 ms 24020 KB Output is correct
18 Correct 14 ms 23972 KB Output is correct
19 Correct 122 ms 45128 KB Output is correct
20 Correct 120 ms 45700 KB Output is correct
21 Correct 126 ms 46492 KB Output is correct
22 Correct 121 ms 46528 KB Output is correct
23 Correct 155 ms 46560 KB Output is correct
24 Correct 149 ms 46488 KB Output is correct
25 Correct 96 ms 45900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 46996 KB Output is correct
2 Correct 164 ms 47124 KB Output is correct
3 Correct 208 ms 47132 KB Output is correct
4 Correct 155 ms 47612 KB Output is correct
5 Correct 174 ms 47716 KB Output is correct
6 Correct 245 ms 47696 KB Output is correct
7 Correct 193 ms 49936 KB Output is correct
8 Correct 177 ms 50152 KB Output is correct
9 Correct 171 ms 46512 KB Output is correct
10 Correct 183 ms 49656 KB Output is correct
11 Correct 236 ms 49400 KB Output is correct
12 Correct 185 ms 49576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 155 ms 47324 KB Output is correct
3 Correct 172 ms 47356 KB Output is correct
4 Correct 218 ms 47296 KB Output is correct
5 Correct 171 ms 47384 KB Output is correct
6 Correct 176 ms 47448 KB Output is correct
7 Correct 179 ms 47492 KB Output is correct
8 Correct 157 ms 50336 KB Output is correct
9 Correct 155 ms 50524 KB Output is correct
10 Correct 210 ms 50372 KB Output is correct
11 Correct 176 ms 50296 KB Output is correct
12 Correct 115 ms 49600 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 15 ms 23952 KB Output is correct
16 Correct 12 ms 24052 KB Output is correct
17 Correct 13 ms 24044 KB Output is correct
18 Correct 14 ms 24012 KB Output is correct
19 Correct 13 ms 24020 KB Output is correct
20 Correct 12 ms 24040 KB Output is correct
21 Correct 13 ms 24028 KB Output is correct
22 Correct 11 ms 23708 KB Output is correct
23 Correct 14 ms 23764 KB Output is correct
24 Correct 13 ms 24020 KB Output is correct
25 Correct 15 ms 24020 KB Output is correct
26 Correct 14 ms 24024 KB Output is correct
27 Correct 15 ms 24020 KB Output is correct
28 Correct 15 ms 24008 KB Output is correct
29 Correct 14 ms 24020 KB Output is correct
30 Correct 16 ms 24020 KB Output is correct
31 Correct 44 ms 27812 KB Output is correct
32 Correct 45 ms 28076 KB Output is correct
33 Correct 48 ms 28240 KB Output is correct
34 Correct 38 ms 28368 KB Output is correct
35 Correct 37 ms 28168 KB Output is correct
36 Correct 37 ms 28164 KB Output is correct
37 Correct 40 ms 27792 KB Output is correct
38 Correct 12 ms 23812 KB Output is correct
39 Correct 12 ms 23764 KB Output is correct
40 Correct 13 ms 24044 KB Output is correct
41 Correct 13 ms 24028 KB Output is correct
42 Correct 13 ms 24028 KB Output is correct
43 Correct 17 ms 23952 KB Output is correct
44 Correct 15 ms 24032 KB Output is correct
45 Correct 12 ms 24020 KB Output is correct
46 Correct 14 ms 23972 KB Output is correct
47 Correct 122 ms 45128 KB Output is correct
48 Correct 120 ms 45700 KB Output is correct
49 Correct 126 ms 46492 KB Output is correct
50 Correct 121 ms 46528 KB Output is correct
51 Correct 155 ms 46560 KB Output is correct
52 Correct 149 ms 46488 KB Output is correct
53 Correct 96 ms 45900 KB Output is correct
54 Correct 145 ms 46996 KB Output is correct
55 Correct 164 ms 47124 KB Output is correct
56 Correct 208 ms 47132 KB Output is correct
57 Correct 155 ms 47612 KB Output is correct
58 Correct 174 ms 47716 KB Output is correct
59 Correct 245 ms 47696 KB Output is correct
60 Correct 193 ms 49936 KB Output is correct
61 Correct 177 ms 50152 KB Output is correct
62 Correct 171 ms 46512 KB Output is correct
63 Correct 183 ms 49656 KB Output is correct
64 Correct 236 ms 49400 KB Output is correct
65 Correct 185 ms 49576 KB Output is correct
66 Correct 11 ms 23760 KB Output is correct
67 Correct 143 ms 49816 KB Output is correct
68 Correct 159 ms 49796 KB Output is correct
69 Correct 225 ms 49796 KB Output is correct
70 Correct 194 ms 50000 KB Output is correct
71 Correct 157 ms 49872 KB Output is correct
72 Correct 170 ms 49980 KB Output is correct
73 Correct 164 ms 50292 KB Output is correct
74 Correct 162 ms 50344 KB Output is correct
75 Correct 185 ms 50236 KB Output is correct
76 Correct 188 ms 50224 KB Output is correct
77 Correct 111 ms 49568 KB Output is correct
78 Correct 12 ms 23764 KB Output is correct
79 Correct 14 ms 24048 KB Output is correct
80 Correct 15 ms 23952 KB Output is correct
81 Correct 16 ms 24152 KB Output is correct
82 Correct 18 ms 24044 KB Output is correct
83 Correct 14 ms 24020 KB Output is correct
84 Correct 13 ms 24020 KB Output is correct
85 Correct 14 ms 24056 KB Output is correct
86 Correct 45 ms 27920 KB Output is correct
87 Correct 55 ms 27988 KB Output is correct
88 Correct 46 ms 28252 KB Output is correct
89 Correct 38 ms 28336 KB Output is correct
90 Correct 37 ms 28232 KB Output is correct
91 Correct 39 ms 28196 KB Output is correct
92 Correct 46 ms 27796 KB Output is correct
93 Correct 140 ms 46564 KB Output is correct
94 Correct 127 ms 45696 KB Output is correct
95 Correct 129 ms 46396 KB Output is correct
96 Correct 124 ms 46496 KB Output is correct
97 Correct 175 ms 46512 KB Output is correct
98 Correct 162 ms 46524 KB Output is correct
99 Correct 86 ms 45852 KB Output is correct
100 Correct 209 ms 49960 KB Output is correct
101 Correct 206 ms 49992 KB Output is correct
102 Correct 190 ms 50092 KB Output is correct
103 Correct 201 ms 49596 KB Output is correct
104 Correct 239 ms 49376 KB Output is correct
105 Correct 181 ms 49608 KB Output is correct
106 Correct 172 ms 49304 KB Output is correct
107 Correct 203 ms 49300 KB Output is correct
108 Correct 183 ms 49992 KB Output is correct
109 Correct 167 ms 50000 KB Output is correct
110 Correct 207 ms 49940 KB Output is correct
111 Correct 208 ms 49824 KB Output is correct