Submission #751931

# Submission time Handle Problem Language Result Execution time Memory
751931 2023-06-01T20:30:04 Z tch1cherin Railway Trip 2 (JOI22_ho_t4) C++17
100 / 100
975 ms 64156 KB
#include <bits/stdc++.h>
using namespace std;
 
struct Event {
  int modl, modr, type;
 
  Event() {}
 
  Event(int _modl, int _modr, int _type) : modl(_modl), modr(_modr), type(_type) {}
};

struct SegmentTree {
  struct Node {
    int Min, Max;
  };

  Node merge(Node a, Node b) {
    return {min(a.Min, b.Min), max(a.Max, b.Max)};
  }

  int size;
  vector<Node> tree;

  SegmentTree() {}

  SegmentTree(vector<Node> a) {
    int n = (int)a.size();
    size = 2 << __lg(n);
    tree.assign(size * 2, {INT_MAX, INT_MIN});
    for (int i = 0; i < n; i++) {
      tree[size + i] = a[i];
    }
    for (int i = size - 1; i > 0; i--) {
      tree[i] = merge(tree[i * 2], tree[i * 2 + 1]);
    }
  }

  Node query(int l, int r, int x, int lx, int rx) {
    if (lx >= r || rx <= l) {
      return {INT_MAX, INT_MIN};
    } else if (lx >= l && rx <= r) {
      return tree[x];
    } else {
      int mid = (lx + rx) / 2;
      Node left = query(l, r, x * 2, lx, mid);
      Node right = query(l, r, x * 2 + 1, mid, rx);
      return merge(left, right);
    }
  }

  Node query(int l, int r) {
    return query(l, r + 1, 1, 0, size);
  } 
};

void solve() {
  int N, K;
  cin >> N >> K;
  int M;
  cin >> M;
  vector<int> A(M), B(M);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--, B[i]--;
  }
  vector<vector<Event>> events(N);
  for (int i = 0; i < M; i++) {
    if (A[i] < B[i]) {
      events[A[i]].emplace_back(N, B[i], 1);
      events[min(A[i] + K - 1, B[i])].emplace_back(N, B[i], -1);
    } else {
      events[max(A[i] - K + 1, B[i])].emplace_back(B[i], -1, 1);
      events[A[i]].emplace_back(B[i], -1, -1);
    }
  }
  vector<int> L(N), R(N);
  multiset<int> sl, sr;
  for (int i = 0; i < N; i++) {
    L[i] = i, R[i] = i;
    for (auto [modl, modr, type] : events[i]) {
      if (type == 1) {
        sl.insert(modl);
        sr.insert(modr);
      }
    }
    if (!sl.empty()) {
      L[i] = min(L[i], *sl.begin());
      R[i] = max(R[i], *sr.rbegin());
    }
    for (auto [modl, modr, type] : events[i]) {
      if (type == -1) {
        sl.erase(sl.find(modl));
        sr.erase(sr.find(modr));
      }
    }
  }
  vector<SegmentTree> tree(20);
  vector<SegmentTree::Node> layer(N);
  for (int i = 0; i < N; i++) {
    layer[i] = {L[i], R[i]};
  }
  tree[0] = SegmentTree(layer);
  for (int i = 0; i + 1 < 20; i++) {
    for (int j = 0; j < N; j++) {
      layer[j] = tree[i].query(layer[j].Min, layer[j].Max);  
    }
    tree[i + 1] = SegmentTree(layer);  
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int S, T;
    cin >> S >> T;
    S--, T--;
    SegmentTree::Node x = {S, S};
    int ans = 0;
    bool good = false;
    for (int i = 20 - 1; i >= 0; i--) {
      SegmentTree::Node y = x;
      x = tree[i].query(x.Min, x.Max);
      if (x.Min <= T && T <= x.Max) {
        x = y;
        good = true;
      } else {
        ans += 1 << i;
      }
    }
    if (!good) {
      cout << -1 << "\n";
    } else {
      cout << 1 + ans << "\n";
    }
  }
}
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 408 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 448 KB Output is correct
6 Correct 2 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 2 ms 468 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 448 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 408 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 448 KB Output is correct
6 Correct 2 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 2 ms 468 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 448 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 10 ms 1160 KB Output is correct
14 Correct 10 ms 1100 KB Output is correct
15 Correct 6 ms 1064 KB Output is correct
16 Correct 7 ms 1108 KB Output is correct
17 Correct 16 ms 1108 KB Output is correct
18 Correct 5 ms 1108 KB Output is correct
19 Correct 8 ms 1236 KB Output is correct
20 Correct 6 ms 1252 KB Output is correct
21 Correct 3 ms 1236 KB Output is correct
22 Correct 9 ms 1236 KB Output is correct
23 Correct 10 ms 1096 KB Output is correct
24 Correct 12 ms 1184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 371 ms 50184 KB Output is correct
2 Correct 314 ms 50156 KB Output is correct
3 Correct 475 ms 50988 KB Output is correct
4 Correct 324 ms 49840 KB Output is correct
5 Correct 407 ms 60380 KB Output is correct
6 Correct 398 ms 56576 KB Output is correct
7 Correct 323 ms 56812 KB Output is correct
8 Correct 273 ms 54720 KB Output is correct
9 Correct 266 ms 55292 KB Output is correct
10 Correct 444 ms 58128 KB Output is correct
11 Correct 347 ms 58136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 518 ms 52648 KB Output is correct
2 Correct 729 ms 61116 KB Output is correct
3 Correct 633 ms 51900 KB Output is correct
4 Correct 406 ms 57424 KB Output is correct
5 Correct 538 ms 64084 KB Output is correct
6 Correct 537 ms 63936 KB Output is correct
7 Correct 610 ms 58752 KB Output is correct
8 Correct 563 ms 58848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 812 ms 61560 KB Output is correct
2 Correct 814 ms 52000 KB Output is correct
3 Correct 686 ms 51036 KB Output is correct
4 Correct 643 ms 48520 KB Output is correct
5 Correct 457 ms 47244 KB Output is correct
6 Correct 534 ms 46836 KB Output is correct
7 Correct 556 ms 54704 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 9 ms 1260 KB Output is correct
10 Correct 623 ms 62796 KB Output is correct
11 Correct 814 ms 64156 KB Output is correct
12 Correct 734 ms 62556 KB Output is correct
13 Correct 975 ms 61952 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 9 ms 1096 KB Output is correct
16 Correct 406 ms 58176 KB Output is correct
17 Correct 686 ms 58892 KB Output is correct
18 Correct 681 ms 58820 KB Output is correct
19 Correct 663 ms 58816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 408 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 448 KB Output is correct
6 Correct 2 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 2 ms 468 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 448 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 10 ms 1160 KB Output is correct
14 Correct 10 ms 1100 KB Output is correct
15 Correct 6 ms 1064 KB Output is correct
16 Correct 7 ms 1108 KB Output is correct
17 Correct 16 ms 1108 KB Output is correct
18 Correct 5 ms 1108 KB Output is correct
19 Correct 8 ms 1236 KB Output is correct
20 Correct 6 ms 1252 KB Output is correct
21 Correct 3 ms 1236 KB Output is correct
22 Correct 9 ms 1236 KB Output is correct
23 Correct 10 ms 1096 KB Output is correct
24 Correct 12 ms 1184 KB Output is correct
25 Correct 371 ms 50184 KB Output is correct
26 Correct 314 ms 50156 KB Output is correct
27 Correct 475 ms 50988 KB Output is correct
28 Correct 324 ms 49840 KB Output is correct
29 Correct 407 ms 60380 KB Output is correct
30 Correct 398 ms 56576 KB Output is correct
31 Correct 323 ms 56812 KB Output is correct
32 Correct 273 ms 54720 KB Output is correct
33 Correct 266 ms 55292 KB Output is correct
34 Correct 444 ms 58128 KB Output is correct
35 Correct 347 ms 58136 KB Output is correct
36 Correct 518 ms 52648 KB Output is correct
37 Correct 729 ms 61116 KB Output is correct
38 Correct 633 ms 51900 KB Output is correct
39 Correct 406 ms 57424 KB Output is correct
40 Correct 538 ms 64084 KB Output is correct
41 Correct 537 ms 63936 KB Output is correct
42 Correct 610 ms 58752 KB Output is correct
43 Correct 563 ms 58848 KB Output is correct
44 Correct 812 ms 61560 KB Output is correct
45 Correct 814 ms 52000 KB Output is correct
46 Correct 686 ms 51036 KB Output is correct
47 Correct 643 ms 48520 KB Output is correct
48 Correct 457 ms 47244 KB Output is correct
49 Correct 534 ms 46836 KB Output is correct
50 Correct 556 ms 54704 KB Output is correct
51 Correct 2 ms 468 KB Output is correct
52 Correct 9 ms 1260 KB Output is correct
53 Correct 623 ms 62796 KB Output is correct
54 Correct 814 ms 64156 KB Output is correct
55 Correct 734 ms 62556 KB Output is correct
56 Correct 975 ms 61952 KB Output is correct
57 Correct 2 ms 468 KB Output is correct
58 Correct 9 ms 1096 KB Output is correct
59 Correct 406 ms 58176 KB Output is correct
60 Correct 686 ms 58892 KB Output is correct
61 Correct 681 ms 58820 KB Output is correct
62 Correct 663 ms 58816 KB Output is correct
63 Correct 654 ms 50580 KB Output is correct
64 Correct 761 ms 51536 KB Output is correct
65 Correct 656 ms 50704 KB Output is correct
66 Correct 581 ms 60940 KB Output is correct
67 Correct 572 ms 57316 KB Output is correct
68 Correct 560 ms 59624 KB Output is correct
69 Correct 505 ms 64012 KB Output is correct
70 Correct 550 ms 58788 KB Output is correct
71 Correct 674 ms 58972 KB Output is correct