Submission #597588

# Submission time Handle Problem Language Result Execution time Memory
597588 2022-07-16T11:04:27 Z boris_mihov Event Hopping (BOI22_events) C++14
55 / 100
119 ms 16128 KB
#include <algorithm>
#include <iostream>
#include <numeric>

const int MAXN = 100000 + 10;
const int MAXLOG = 17;
const int INF = 1e9 + 10;

struct Event
{
    int s, e;
    inline friend bool operator < (Event a, Event b)
    {
        return a.s < b.s || (a.s == b.s && a.e < b.e);
    }
} ev[MAXN];

int n, q;
int getLog[MAXN];
int rmq[MAXLOG][MAXN];
int lift[MAXLOG][MAXN];
int perm[MAXN], newNum[MAXN];

int cmp(int x, int y)
{
    if (ev[x].e >= ev[y].e) return x;
    return y;
}

void buildRMQ()
{
    std::iota(rmq[0]+1, rmq[0]+1+n, 1);
    for (int log = 1 ; (1 << log) <= n ; ++log)
    {
        for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
        {
            rmq[log][i] = cmp(rmq[log-1][i], rmq[log-1][i + (1 << log-1)]);
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        getLog[i] = getLog[i-1];
        if ((1 << getLog[i]+1) < i) ++getLog[i];
    }
}

int upperBinSearch(int value)
{
    int l = 1, r = n+1, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (ev[mid].s <= value) l = mid;
        else r = mid;
    }

    return l;
}

int lowerBinSearch(int idx)
{
    int l = 0, r = n, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (mid <= idx || ev[mid].s < ev[idx].s) l = mid;
        else r = mid;
    }

    return r;
}

int findMaximum(int l, int r)
{
    int log = getLog[r-l+1];
    return cmp(rmq[log][l], rmq[log][r - (1 << log) + 1]);
}

void buildLift()
{
    for (int log = 1 ; (1 << log) <= n ; ++log)
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            lift[log][i] = lift[log-1][lift[log-1][i]];
        }
    }
}

int binaryLifting(int from, int value)
{
    if (ev[from].s > value) return -1;
    int ans = 1 + (ev[from].e < value);
    for (int log = MAXLOG-1 ; log >= 0 ; --log)
    {
        if (lift[log][from] == 0) continue;
        if (ev[lift[log][from]].e < value)
        {
            ans += (1 << log);
            from = lift[log][from];   
        }
    }

    if (ev[lift[0][from]].e < value) return -1;
    else return ans;
}

void solve()
{
    std::iota(perm+1, perm+1+n, 1);
    std::sort(perm+1, perm+1+n, [&](int x, int y)
    {
        return ev[x] < ev[y];
    });

    for (int i = 1 ; i <= n ; ++i)
    {
        newNum[perm[i]] = i;
    }

    std::sort(ev+1, ev+1+n);
    buildRMQ();

    for (int i = 1 ; i <= n ; ++i)
    {
        int from = lowerBinSearch(i);
        int to = upperBinSearch(ev[i].e);
        if (i+1 <= from && from <= to)
        {
            int jump = findMaximum(from, to);
            lift[0][i] = jump;
            if (i == jump) lift[0][i] = 0;
        }
    }

    buildLift();
    for (int i = 1 ; i <= q ; ++i)
    {
        int st, en;
        std::cin >> st >> en; 
        std::swap(st, en);
       
        if (st == en)
        {
            std::cout << 0 << '\n';
            continue;
        }

        st = newNum[st];
        en = ev[newNum[en]].s;
        int res = binaryLifting(st, en);
        if (res == -1) std::cout << "impossible\n";
        else std::cout << res << '\n';
    }
}

void read()
{
    std::cin >> n >> q;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> ev[i].s >> ev[i].e;
        ev[i].s = INF - ev[i].s;
        ev[i].e = INF - ev[i].e;
        std::swap(ev[i].s, ev[i].e);
    }
}

void fastIO()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIO();
    read();
    solve();

    return 0;
}

Compilation message

events.cpp: In function 'void buildRMQ()':
events.cpp:37:70: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   37 |             rmq[log][i] = cmp(rmq[log-1][i], rmq[log-1][i + (1 << log-1)]);
      |                                                                   ~~~^~
events.cpp:44:28: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   44 |         if ((1 << getLog[i]+1) < i) ++getLog[i];
      |                   ~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 80 ms 15688 KB Output is correct
3 Correct 76 ms 15596 KB Output is correct
4 Correct 80 ms 15572 KB Output is correct
5 Correct 82 ms 15732 KB Output is correct
6 Correct 85 ms 15700 KB Output is correct
7 Correct 103 ms 15788 KB Output is correct
8 Correct 71 ms 15692 KB Output is correct
9 Correct 79 ms 15792 KB Output is correct
10 Correct 119 ms 15952 KB Output is correct
11 Correct 99 ms 16000 KB Output is correct
12 Correct 59 ms 16128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 27 ms 1484 KB Output is correct
20 Correct 34 ms 1424 KB Output is correct
21 Correct 31 ms 1740 KB Output is correct
22 Incorrect 27 ms 1820 KB Output isn't correct
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 56 ms 15064 KB Output is correct
20 Correct 57 ms 15060 KB Output is correct
21 Correct 67 ms 15040 KB Output is correct
22 Correct 56 ms 15064 KB Output is correct
23 Correct 51 ms 14964 KB Output is correct
24 Correct 53 ms 15060 KB Output is correct
25 Correct 36 ms 15012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 15616 KB Output is correct
2 Correct 79 ms 15644 KB Output is correct
3 Correct 90 ms 15576 KB Output is correct
4 Correct 79 ms 15740 KB Output is correct
5 Correct 99 ms 15996 KB Output is correct
6 Correct 98 ms 15832 KB Output is correct
7 Correct 110 ms 15788 KB Output is correct
8 Correct 116 ms 15932 KB Output is correct
9 Correct 50 ms 15056 KB Output is correct
10 Correct 88 ms 15412 KB Output is correct
11 Correct 88 ms 15240 KB Output is correct
12 Correct 84 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 80 ms 15688 KB Output is correct
3 Correct 76 ms 15596 KB Output is correct
4 Correct 80 ms 15572 KB Output is correct
5 Correct 82 ms 15732 KB Output is correct
6 Correct 85 ms 15700 KB Output is correct
7 Correct 103 ms 15788 KB Output is correct
8 Correct 71 ms 15692 KB Output is correct
9 Correct 79 ms 15792 KB Output is correct
10 Correct 119 ms 15952 KB Output is correct
11 Correct 99 ms 16000 KB Output is correct
12 Correct 59 ms 16128 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 27 ms 1484 KB Output is correct
32 Correct 34 ms 1424 KB Output is correct
33 Correct 31 ms 1740 KB Output is correct
34 Incorrect 27 ms 1820 KB Output isn't correct
35 Halted 0 ms 0 KB -