Submission #1346392

#TimeUsernameProblemLanguageResultExecution timeMemory
1346392Jakub_WozniakEvent Hopping (BOI22_events)C++20
20 / 100
479 ms31116 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;
}

const int base = (1<<19);
pii drz[base*2];

pii Qu(int X)
{
    pii maxi = {-maxn,-maxn};
    X += base;
    while(X)
    {
        maxi = max(maxi , drz[X]);
        X /= 2;
    }
    return maxi;
}

void U(int l , int r , pii val)
{
    l += base;
    r += base;
    l--;
    r++;
    while(l/2 != r/2)
    {
        if((l&1) == 0)drz[l+1] = max(drz[l+1],val);
        if((r&1) == 1)drz[r-1] = max(drz[r-1],val);
        l /= 2;
        r /= 2;
    }
}

unordered_map<int,int> UM;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int N , Q ,x , y; 
    cin >> N >> Q;

    set<int> Si;
    for(int i = 1 ; i <= N ; i++)
    {
        cin >> site[i].st.st >> site[i].st.nd;
        Si.insert(site[i].st.st);
        Si.insert(site[i].st.nd);
        site[i].nd = i;
    }

    int licz = 1;
    for(auto p : Si)
    {
        UM[p] = licz;
        licz++;
    }

    for(int i = 1 ; i <= N ; i++)
    {
        site[i].st.st = UM[site[i].st.st];
        site[i].st.nd = UM[site[i].st.nd];
    }


    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 indi = 0 , maxiL = 0;
    for(int i = N ; i >= 1; i--)
    {
        ojc[i] = max((int)i,Qu(site[i].st.nd).nd);
        U(site[i].st.st , site[i].st.nd , {site[i].st.nd, i});
    }

        for(int i = N ; i >= 1; i--)
        {
            if(ojc[i] == i)
            {
                jump[i] = i;
                jumpsiz[i] = 1;
                continue;
            }

            if(jumpsiz[ojc[i]] == jumpsiz[jump[ojc[i]]] && jump[ojc[i]] != ojc[i])
            {
                jump[i] = jump[jump[ojc[i]]];
                jumpsiz[i] = jumpsiz[ojc[i]]*2+1;
            }
            else 
            {
                jump[i] = ojc[i];
                jumpsiz[i] = 1;
            }
        }

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

        
        if(Z.st == Z.nd)
        {
            res[ind] = 0;
            continue;
        }
        if(site[Z.st].st.nd > site[Z.nd].st.nd)
        {
            res[ind] = maxn*4+5;
            continue;
        }
        if(Z.st > Z.nd)
        {
            int akt = Z.st;
            if(site[Z.nd].st.st <= site[akt].st.nd && site[Z.nd].st.nd >= site[akt].st.nd)res[ind] = 1;
            else res[ind] = maxn*4+5;
            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)
        {
            int J = jump[akt];
            if(J != akt && site[J].st.nd < site[Z.nd].st.nd && J != ojc[akt])
            {
                resi += jumpsiz[akt];
                akt = J;
                continue;
            }
            resi++;
            akt = ojc[akt];
        }

        resi++;
        akt = ojc[akt];
        if(site[Z.nd].st.nd <= site[akt].st.nd);
        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...