제출 #1255100

#제출 시각아이디문제언어결과실행 시간메모리
1255100chikien2009Event Hopping (BOI22_events)C++20
45 / 100
134 ms19016 KiB
#include <bits/stdc++.h>

using namespace std;

void setup()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int n, m, q, d, e, res;
int a[100000], b[100000], c[100000], sp1[20][100000], sp2[20][100000];
pair<int, int> p[100000];

inline void Update(int x, int y, int ind)
{
    if (sp1[x][y] == -1)
    {
        sp1[x][y] = ind;
    }
    else if (ind != -1 && a[sp1[x][y]] > a[ind])
    {
        sp1[x][y] = ind;
    }
}

inline int Get(int l, int r)
{
    int k = __lg(r - l + 1), p1 = sp1[k][l], p2 = sp1[k][r - (1 << k) + 1];
    if (p1 == -1)
    {
        return p2;
    }
    else if (p2 == -1)
    {
        return p1;
    }
    else
    {
        return (a[p1] < a[p2] ? p1 : p2);
    }
}

int main()
{
    setup();

    cin >> n >> q;
    for (int i = 0; i < 20; ++i)
    {
        fill_n(sp1[i], 1e5, -1);
        fill_n(sp2[i], 1e5, -1);
    }
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i] >> b[i];
        c[i] = b[i];
        p[i] = {b[i], i};
    }
    sort(c, c + n);
    m = unique(c, c + n) - c;
    for (int i = 0; i < n; ++i)
    {
        a[i] = lower_bound(c, c + m, a[i]) - c;
        b[i] = lower_bound(c, c + m, b[i]) - c;
        Update(0, b[i], i);
    }
    for (int i = 1; i < 20; ++i)
    {
        for (int j = 0; j + (1 << i) <= m; ++j)
        {
            sp1[i][j] = sp1[i - 1][j];
            Update(i, j, sp1[i - 1][j + (1 << (i - 1))]);
        }
    }
    sort(p, p + n);
    for (int z = 0, i; z < n; ++z)
    {
        i = p[z].second;
        d = Get(a[i], b[i]);
        sp2[0][i] = (d == i ? -1 : d);
        for (int j = 1; j < 20; ++j)
        {
            sp2[j][i] = (sp2[j - 1][i] == -1 ? -1 : sp2[j - 1][sp2[j - 1][i]]);
        }
    }
    while (q--)
    {
        cin >> d >> e;
        d--;
        e--;
        res = (d != e);
        if (a[e] <= b[d])
        {
            cout << (b[d] <= b[e] ? to_string(res) : "impossible") << "\n";
            continue;
        }
        for (int i = 19; i >= 0; --i)
        {
            if (sp2[i][e] != -1 && b[d] < a[sp2[i][e]])
            {
                res += (1 << i);
                e = sp2[i][e];
            }
        }
        cout << (sp2[0][e] != -1 && a[sp2[0][e]] <= b[d] ? to_string(res + 1) : "impossible") << "\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...