Submission #849993

#TimeUsernameProblemLanguageResultExecution timeMemory
849993Ahmed57Event Hopping (BOI22_events)C++17
30 / 100
707 ms33796 KiB
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){
    return a.first.second<b.first.second;
}
int P[100001][17],ind[100001];
vector<int> adj[100001];
signed main(){
    int n,q;cin>>n>>q;
    if(n<=1000){
        vector<pair<int,int>> v;
    for(int i = 0;i<n;i++){
        int a,b;cin>>a>>b;
        v.push_back({a,b});
    }
    for(int i = 0;i<n;i++){
        for(int j = 0;j<n;j++){
            if(i!=j){
                if(v[j].first<=v[i].second&&v[i].second<=v[j].second){
                    adj[i].push_back(j);
                }
            }
        }
    }
    vector<pair<int,int>> qu[n];
    for(int i = 0;i<q;i++){
        int a,b;cin>>a>>b;
        a--;b--;
        qu[a].push_back({b,i});
    }
    int ans[q] = {0};
    int cost[n] = {0};
    for(int i = 0;i<n;i++){
        for(int j = 0;j<n;j++){
            cost[j] = 1e9;;
        }
        queue<int> q;q.push(i);
        cost[i] = 0;
        while(!q.empty()){
            int x = q.front();q.pop();
            for(auto j:adj[x]){
                if(cost[j]==1e9){
                    cost[j] = cost[x]+1;
                    q.push(j);
                }
            }
        }
        for(auto j:qu[i]){
            ans[j.second] = cost[j.first];
        }
    }
    for(int i = 0;i<q;i++){
        if(ans[i]==1e9)cout<<"impossible\n";
        else cout<<ans[i]<<"\n";
    }
    return 0;
    }
    vector<pair<pair<int,int>,int>> v;
    map<int,int> comp,sav;
    for(int i = 0;i<n;i++){
        int a,b;
        cin>>a>>b;
        comp[a]++;comp[b]++;
        v.push_back({{a,b},i});
    }
    long long z = 0;
    for(auto i:comp){
        sav[i.first] = ++z;
    }
    for(int i = 0;i<n;i++){
        v[i].first.first = sav[v[i].first.first];
        v[i].first.second = sav[v[i].first.second];
    }
    sort(v.begin(),v.end(),cmp);
    int in = 0;
    for(int i = 0;i<v.size();i++){
        ind[v[i].second] = i;
        while(in<n&&v[in].first.first<=v[i].first.second){
            in++;
        }
        P[i][0] = in-1;
    }
    for(int i = n-1;i>=0;i--){
        for(int j = 1;j<17;j++){
            P[i][j] = P[P[i][j-1]][j-1];
        }
    }
    while(q--){
        int a,b;cin>>a>>b;
        a--;b--;
        if(ind[a]>ind[b]){
        cout<<"impossible\n";
        continue;
        }else if(ind[a]==ind[b]){
        cout<<0<<endl;
        continue;
        }
        int sum = 0 , cur = ind[a];
        for(int i = 16;i>=0;i--){
            if(v[P[cur][i]].first.second<v[ind[b]].first.second){
                sum+=(1<<i);
                cur = P[cur][i];
            }
        }
        if(v[P[cur][0]].first.second>=v[ind[b]].first.second){
            cout<<sum+1<<endl;
        }else cout<<"impossible\n";
    }
}

Compilation message (stderr)

events.cpp: In function 'int main()':
events.cpp:76:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for(int i = 0;i<v.size();i++){
      |                   ~^~~~~~~~~
#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...