답안 #395969

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395969 2021-04-29T09:39:33 Z rama_pang Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1271 ms 109368 KB
#include <bits/stdc++.h>
using namespace std;

class SegTree {
 public:
  int sz;
  vector<int> tree;

  SegTree(int sz) : sz(sz), tree(2 * sz, 0) {}

  void Modify(int p, int x) {
    for (p += sz; p > 0; p /= 2) tree[p] = max(tree[p], x);
  }

  int Query(int l, int r) {
    int res = 0;
    for (l += sz, r += sz + 1; l < r; l /= 2, r /= 2) {
      if (l & 1) res = max(res, tree[l++]);
      if (r & 1) res = max(res, tree[--r]);
    }
    return res;
  }
};

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

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

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<int> ans(Q);
  vector<vector<array<int, 3>>> query(N);
  for (int i = 0; i < Q; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    l--, r--;
    query[l].push_back({r, k, i});
  }

  // Solution:
  //
  // For a range [L, R], we want to pick
  // i, j, s.t. L <= i < k <= R, A[i] > A[j],
  // where A[i] + A[j] is maximal. Then, if
  // A[i] + A[j] > k, the books cannot be sorted;
  // otherwise, it can.
  //
  // Note that for i < j, A[i] > A[j], since we can
  // only swap adjacent books, at some point in sorting
  // A[i] and A[j] must be adjacent, and we must swap them.
  //
  // Let's sweep from right to left. Note that for i < j,
  // A[i] > A[j]:
  // - If there exist i < k < j, s.t. A[j] < A[i] < A[k], the pair
  //   (k, j) is strictly better.
  // - If there exist i < k < j, s.t. A[i] > A[k] > A[j], the pair
  //   (i, k) is strictly better.
  //
  // Note that this means, for all (i, j), it is optimal if all indices
  // i < k < j, either A[k] < min(A[i], A[j]) must hold.
  //
  // This means, we only need to care about the nearest element to the left
  // of j which is greater than A[j]. This is only O(N), and can be computed
  // easily.
  //
  // We can answer everything offline in O((N + Q) log N).

  vector<vector<int>> candidates(N);
  { // Compute candidates, (i, j) is good if i is nearest element greater than A[j] to the right
    vector<int> st;
    for (int i = 0; i < N; i++) {
      while (!st.empty() && A[st.back()] <= A[i]) {
        st.pop_back();
      }
      if (!st.empty()) {
        candidates[st.back()].emplace_back(i);
      }
      st.emplace_back(i);
    }
  }

  SegTree seg(N);
  for (int l = N - 1; l >= 0; l--) {
    for (auto r : candidates[l]) {
      seg.Modify(r, A[l] + A[r]);
    }
    for (auto [r, k, q] : query[l]) {
      ans[q] = seg.Query(l, r) <= k;
    }
  }

  for (int i = 0; i < Q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 3 ms 844 KB Output is correct
13 Correct 4 ms 764 KB Output is correct
14 Correct 5 ms 972 KB Output is correct
15 Correct 5 ms 972 KB Output is correct
16 Correct 4 ms 844 KB Output is correct
17 Correct 4 ms 716 KB Output is correct
18 Correct 4 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1219 ms 108400 KB Output is correct
2 Correct 1180 ms 108372 KB Output is correct
3 Correct 1227 ms 108352 KB Output is correct
4 Correct 1212 ms 108288 KB Output is correct
5 Correct 1106 ms 92124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 12540 KB Output is correct
2 Correct 85 ms 12084 KB Output is correct
3 Correct 77 ms 10764 KB Output is correct
4 Correct 87 ms 10948 KB Output is correct
5 Correct 78 ms 10932 KB Output is correct
6 Correct 62 ms 10436 KB Output is correct
7 Correct 59 ms 10340 KB Output is correct
8 Correct 71 ms 10676 KB Output is correct
9 Correct 41 ms 3772 KB Output is correct
10 Correct 86 ms 10604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 3 ms 844 KB Output is correct
13 Correct 4 ms 764 KB Output is correct
14 Correct 5 ms 972 KB Output is correct
15 Correct 5 ms 972 KB Output is correct
16 Correct 4 ms 844 KB Output is correct
17 Correct 4 ms 716 KB Output is correct
18 Correct 4 ms 844 KB Output is correct
19 Correct 231 ms 27508 KB Output is correct
20 Correct 225 ms 27568 KB Output is correct
21 Correct 209 ms 26464 KB Output is correct
22 Correct 190 ms 26456 KB Output is correct
23 Correct 243 ms 26432 KB Output is correct
24 Correct 202 ms 23516 KB Output is correct
25 Correct 153 ms 23476 KB Output is correct
26 Correct 199 ms 23748 KB Output is correct
27 Correct 181 ms 23740 KB Output is correct
28 Correct 188 ms 23828 KB Output is correct
29 Correct 190 ms 24260 KB Output is correct
30 Correct 192 ms 24260 KB Output is correct
31 Correct 199 ms 24260 KB Output is correct
32 Correct 186 ms 24260 KB Output is correct
33 Correct 190 ms 24260 KB Output is correct
34 Correct 143 ms 22788 KB Output is correct
35 Correct 139 ms 22724 KB Output is correct
36 Correct 147 ms 22596 KB Output is correct
37 Correct 136 ms 22556 KB Output is correct
38 Correct 142 ms 22980 KB Output is correct
39 Correct 187 ms 23160 KB Output is correct
40 Correct 129 ms 17272 KB Output is correct
41 Correct 158 ms 22356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 3 ms 844 KB Output is correct
13 Correct 4 ms 764 KB Output is correct
14 Correct 5 ms 972 KB Output is correct
15 Correct 5 ms 972 KB Output is correct
16 Correct 4 ms 844 KB Output is correct
17 Correct 4 ms 716 KB Output is correct
18 Correct 4 ms 844 KB Output is correct
19 Correct 1219 ms 108400 KB Output is correct
20 Correct 1180 ms 108372 KB Output is correct
21 Correct 1227 ms 108352 KB Output is correct
22 Correct 1212 ms 108288 KB Output is correct
23 Correct 1106 ms 92124 KB Output is correct
24 Correct 89 ms 12540 KB Output is correct
25 Correct 85 ms 12084 KB Output is correct
26 Correct 77 ms 10764 KB Output is correct
27 Correct 87 ms 10948 KB Output is correct
28 Correct 78 ms 10932 KB Output is correct
29 Correct 62 ms 10436 KB Output is correct
30 Correct 59 ms 10340 KB Output is correct
31 Correct 71 ms 10676 KB Output is correct
32 Correct 41 ms 3772 KB Output is correct
33 Correct 86 ms 10604 KB Output is correct
34 Correct 231 ms 27508 KB Output is correct
35 Correct 225 ms 27568 KB Output is correct
36 Correct 209 ms 26464 KB Output is correct
37 Correct 190 ms 26456 KB Output is correct
38 Correct 243 ms 26432 KB Output is correct
39 Correct 202 ms 23516 KB Output is correct
40 Correct 153 ms 23476 KB Output is correct
41 Correct 199 ms 23748 KB Output is correct
42 Correct 181 ms 23740 KB Output is correct
43 Correct 188 ms 23828 KB Output is correct
44 Correct 190 ms 24260 KB Output is correct
45 Correct 192 ms 24260 KB Output is correct
46 Correct 199 ms 24260 KB Output is correct
47 Correct 186 ms 24260 KB Output is correct
48 Correct 190 ms 24260 KB Output is correct
49 Correct 143 ms 22788 KB Output is correct
50 Correct 139 ms 22724 KB Output is correct
51 Correct 147 ms 22596 KB Output is correct
52 Correct 136 ms 22556 KB Output is correct
53 Correct 142 ms 22980 KB Output is correct
54 Correct 187 ms 23160 KB Output is correct
55 Correct 129 ms 17272 KB Output is correct
56 Correct 158 ms 22356 KB Output is correct
57 Correct 1246 ms 109368 KB Output is correct
58 Correct 1271 ms 109272 KB Output is correct
59 Correct 1262 ms 106728 KB Output is correct
60 Correct 1212 ms 106736 KB Output is correct
61 Correct 1200 ms 106616 KB Output is correct
62 Correct 1200 ms 106676 KB Output is correct
63 Correct 751 ms 86892 KB Output is correct
64 Correct 739 ms 86716 KB Output is correct
65 Correct 1069 ms 90776 KB Output is correct
66 Correct 1096 ms 90844 KB Output is correct
67 Correct 1163 ms 90644 KB Output is correct
68 Correct 1185 ms 92436 KB Output is correct
69 Correct 1133 ms 92264 KB Output is correct
70 Correct 1124 ms 91444 KB Output is correct
71 Correct 1115 ms 91252 KB Output is correct
72 Correct 1149 ms 91144 KB Output is correct
73 Correct 656 ms 84736 KB Output is correct
74 Correct 676 ms 84376 KB Output is correct
75 Correct 665 ms 84476 KB Output is correct
76 Correct 650 ms 84112 KB Output is correct
77 Correct 662 ms 84028 KB Output is correct
78 Correct 966 ms 91752 KB Output is correct
79 Correct 701 ms 57504 KB Output is correct
80 Correct 933 ms 89712 KB Output is correct