답안 #722688

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
722688 2023-04-12T16:12:13 Z dxz05 Event Hopping (BOI22_events) C++17
25 / 100
1500 ms 19164 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;

template <typename T>
struct SegmentTree{
    int n = 0;
    vector<T> tree;

    T neutral_element = make_pair(1e9, -1); // e.g. 0 for sum, INF for min ...

    SegmentTree(){};
    SegmentTree(int _n){
        n = _n;
        tree.assign(n * 4 + 5, neutral_element);
    }

    inline T combine(T lf, T rg){
        return min(lf, rg); // change it
    }

    inline void build(int v, int tl, int tr, const vector<T> &a){
        if (tl == tr){
            tree[v] = a[tl];
            return;
        }
        int tm = (tl + tr) >> 1;
        build(v << 1, tl, tm, a);
        build(v << 1 | 1, tm + 1, tr, a);
        tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
    }

    inline void init(const vector<T> &a){
        n = (int)a.size();
        tree.assign(n * 4 + 5, neutral_element);
        build(1, 0, n - 1, a);
    }

    inline void update(int v, int tl, int tr, int pos, T val){
        if (tl == tr){
            tree[v] = min(tree[v], val); // pay attention!
            return;
        }
        int tm = (tl + tr) >> 1;
        if (pos <= tm){
            update(v << 1, tl, tm, pos, val);
        } else {
            update(v << 1 | 1, tm + 1, tr, pos, val);
        }
        tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
    }

    inline void update(int pos, T val){
        update(1, 0, n - 1, pos, val);
    }

    inline T get(int v, int tl, int tr, int l, int r){
        if (l <= tl && tr <= r) return tree[v];
        if (tl > r || tr < l) return neutral_element;
        int tm = (tl + tr) >> 1;
        return combine(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
    }

    inline T get(int l, int r){
        return get(1, 0, n - 1, l, r);
    }

};

int f[N];
int l[N], r[N];

bool can_switch(int i, int j){
    return l[j] <= r[i] && r[i] <= r[j];
}

void solve(){
    int n, q;
    cin >> n >> q;

    map<int, int> mp;
    for (int i = 0; i < n; i++){
        cin >> l[i] >> r[i];
        mp[l[i]], mp[r[i]];
    }

    int z = 0;
    for (auto &now : mp) now.second = z++;

    SegmentTree<pair<int, int>> st(n * 2);
    for (int i = 0; i < n; i++) {
        st.update(mp[r[i]], make_pair(mp[l[i]], i));
    }

    for (int i = 0; i < n; i++){
        int j = st.get(mp[l[i]], mp[r[i]]).second;
        f[i] = (j != -1 && l[j] < l[i] ? j : -1);
    }

    while (q--){
        int x, y;
        cin >> x >> y;
        --x, --y;
        if (x == y){
            cout << 0 << endl;
            continue;
        }
        if (can_switch(x, y)){
            cout << 1 << endl;
            continue;
        }
        int cnt = 1;
        while (l[y] > r[x]){
            y = f[y];
            cnt++;
            if (y == -1) break;
        }

        if (y != -1 && can_switch(x, y)){
            cout << cnt << endl;
        } else {
            cout << "impossible" << endl;
        }
    }

}

int main() {
    ios_base::sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Execution timed out 1519 ms 13044 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 476 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 476 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 632 ms 1764 KB Output is correct
20 Execution timed out 1510 ms 1796 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 476 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 2 ms 472 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 354 ms 12712 KB Output is correct
20 Correct 229 ms 13648 KB Output is correct
21 Correct 269 ms 14272 KB Output is correct
22 Correct 290 ms 16140 KB Output is correct
23 Correct 324 ms 19164 KB Output is correct
24 Correct 395 ms 18996 KB Output is correct
25 Correct 49 ms 8908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1540 ms 13200 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Execution timed out 1519 ms 13044 KB Time limit exceeded
3 Halted 0 ms 0 KB -