Submission #1351537

#TimeUsernameProblemLanguageResultExecution timeMemory
1351537umbraphileEvent Hopping (BOI22_events)C++20
10 / 100
1595 ms2776 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];
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])
                    {
                        // cout << dist[i] << " ";
                        dist[i] = dist[d] + 1;
                        // cout << dist[i] << "\n";
                        q.push(i);
                    }
                }
            }
        }
        if(!ok)  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...