답안 #972005

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972005 2024-04-29T17:24:39 Z idas Event Hopping (BOI22_events) C++11
0 / 100
45 ms 7124 KB
#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) int((x).size())
#define pb push_back
#define s second
#define f first

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;

const int INF=1e9;

const int N=1e5+10;
int n, q, s[N], e[N], gr[N], d[N];
set<pii> same;
bool in[N];

int main()
{
    FAST_IO;
    cin >> n >> q;

    vector<tiii> inf;
    FOR(i, 0, n)
    {
        cin >> s[i] >> e[i];
        inf.pb({s[i],e[i],i});
    }

    sort(inf.begin(), inf.end());

    int j=-1;
    for(auto it : inf){
        int a, b, i;
        tie(a, b, i)=it;

        if(j==-1){
            j=i;
            gr[i]=i;
            d[i]=0;
            continue;
        }

        if(s[j]<=s[i] && e[i]<=e[j]){
            in[i]=true;
            gr[i]=gr[j];
            d[i]=d[j]-1;
            continue;
        }

        if(s[i]<=e[j] && e[j]<=e[i]){
            if(e[j]==e[i]){
                same.insert({i,j});
                same.insert({j,i});
            }

            gr[i]=gr[j];
            d[i]=d[j]+1;
        }
        else{
            gr[i]=i;
            d[i]=0;
        }

        j=i;
    }

    while(q--){
        int a, b; cin >> a >> b; --a; --b;

        if(a==b){
            cout << 0 << '\n';
            continue;
        }

        if(same.count({a,b})){
            cout << 1 << '\n';
            continue;
        }

        if(in[a]){
            if(gr[b]!=gr[a] || d[b]<=d[a]){
                cout << "impossible\n";
            }
            else{
                cout << d[b]-d[a] << '\n';
            }
            continue;
        }

        if(in[b] || gr[a]!=gr[b] || d[b]<d[a]){
            cout << "impossible\n";
            continue;
        }

        cout << d[b]-d[a] << '\n';
    }

//    FOR(i, 0, n)
//    {
//        cout << gr[i] << " " << d[i] << '\n';
//    }
}
/*
5 5
1 1
1 2
2 3
4 5
5 6
1 2
2 3
3 3
3 4
4 5

3 3
1 2
2 3
3 3
3 2
1 3
3 1
*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 6724 KB Output is correct
2 Correct 44 ms 6596 KB Output is correct
3 Correct 45 ms 6608 KB Output is correct
4 Correct 40 ms 7112 KB Output is correct
5 Correct 43 ms 7124 KB Output is correct
6 Incorrect 43 ms 6856 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -