답안 #1072095

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1072095 2024-08-23T14:16:03 Z duckindog Event Hopping (BOI22_events) C++17
100 / 100
106 ms 38592 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 200'000 + 10;
int n, q;
struct Event { 
  int st, ed, idx;
} a[N];
int rvs[N];

int lg[N];

int f[N][19];

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);

  cin >> n >> q;
  for (int i = 1; i <= n; ++i) cin >> a[i].st >> a[i].ed, a[i].idx = i;
  sort(a + 1, a + n + 1, [&](const auto& a, const auto& b) { return a.ed < b.ed; });
  for (int i = 1; i <= n; ++i) rvs[a[i].idx] = i;

  for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
  { //init
    vector<int> rrh;
    for (int i = 1; i <= n; ++i) { 
      rrh.push_back(a[i].st);
      rrh.push_back(a[i].ed);
    }
    sort(rrh.begin(), rrh.end());
    rrh.erase(unique(rrh.begin(), rrh.end()), rrh.end());

    a[0] = {1'000'000, 1'000'000};
    vector<vector<int>> save(rrh.size() + 1);
    for (int i = 1; i <= n; ++i) { 
      a[i].st = upper_bound(rrh.begin(), rrh.end(), a[i].st) - rrh.begin();
      a[i].ed = upper_bound(rrh.begin(), rrh.end(), a[i].ed) - rrh.begin();
      save[a[i].ed].push_back(i);
    }

    vector<array<int, 19>> st(rrh.size() + 1);
    using pii = pair<int, int>;
    priority_queue<pii, vector<pii>, greater<>> q;
    for (int i = 1; i <= rrh.size(); ++i) { 
      for (const auto& idx : save[i]) q.push({a[idx].st, idx});
      while (q.size() && a[q.top().second].ed < i) q.pop();
      st[i][0] = (!q.size() ? 0 : q.top().second);
    }

    for (int j = 1; j <= 18; ++j) { 
      for (int i = 1; i + (1 << j) - 1 <= rrh.size(); ++i) { 
        int lt = st[i][j - 1], rt = st[i + (1 << j - 1)][j - 1];
        st[i][j] = (a[lt].st < a[rt].st ? lt : rt);
      }
    }

    auto get = [&](int l, int r) { 
      int j = lg[r - l + 1];
      int lt = st[l][j], rt = st[r - (1 << j) + 1][j];
      return a[lt].st < a[rt].st ? lt : rt;
    };
    for (int i = 1; i <= n; ++i) f[i][0] = get(a[i].st, a[i].ed);
  }

  auto in = [&](int x, int y) { 
    return a[y].st <= a[x].ed && a[x].ed <= a[y].ed;
  };

  for (int j = 1; j <= 18; ++j) { 
    for (int i = n; i >= 1; --i) f[i][j] = f[f[i][j - 1]][j - 1];
  }

  while (q--) { 
    int x, y; cin >> x >> y;
    x = rvs[x]; y = rvs[y];

    if (in(x, y)) { cout << 1 - (x == y) << "\n"; continue; }
    if (a[x].st >= a[y].ed) { cout << "impossible\n"; continue; }

    int answer = 1;
    for (int i = 18; i >= 0; --i) { 
      if (!in(x, f[y][i]) && a[f[y][i]].ed >= a[x].ed) { 
        answer += (1 << i);
        y = f[y][i];
      }
    } y = f[y][0];

    if (!in(x, y)) { cout << "impossible\n"; continue; }
    cout << answer + (x != y) << "\n";
  }
}

Compilation message

events.cpp: In function 'int32_t main()':
events.cpp:45:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for (int i = 1; i <= rrh.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~
events.cpp:52:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |       for (int i = 1; i + (1 << j) - 1 <= rrh.size(); ++i) {
      |                       ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
events.cpp:53:52: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   53 |         int lt = st[i][j - 1], rt = st[i + (1 << j - 1)][j - 1];
      |                                                  ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 84 ms 27844 KB Output is correct
3 Correct 74 ms 27784 KB Output is correct
4 Correct 73 ms 27620 KB Output is correct
5 Correct 74 ms 29636 KB Output is correct
6 Correct 70 ms 30588 KB Output is correct
7 Correct 90 ms 30956 KB Output is correct
8 Correct 80 ms 38432 KB Output is correct
9 Correct 80 ms 38392 KB Output is correct
10 Correct 78 ms 28732 KB Output is correct
11 Correct 88 ms 32572 KB Output is correct
12 Correct 57 ms 26316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2420 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2664 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2420 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2664 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2524 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2648 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 27 ms 6276 KB Output is correct
20 Correct 25 ms 6560 KB Output is correct
21 Correct 39 ms 6624 KB Output is correct
22 Correct 21 ms 6656 KB Output is correct
23 Correct 20 ms 6536 KB Output is correct
24 Correct 22 ms 6536 KB Output is correct
25 Correct 22 ms 6024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2420 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2664 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2508 KB Output is correct
11 Correct 1 ms 2508 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 1 ms 2660 KB Output is correct
17 Correct 1 ms 2856 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 51 ms 28340 KB Output is correct
20 Correct 46 ms 27612 KB Output is correct
21 Correct 56 ms 26756 KB Output is correct
22 Correct 53 ms 31948 KB Output is correct
23 Correct 57 ms 38344 KB Output is correct
24 Correct 59 ms 38148 KB Output is correct
25 Correct 33 ms 17360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 28624 KB Output is correct
2 Correct 69 ms 28620 KB Output is correct
3 Correct 86 ms 28624 KB Output is correct
4 Correct 74 ms 38568 KB Output is correct
5 Correct 77 ms 28732 KB Output is correct
6 Correct 86 ms 38352 KB Output is correct
7 Correct 96 ms 38352 KB Output is correct
8 Correct 97 ms 38456 KB Output is correct
9 Correct 65 ms 38148 KB Output is correct
10 Correct 77 ms 38348 KB Output is correct
11 Correct 79 ms 38460 KB Output is correct
12 Correct 77 ms 38348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 84 ms 27844 KB Output is correct
3 Correct 74 ms 27784 KB Output is correct
4 Correct 73 ms 27620 KB Output is correct
5 Correct 74 ms 29636 KB Output is correct
6 Correct 70 ms 30588 KB Output is correct
7 Correct 90 ms 30956 KB Output is correct
8 Correct 80 ms 38432 KB Output is correct
9 Correct 80 ms 38392 KB Output is correct
10 Correct 78 ms 28732 KB Output is correct
11 Correct 88 ms 32572 KB Output is correct
12 Correct 57 ms 26316 KB Output is correct
13 Correct 1 ms 2420 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2664 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2524 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 1 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 27 ms 6276 KB Output is correct
32 Correct 25 ms 6560 KB Output is correct
33 Correct 39 ms 6624 KB Output is correct
34 Correct 21 ms 6656 KB Output is correct
35 Correct 20 ms 6536 KB Output is correct
36 Correct 22 ms 6536 KB Output is correct
37 Correct 22 ms 6024 KB Output is correct
38 Correct 1 ms 2508 KB Output is correct
39 Correct 1 ms 2508 KB Output is correct
40 Correct 2 ms 2652 KB Output is correct
41 Correct 2 ms 2648 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 2 ms 2652 KB Output is correct
44 Correct 1 ms 2660 KB Output is correct
45 Correct 1 ms 2856 KB Output is correct
46 Correct 1 ms 2392 KB Output is correct
47 Correct 51 ms 28340 KB Output is correct
48 Correct 46 ms 27612 KB Output is correct
49 Correct 56 ms 26756 KB Output is correct
50 Correct 53 ms 31948 KB Output is correct
51 Correct 57 ms 38344 KB Output is correct
52 Correct 59 ms 38148 KB Output is correct
53 Correct 33 ms 17360 KB Output is correct
54 Correct 90 ms 28624 KB Output is correct
55 Correct 69 ms 28620 KB Output is correct
56 Correct 86 ms 28624 KB Output is correct
57 Correct 74 ms 38568 KB Output is correct
58 Correct 77 ms 28732 KB Output is correct
59 Correct 86 ms 38352 KB Output is correct
60 Correct 96 ms 38352 KB Output is correct
61 Correct 97 ms 38456 KB Output is correct
62 Correct 65 ms 38148 KB Output is correct
63 Correct 77 ms 38348 KB Output is correct
64 Correct 79 ms 38460 KB Output is correct
65 Correct 77 ms 38348 KB Output is correct
66 Correct 1 ms 2396 KB Output is correct
67 Correct 77 ms 28632 KB Output is correct
68 Correct 79 ms 28612 KB Output is correct
69 Correct 79 ms 28624 KB Output is correct
70 Correct 77 ms 29596 KB Output is correct
71 Correct 81 ms 30516 KB Output is correct
72 Correct 76 ms 30776 KB Output is correct
73 Correct 85 ms 38352 KB Output is correct
74 Correct 86 ms 38352 KB Output is correct
75 Correct 106 ms 28644 KB Output is correct
76 Correct 86 ms 32460 KB Output is correct
77 Correct 57 ms 26328 KB Output is correct
78 Correct 1 ms 2396 KB Output is correct
79 Correct 1 ms 2652 KB Output is correct
80 Correct 2 ms 2652 KB Output is correct
81 Correct 1 ms 2652 KB Output is correct
82 Correct 1 ms 2652 KB Output is correct
83 Correct 1 ms 2652 KB Output is correct
84 Correct 1 ms 2652 KB Output is correct
85 Correct 1 ms 2396 KB Output is correct
86 Correct 25 ms 6208 KB Output is correct
87 Correct 29 ms 6368 KB Output is correct
88 Correct 30 ms 6552 KB Output is correct
89 Correct 21 ms 6628 KB Output is correct
90 Correct 29 ms 6436 KB Output is correct
91 Correct 21 ms 6616 KB Output is correct
92 Correct 21 ms 6280 KB Output is correct
93 Correct 47 ms 28368 KB Output is correct
94 Correct 43 ms 27620 KB Output is correct
95 Correct 59 ms 26860 KB Output is correct
96 Correct 52 ms 31952 KB Output is correct
97 Correct 53 ms 38348 KB Output is correct
98 Correct 55 ms 38140 KB Output is correct
99 Correct 23 ms 17360 KB Output is correct
100 Correct 82 ms 38592 KB Output is correct
101 Correct 88 ms 38352 KB Output is correct
102 Correct 100 ms 38352 KB Output is correct
103 Correct 73 ms 38440 KB Output is correct
104 Correct 71 ms 38352 KB Output is correct
105 Correct 73 ms 38352 KB Output is correct
106 Correct 75 ms 25300 KB Output is correct
107 Correct 79 ms 27856 KB Output is correct
108 Correct 75 ms 27088 KB Output is correct
109 Correct 73 ms 27088 KB Output is correct
110 Correct 93 ms 38468 KB Output is correct
111 Correct 100 ms 38492 KB Output is correct