Submission #864209

# Submission time Handle Problem Language Result Execution time Memory
864209 2023-10-22T09:00:01 Z danikoynov Event Hopping (BOI22_events) C++14
0 / 100
1500 ms 3084 KB
#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 10;
     
struct interval
{
    int s, e, idx;
}seg[maxn];
     
int n, q;   
pair < int, int > task[maxn];

void input()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i ++)
    {
        cin >> seg[i].s >> seg[i].e;
        seg[i].idx = i;
    }
    for (int i = 1; i <= q; i ++)
    {
        cin >> task[i].first >> task[i].second;
    }
}


bool cmp_end(interval seg1, interval seg2)
{
    return seg1.e < seg2.e;
}

int lf[maxn], rev[maxn];
void build_graph()
{
    sort(seg + 1, seg + n + 1, cmp_end);
    for (int i = 1; i <= n; i ++)
    {
        rev[seg[i].idx] = i;
    }

    for (int i = 1; i <= n; i ++)
    {
        lf[i] = 1;
        while(seg[lf[i]].e < seg[i].s)
            lf[i] ++;
    }
}

void answer_queries()
{
    /**cout << "----------------------" << endl;
    for (int i = 1; i <= n; i ++)
        cout << seg[i].s << " :: " << seg[i].e << endl;*/
    
    for (int i = 1; i <= q; i ++)
    {
        int st = rev[task[i].first], en = rev[task[i].second];
        if (st > en)
        {
            cout << "impossible" << endl;
            continue;
        }

        int steps = 0, last = en + 1, bound = en;
        bool done = false;
        while(bound > st)
        {
            ///cout << last << " " << bound << endl;
            steps ++;
            int new_bound = bound;
            for (int i = bound; i < last; i ++)
                new_bound = min(new_bound, lf[i]);
            ///cout << new_bound << endl;
            if (new_bound == bound)
            {
                done = true;
                break;
            }

            last = bound;
            bound = new_bound;
        }

        if (done)
            cout << "impossible" << endl;
        else
            cout << steps << endl;
    }
}
void solve()
{  
    input();
    build_graph();
    answer_queries();
}

int main()
{   
    ///speed();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Execution timed out 1596 ms 2976 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Incorrect 1 ms 2396 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Incorrect 1 ms 2396 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Incorrect 1 ms 2396 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1552 ms 3084 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Execution timed out 1596 ms 2976 KB Time limit exceeded
3 Halted 0 ms 0 KB -