답안 #711974

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711974 2023-03-17T19:49:34 Z Johann Event Hopping (BOI22_events) C++14
100 / 100
245 ms 19568 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef tuple<int, int, int> tiii;
typedef vector<tiii> vtiii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

struct event
{
    int s, e, idx;
};

struct segtree
{
    vpii arr;
    int size;
    void init(vector<event> &E)
    {
        size = 1;
        while (size < sz(E))
            size *= 2;
        arr.assign(2 * size, {INT_MAX, -1});
        for (int i = 0; i < sz(E); ++i)
            arr[i + size] = {E[i].s, E[i].idx};
    }
    void setMin(int l, int r, pii v)
    {
        setMin(l, r, v, 1, 0, size);
    }
    void setMin(int l, int r, pii v, int x, int lx, int rx)
    {
        if (l <= lx && rx <= r)
        {
            arr[x] = min(arr[x], v);
            return;
        }
        if (r <= lx || rx <= l)
            return;
        int m = (lx + rx) / 2;
        setMin(l, r, v, 2 * x, lx, m);
        setMin(l, r, v, 2 * x + 1, m, rx);
    }
    pii queryMin(int i) { return queryMin(i, 1, 0, size); }
    pii queryMin(int i, int x, int lx, int rx)
    {
        if (rx - lx == 1)
            return arr[x];
        int m = (lx + rx) / 2;
        pii tmp;
        if (i < m)
            tmp = queryMin(i, 2 * x, lx, m);
        else
            tmp = queryMin(i, 2 * x + 1, m, rx);
        return min(tmp, arr[x]);
    }
};

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

    int N, Q;
    cin >> N >> Q;

    vector<event> E(N);
    vpii Eb(N);
    for (int i = 0; i < N; ++i)
    {
        cin >> E[i].s >> E[i].e;
        E[i].idx = i;
        Eb[i] = {E[i].s, E[i].e};
    }

    sort(all(E), [](event a, event b)
         { return a.s < b.s; });
    vi posInSeg(N);
    for (int i = 0; i < N; ++i)
        posInSeg[E[i].idx] = i;
    segtree seg;
    seg.init(E);

    vvi vorg(N, vi(ceil(log2(N)) + 1));

    auto cmp = [&](int a, int b)
    { if (Eb[a].second == Eb[b].second) return Eb[a].first > Eb[b].first; else return Eb[a].second > Eb[b].second; };
    priority_queue<int, vi, decltype(cmp)> pq(cmp);
    for (int i = 0; i < N; ++i)
        pq.push(i);

    int pointerI = 0;
    while (!pq.empty())
    {
        // move border of starts
        int nextBorder = Eb[pq.top()].second;
        while (pointerI < sz(E) && E[pointerI].s <= nextBorder)
            ++pointerI;
        // add correspondinge elements to segtree
        // TODO: I guess there is no need for a second queue todo
        int idx = pq.top();
        pq.pop();
        seg.setMin(0, pointerI, {Eb[idx].first, idx});
        vorg[idx][0] = seg.queryMin(posInSeg[idx]).second;
    }
    for (int j = 1; j < sz(vorg[0]); ++j)
        for (int i = 0; i < N; ++i)
            vorg[i][j] = vorg[vorg[i][j - 1]][j - 1];

    for (int q = 0, s, e; q < Q; ++q)
    {
        cin >> s >> e;
        --s, --e;
        if (s == e)
        {
            cout << 0 << "\n";
            continue;
        }
        if (Eb[e].first <= Eb[s].second)
        {
            if (Eb[s].second <= Eb[e].second)
                cout << 1 << "\n";
            else // then (Eb[s].second > Eb[e].second)
                cout << "impossible\n";
            continue;
        }

        int ans = 0;
        int tmp = e;
        for (int j = sz(vorg[tmp]) - 1; j >= 0; --j)
        {
            tmp = vorg[e][j];
            if (Eb[s].second < Eb[tmp].first)
            {
                e = tmp;
                ans += (1 << j);
            }
        }
        tmp = vorg[e][0];
        if (Eb[tmp].first <= Eb[s].second)
            cout << ans + 2 << "\n";
        else
            cout << "impossible\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 162 ms 19112 KB Output is correct
3 Correct 178 ms 19148 KB Output is correct
4 Correct 242 ms 19044 KB Output is correct
5 Correct 187 ms 19112 KB Output is correct
6 Correct 178 ms 19148 KB Output is correct
7 Correct 180 ms 19152 KB Output is correct
8 Correct 147 ms 19524 KB Output is correct
9 Correct 135 ms 19568 KB Output is correct
10 Correct 196 ms 19404 KB Output is correct
11 Correct 178 ms 19432 KB Output is correct
12 Correct 96 ms 18760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 1 ms 476 KB Output is correct
13 Correct 1 ms 476 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 472 KB Output is correct
17 Correct 1 ms 472 KB Output is correct
18 Correct 1 ms 472 KB Output is correct
19 Correct 35 ms 2508 KB Output is correct
20 Correct 38 ms 2508 KB Output is correct
21 Correct 47 ms 2724 KB Output is correct
22 Correct 28 ms 2764 KB Output is correct
23 Correct 27 ms 2684 KB Output is correct
24 Correct 24 ms 2624 KB Output is correct
25 Correct 30 ms 2260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 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 460 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 360 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 124 ms 17380 KB Output is correct
20 Correct 118 ms 16720 KB Output is correct
21 Correct 143 ms 17360 KB Output is correct
22 Correct 119 ms 17492 KB Output is correct
23 Correct 95 ms 17416 KB Output is correct
24 Correct 117 ms 17396 KB Output is correct
25 Correct 70 ms 16768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 19124 KB Output is correct
2 Correct 167 ms 19096 KB Output is correct
3 Correct 236 ms 19028 KB Output is correct
4 Correct 132 ms 19528 KB Output is correct
5 Correct 179 ms 19420 KB Output is correct
6 Correct 214 ms 19236 KB Output is correct
7 Correct 185 ms 19216 KB Output is correct
8 Correct 146 ms 19368 KB Output is correct
9 Correct 123 ms 17376 KB Output is correct
10 Correct 161 ms 18784 KB Output is correct
11 Correct 135 ms 18604 KB Output is correct
12 Correct 188 ms 18896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 162 ms 19112 KB Output is correct
3 Correct 178 ms 19148 KB Output is correct
4 Correct 242 ms 19044 KB Output is correct
5 Correct 187 ms 19112 KB Output is correct
6 Correct 178 ms 19148 KB Output is correct
7 Correct 180 ms 19152 KB Output is correct
8 Correct 147 ms 19524 KB Output is correct
9 Correct 135 ms 19568 KB Output is correct
10 Correct 196 ms 19404 KB Output is correct
11 Correct 178 ms 19432 KB Output is correct
12 Correct 96 ms 18760 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 1 ms 324 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 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 320 KB Output is correct
24 Correct 1 ms 476 KB Output is correct
25 Correct 1 ms 476 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 472 KB Output is correct
29 Correct 1 ms 472 KB Output is correct
30 Correct 1 ms 472 KB Output is correct
31 Correct 35 ms 2508 KB Output is correct
32 Correct 38 ms 2508 KB Output is correct
33 Correct 47 ms 2724 KB Output is correct
34 Correct 28 ms 2764 KB Output is correct
35 Correct 27 ms 2684 KB Output is correct
36 Correct 24 ms 2624 KB Output is correct
37 Correct 30 ms 2260 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 460 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 360 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 124 ms 17380 KB Output is correct
48 Correct 118 ms 16720 KB Output is correct
49 Correct 143 ms 17360 KB Output is correct
50 Correct 119 ms 17492 KB Output is correct
51 Correct 95 ms 17416 KB Output is correct
52 Correct 117 ms 17396 KB Output is correct
53 Correct 70 ms 16768 KB Output is correct
54 Correct 172 ms 19124 KB Output is correct
55 Correct 167 ms 19096 KB Output is correct
56 Correct 236 ms 19028 KB Output is correct
57 Correct 132 ms 19528 KB Output is correct
58 Correct 179 ms 19420 KB Output is correct
59 Correct 214 ms 19236 KB Output is correct
60 Correct 185 ms 19216 KB Output is correct
61 Correct 146 ms 19368 KB Output is correct
62 Correct 123 ms 17376 KB Output is correct
63 Correct 161 ms 18784 KB Output is correct
64 Correct 135 ms 18604 KB Output is correct
65 Correct 188 ms 18896 KB Output is correct
66 Correct 1 ms 212 KB Output is correct
67 Correct 152 ms 19124 KB Output is correct
68 Correct 170 ms 19148 KB Output is correct
69 Correct 245 ms 19064 KB Output is correct
70 Correct 193 ms 19120 KB Output is correct
71 Correct 165 ms 19116 KB Output is correct
72 Correct 176 ms 19168 KB Output is correct
73 Correct 134 ms 19532 KB Output is correct
74 Correct 129 ms 19536 KB Output is correct
75 Correct 187 ms 19404 KB Output is correct
76 Correct 157 ms 19532 KB Output is correct
77 Correct 97 ms 18760 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 468 KB Output is correct
80 Correct 1 ms 364 KB Output is correct
81 Correct 1 ms 456 KB Output is correct
82 Correct 2 ms 468 KB Output is correct
83 Correct 1 ms 468 KB Output is correct
84 Correct 1 ms 464 KB Output is correct
85 Correct 1 ms 456 KB Output is correct
86 Correct 33 ms 2444 KB Output is correct
87 Correct 37 ms 2496 KB Output is correct
88 Correct 33 ms 2708 KB Output is correct
89 Correct 25 ms 2816 KB Output is correct
90 Correct 24 ms 2628 KB Output is correct
91 Correct 30 ms 2664 KB Output is correct
92 Correct 28 ms 2252 KB Output is correct
93 Correct 118 ms 17508 KB Output is correct
94 Correct 111 ms 16620 KB Output is correct
95 Correct 120 ms 17372 KB Output is correct
96 Correct 124 ms 17500 KB Output is correct
97 Correct 108 ms 17400 KB Output is correct
98 Correct 128 ms 17376 KB Output is correct
99 Correct 77 ms 16716 KB Output is correct
100 Correct 210 ms 19304 KB Output is correct
101 Correct 188 ms 19168 KB Output is correct
102 Correct 149 ms 19280 KB Output is correct
103 Correct 156 ms 18896 KB Output is correct
104 Correct 145 ms 18592 KB Output is correct
105 Correct 178 ms 18780 KB Output is correct
106 Correct 170 ms 18520 KB Output is correct
107 Correct 230 ms 18484 KB Output is correct
108 Correct 138 ms 19164 KB Output is correct
109 Correct 147 ms 19120 KB Output is correct
110 Correct 141 ms 19136 KB Output is correct
111 Correct 136 ms 19152 KB Output is correct