답안 #868530

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
868530 2023-10-31T16:55:45 Z Dec0Dedd Event Hopping (BOI22_events) C++14
0 / 100
61 ms 21888 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int N = 1e5+10;
const int K = 5;
const int INF = 1e6+10;

int up[K][N], d[K][N], s[N], lf[N], e[N], mx[N], n, q;
vector<pii> v;
vector<int> add[N], rm[N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    
    cin>>n>>q;
    for (int i=0; i<n; ++i) {
        cin>>s[i]>>e[i];
        v.push_back({e[i], i});
    } sort(v.begin(), v.end());

    for (int i=0; i<n; ++i) {
        int j=lower_bound(v.begin(), v.end(), make_pair(s[v[i].second], -1))-v.begin();
        lf[i]=j;
        add[lf[i]].push_back(v[i].second);
        rm[i+1].push_back(v[i].second);
    }

    multiset<pii> ms;
    for (int i=0; i<n; ++i) {
        for (auto u : add[i]) ms.insert({e[u], u});
        for (auto u : rm[i]) ms.erase(ms.find({e[u], u}));
        mx[i]=ms.rbegin()->second;
    }

    for (int i=n-1; i>=0; --i) {
        up[0][i]=mx[i], d[0][i]=(mx[i] != i);
        for (int j=1; j<K; ++j) {
            up[j][i]=up[j-1][up[j-1][i]];
            d[j][i]=d[j-1][i]+d[j-1][up[j-1][i]];
        }
    }

    while (q--) {
        int l, r; cin>>l>>r; --l, --r;
        if (e[r] < e[l]) {
            cout<<"impossible\n";
            continue;
        }

        int ans=0;
        for (int j=K-1; j>=0; --j) {
            if (up[j][l] <= r) ans+=d[j][l], l=up[j][l];
        }

        if (l == r) ans+=0;
        else {
            if (s[r] <= e[l]) ++ans, l=r;
            else {
                cout<<"impossible\n";
                continue;
            }
        }

        assert(l == r);
        cout<<ans<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Incorrect 56 ms 21788 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Incorrect 2 ms 8796 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Incorrect 2 ms 8796 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Incorrect 2 ms 8796 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 61 ms 21888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Incorrect 56 ms 21788 KB Output isn't correct
3 Halted 0 ms 0 KB -