제출 #1346317

#제출 시각아이디문제언어결과실행 시간메모리
1346317Jakub_WozniakEvent Hopping (BOI22_events)C++20
10 / 100
1596 ms12844 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define st first
#define nd second
const int maxn = (1e+5)+9;
pair<pii,int> site[maxn];
int kol[maxn];
int res[maxn];
vector <pair<pii,int>> V;
int ojc[maxn];
int jump[maxn] , jumpsiz[maxn];

bool cmp(pair<pii,int> p1 , pair<pii,int> p2)
{
    if(p1.st.nd == p2.st.nd)
    {
        return p1.st.st < p2.st.st;
    }
    return p1.st.nd < p2.st.nd;
}

int main()
{

    int N , Q ,x , y; 
    cin >> N >> Q;
    for(int i = 1 ; i <= N ; i++)
    {
        cin >> site[i].st.st >> site[i].st.nd;
        site[i].nd = i;
    }

    sort(site+1,site+N+1,cmp);
    for(int i = 1 ; i <= N ; i++)
    {
        cerr << i << ": " << site[i].st.st << ' ' << site[i].st.nd << "  " << site[i].nd << '\n';
        kol[site[i].nd] = i; 
    }

    for(int i = 0 ; i < Q; i++)
    {
        cin >> x >> y;
        V.push_back({{kol[x],kol[y]},i});
    }

    sort(V.begin() , V.end() , cmp);

    int lst = 0 , ind; pii Z;
    for(auto e : V)
    {
        Z = e.st;
        ind = e.nd;

        while(lst < Z.nd) // O(n*n)
        {
            lst++;
            for(int i = 1 ; i < lst ; i++)
            {
                if(site[lst].st.st <= site[i].st.nd && site[ojc[i]].st.nd < site[lst].st.nd)
                {
                    ojc[i] = lst;
                }
            }
            ojc[lst] = lst;

            cerr << lst << ": ";
            for(int i = 1; i <= lst ; i++)cerr << ojc[i] << ' ' ; cerr << '\n';
        }

        if(Z.st == Z.nd)
        {
            res[ind] = 0;
            continue;
        }

        int akt = Z.st;
        int resi = 0;
        while(ojc[akt] != akt && site[ojc[akt]].st.nd < site[Z.nd].st.nd) // O(n*Q)
        {
            resi++;
            akt = ojc[akt];
        }

        if(site[Z.nd].st.st <= site[akt].st.nd)
        {
            resi++;
        }
        else
        {
            resi++;
            akt = ojc[akt];
            if(site[Z.nd].st.st <= site[akt].st.nd)
            {
                resi++;
            }
            else resi = maxn*4+5;
        }

        res[ind] = resi;
    }

    for(int i =0 ; i < Q; i++)
    {
        if(res[i] == maxn*4+5)cout << "impossible\n";
        else cout << res[i] << '\n';
    }
    return 0;
}


#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...