제출 #1351557

#제출 시각아이디문제언어결과실행 시간메모리
1351557umbraphileEvent Hopping (BOI22_events)C++20
0 / 100
1594 ms4428 KiB
/*author : umbraphile*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define kaneki ios::sync_with_stdio(0);cin.tie(nullptr)
#define TEST int t; cin >> t;  while (t --)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(),a.rend()
#define mat vector<vector<int>>
const int MOD = 1e9 + 7;
const int inf = 2e18;
const int aura = LLONG_MIN;
const int MAXN = 1e5 + 5;
const int LOG = 20;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};



// i dan j ga otish mumkun :
// if S_j <= E_i and E_i <= E_j
// x y kiritladi sen shortest path topasan xdan y ga yetib borish uchun
// maybe just dijkstra  no weihgt yogu bfs?
int S[MAXN];
int E[MAXN];
// int dist[MAXN];
int par[MAXN];
// void solve()
// {
//     int n,q; cin >> n >> q;
//     for(int i = 1; i <= n; ++ i) cin >> S[i] >> E[i];
//     while(q --)
//     {
//         int x, y; cin >> x >> y;
//         if(x==y){cout << 0 << "\n";continue;}
//         for(int i = 1; i <= n; ++ i) dist[i] = -1;
//         queue<int> q;
//         q.push(x);
//         dist[x] = 0;
//         bool ok = false;
//         while(!q.empty())
//         {
//             int d = q.front();
//             q.pop();
//             if(d == y){cout << dist[d] << "\n";ok = true;break;}
//             for(int i = 1; i <= n; ++ i){
//                 if(dist[i] == -1){
//                     if(S[i] <= E[d] && E[d] <= E[i])
//                     {
//                         dist[i] = dist[d] + 1;
//                         q.push(i);
//                     }
//                 }
//             }
//         }
//         if(!ok)  cout << "impossible\n";
//     }

    
// }




void solve()
{
    int n,q; cin >> n >> q;
    vector<pair<int, int>> a;
    for(int i = 1; i <= n; ++ i){
        cin >> S[i] >> E[i];
        par[i] = -1;
        a.push_back({S[i],i});
    }
    sort(all(a));
    for(int i = 1; i <= n; ++ i)
    {
        for(int j = 0; j < n; ++ j)
        {
            if(i == j) continue;
            int idx = a[j].second;
            if(idx == i) continue;
            if(S[idx] <= E[i] && E[i] <= E[idx]){par[i] = idx;break;}
            if(S[idx] > E[i]) break;
            if(S[j] <= E[i] && E[i] <= E[j]){par[i] = j;break;}
        }
    }
    while(q --)
    {
        int x, y; cin >> x >> y;
        if(x==y){cout << 0 << "\n";continue;}
        int d = x;
        int cnt = 0;
        bool ok = false;
        for(int i = 0; i < n; ++ i)
        {
            d = par[d];
            if(d == -1) break;
            cnt ++;
            if(d == y){ok = true;break;}
            if(E[d] > E[y]) break;
        }
        if(ok) cout << cnt << "\n";
        else cout << "impossible\n";
    }
}

signed main()
{
	//#ifndef LOCAL
    //freopen("input.txt", "mx", stdin);
    //freopen("output.txt", "x", stdout);
    //#endif
    kaneki;
    // pre();
    // sieve();
    solve();
}





#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...