답안 #722706

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
722706 2023-04-12T17:05:45 Z dxz05 Event Hopping (BOI22_events) C++17
25 / 100
1500 ms 24012 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][18];
int l[N], r[N];

int get_parent(int v, int k){
    while (k > 0){
        if (v == -1) break;
        int i = __builtin_ctz(k);
        v = f[v][i];
    }
    return v;
}

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][0] = (j != -1 && l[j] < l[i] ? j : -1);
    }

    for (int j = 1; j < 18; j++){
        for (int i = 0; i < n; i++){
            int mid = f[i][j - 1];
            f[i][j] = (mid != -1 ? f[mid][j - 1] : -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 (y != -1 && l[y] > r[x]) y = f[y][0], cnt++;

        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 0 ms 212 KB Output is correct
2 Execution timed out 1578 ms 19096 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 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 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 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 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 3 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 596 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1229 ms 2376 KB Output is correct
20 Execution timed out 1571 ms 1872 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 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 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 212 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 552 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 793 ms 19452 KB Output is correct
20 Correct 291 ms 19276 KB Output is correct
21 Correct 231 ms 19324 KB Output is correct
22 Correct 274 ms 21120 KB Output is correct
23 Correct 319 ms 24012 KB Output is correct
24 Correct 351 ms 24012 KB Output is correct
25 Correct 58 ms 14632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1568 ms 19072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1578 ms 19096 KB Time limit exceeded
3 Halted 0 ms 0 KB -