Submission #579626

#TimeUsernameProblemLanguageResultExecution timeMemory
579626EliasEvent Hopping (BOI22_events)C++17
0 / 100
1589 ms5012 KiB
#include <bits/stdc++.h>

#ifndef _DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#endif

using namespace std;

#define int int64_t

template <class T>
istream &operator>>(istream &in, vector<T> &v)
{
    for (T &x : v)
        in >> x;
    return in;
}

struct Range
{
    int s, e, i;

    bool operator<(Range other)
    {
        return tuple<int, int, int>{s, e, i} < tuple<int, int, int>{other.s, other.e, other.i};
    }
};

int n, q;

vector<Range> ranges, sR;

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

    cin >> n >> q;

    ranges = vector<Range>(n);

    for (int i = 0; i < n; i++)
    {
        cin >> ranges[i].s >> ranges[i].e;
        ranges[i].i = i;
    }

    sR = ranges;

    sort(sR.begin(), sR.end());

    while (q--)
    {
        int s, e;
        cin >> s >> e;
        s--, e--;

        if (s == e)
        {
            cout << "0\n";
            continue;
        }

        set<pair<int, int>> ongoing;

        int t = ranges[s].e;
        int i = 0;

        int cost = 0;

        while (true)
        {
            while (i < n && sR[i].s <= t)
            {
                if (sR[i].e >= t)
                    ongoing.insert({sR[i].e, sR[i].i});
                i++;
            }

            while (ongoing.size() && prev(ongoing.end())->first > ranges[e].e)
                ongoing.erase(prev(ongoing.end()));

            if (ongoing.size() == 0)
            {
                cout << "impossible\n";
                break;
            }

            cost++;

            if (prev(ongoing.end())->second == e)
            {
                cout << cost << "\n";
                break;
            }
            else
            {
                t = prev(ongoing.end())->first;
                ongoing = set<pair<int, int>>();
            }
        }
    }
}
#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...