답안 #746278

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
746278 2023-05-22T07:08:11 Z Szil Event Hopping (BOI22_events) C++14
0 / 100
414 ms 131268 KB
#include <bits/stdc++.h>
using namespace std;
    
const int MAXN = 100001;
const int K = 20;

struct Event {
    int s, e, idx;

    bool operator<(const Event &x) const {
        return s < x.s;
    }
};

struct Node {
    Node *left, *right;
    Event best; int tl, tr;

    Node(int l_, int r_) : tl(l_), tr(r_) {
        best.s = INT_MAX;
        best.idx = 0;
    }
    
    void expand() {
        if (left == nullptr) {
            int tm = (tl + tr) / 2;
            left = new Node(tl, tm);
            right = new Node(tm+1, tr);
        }
    }
    
    void upd(int l, int r, Event val) {
        if (l > r) return;
        if (l == tl && r == tr) {
            best = min(best, val);
        } else {
            expand();
            int m = (tl + tr) / 2;
            left->upd(l, min(r, m), val);
            right->upd(max(l, m+1), r, val);
        }
    }
    
    Event qry(int pos) {
        if (tl == tr) {
            return best;
        } else {
            expand();
            int m = (tl + tr) / 2;
            if (pos <= m) {
                return min(best, left->qry(pos));
            } else {
                return min(best, right->qry(pos));
            }
        }
    }
};
    
int down[MAXN][K];
    
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, q; cin >> n >> q;
    Node *root = new Node(1, 2e9);
    vector<Event> events(n+1);
    for (int i = 1; i <= n; i++) {
        cin >> events[i].s >> events[i].e; events[i].idx = i;
        root->upd(events[i].s, events[i].e, events[i]);
    }

    for (int i = 1; i <= n; i++) {
        down[i][0] = root->qry(events[i].s).idx;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < K; j++) {
            down[i][j] = down[down[i][j-1]][j-1];
        }
    }

    while (q--) {
        int a_, b_; cin >> a_ >> b_;
        
        if (a_ == b_) {
            cout << "0\n";
            continue;
        }

        Event a = events[a_], b = events[b_];

        if (b.s <= a.e && a.e <= b.e) {
            cout << "1\n";
            continue;
        }

        int ans = 2;
        for (int i = K-1; i >= 0; i--) {
            int next = down[b.idx][i];
            if (next != 0 && a.e < events[next].s) {
                b = events[next];
                ans += (1 << i);
            }
        }

        b = events[down[b.idx][0]];
        if (b.s > a.e || a.e > b.e) {
            cout << "impossible\n";
            continue;
        }
        cout << ans << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 414 ms 131268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -