Submission #966672

# Submission time Handle Problem Language Result Execution time Memory
966672 2024-04-20T08:02:50 Z kilkuwu Strange Device (APIO19_strange_device) C++17
100 / 100
469 ms 95604 KB
#include <bits/stdc++.h>

#define nl '\n'

#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n;
  int64_t A, B;
  std::cin >> n >> A >> B;
  std::vector<int64_t> L(n), R(n);
  for (int i = 0; i < n; i++) {
    std::cin >> L[i] >> R[i];
  }
  auto decompose = [&](int64_t t) -> std::pair<int64_t, int64_t> {
    return {t % B, t / B};
  };

  int step = (B + 1) % A;
  if (step == 0) {
    std::vector<std::pair<int64_t, int64_t>> segs;
    for (int i = 0; i < n; i++) {
      if (R[i] - L[i] + 1 < B) {
        auto r0 = L[i] % B;
        auto r1 = R[i] % B;
        if (r0 <= r1) {
          segs.emplace_back(r0, r1);
        } else {
          segs.emplace_back(0, r1);
          segs.emplace_back(r0, B - 1);
        }
      } else {
        segs.emplace_back(0, B - 1);
      }
    }

    std::sort(segs.begin(), segs.end());

    std::vector<std::pair<int64_t, int64_t>> normalized;
    for (auto [l, r] : segs) {
      if (normalized.empty() || l > normalized.back().second) {
        normalized.emplace_back(l, r);
      } else {
        normalized.back().second = std::max(normalized.back().second, r);
      }
    }

    int64_t ans = 0;
    for (auto [l, r] : normalized) {
      ans += r - l + 1;
    }
    std::cout << ans << nl;
    return 0;
  } 

  auto g = std::gcd(A, step);
  auto thres = A / g;

  std::vector<std::pair<int64_t, int64_t>> segs;

  auto process = [&](int64_t l, int64_t r) {
    auto len = r - l + 1;
    if (len / B >= thres) {
      segs.emplace_back(0, thres * B - 1);
    }
    auto [rl, ql] = decompose(l);
    auto [rr, qr] = decompose(r);

    if (ql >= thres) {
      auto num_shift = ql / thres;
      ql -= num_shift * thres;
      qr -= num_shift * thres;
    }

    if (qr >= thres) {
      segs.emplace_back(rl + ql * B, B - 1 + (thres - 1) * B);
      auto num_shift = qr / thres;
      qr -= num_shift * thres;
      segs.emplace_back(0, rr + qr * B);
    } else {
      segs.emplace_back(rl + ql * B, rr + qr * B);
    }
  };

  for (int i = 0; i < n; i++) {
    process(L[i], R[i]);
  }
  std::sort(segs.begin(), segs.end());

  std::vector<std::pair<int64_t, int64_t>> normalized;
  for (auto [l, r] : segs) {
    if (normalized.empty() || l > normalized.back().second) {
      normalized.emplace_back(l, r);
    } else {
      normalized.back().second = std::max(normalized.back().second, r);
    }
  }

  int64_t ans = 0;
  for (auto [l, r] : normalized) {
    ans += r - l + 1;
  }
  std::cout << ans << nl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 4 ms 1424 KB Output is correct
3 Correct 4 ms 1360 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 1492 KB Output is correct
17 Correct 38 ms 10204 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 228 ms 59868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 364 ms 95544 KB Output is correct
3 Correct 342 ms 94644 KB Output is correct
4 Correct 343 ms 95228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 364 ms 95544 KB Output is correct
3 Correct 342 ms 94644 KB Output is correct
4 Correct 343 ms 95228 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 372 ms 93680 KB Output is correct
7 Correct 348 ms 94408 KB Output is correct
8 Correct 346 ms 93924 KB Output is correct
9 Correct 407 ms 94852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 364 ms 95544 KB Output is correct
3 Correct 342 ms 94644 KB Output is correct
4 Correct 343 ms 95228 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 35 ms 10336 KB Output is correct
7 Correct 38 ms 10172 KB Output is correct
8 Correct 40 ms 10328 KB Output is correct
9 Correct 41 ms 10332 KB Output is correct
10 Correct 34 ms 10184 KB Output is correct
11 Correct 43 ms 10444 KB Output is correct
12 Correct 35 ms 10184 KB Output is correct
13 Correct 42 ms 10188 KB Output is correct
14 Correct 33 ms 10188 KB Output is correct
15 Correct 40 ms 10284 KB Output is correct
16 Correct 41 ms 10460 KB Output is correct
17 Correct 35 ms 10188 KB Output is correct
18 Correct 390 ms 94552 KB Output is correct
19 Correct 340 ms 95068 KB Output is correct
20 Correct 391 ms 94008 KB Output is correct
21 Correct 44 ms 10184 KB Output is correct
22 Correct 33 ms 10184 KB Output is correct
23 Correct 103 ms 27404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 38 ms 10192 KB Output is correct
3 Correct 39 ms 10244 KB Output is correct
4 Correct 404 ms 93172 KB Output is correct
5 Correct 38 ms 10184 KB Output is correct
6 Correct 52 ms 10244 KB Output is correct
7 Correct 36 ms 10216 KB Output is correct
8 Correct 38 ms 10264 KB Output is correct
9 Correct 36 ms 10204 KB Output is correct
10 Correct 38 ms 10256 KB Output is correct
11 Correct 46 ms 10248 KB Output is correct
12 Correct 35 ms 10308 KB Output is correct
13 Correct 38 ms 10264 KB Output is correct
14 Correct 398 ms 94012 KB Output is correct
15 Correct 46 ms 10344 KB Output is correct
16 Correct 359 ms 95604 KB Output is correct
17 Correct 346 ms 93824 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 4 ms 1424 KB Output is correct
3 Correct 4 ms 1360 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 1492 KB Output is correct
17 Correct 38 ms 10204 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 228 ms 59868 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 364 ms 95544 KB Output is correct
31 Correct 342 ms 94644 KB Output is correct
32 Correct 343 ms 95228 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 372 ms 93680 KB Output is correct
35 Correct 348 ms 94408 KB Output is correct
36 Correct 346 ms 93924 KB Output is correct
37 Correct 407 ms 94852 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 35 ms 10336 KB Output is correct
40 Correct 38 ms 10172 KB Output is correct
41 Correct 40 ms 10328 KB Output is correct
42 Correct 41 ms 10332 KB Output is correct
43 Correct 34 ms 10184 KB Output is correct
44 Correct 43 ms 10444 KB Output is correct
45 Correct 35 ms 10184 KB Output is correct
46 Correct 42 ms 10188 KB Output is correct
47 Correct 33 ms 10188 KB Output is correct
48 Correct 40 ms 10284 KB Output is correct
49 Correct 41 ms 10460 KB Output is correct
50 Correct 35 ms 10188 KB Output is correct
51 Correct 390 ms 94552 KB Output is correct
52 Correct 340 ms 95068 KB Output is correct
53 Correct 391 ms 94008 KB Output is correct
54 Correct 44 ms 10184 KB Output is correct
55 Correct 33 ms 10184 KB Output is correct
56 Correct 103 ms 27404 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 38 ms 10192 KB Output is correct
59 Correct 39 ms 10244 KB Output is correct
60 Correct 404 ms 93172 KB Output is correct
61 Correct 38 ms 10184 KB Output is correct
62 Correct 52 ms 10244 KB Output is correct
63 Correct 36 ms 10216 KB Output is correct
64 Correct 38 ms 10264 KB Output is correct
65 Correct 36 ms 10204 KB Output is correct
66 Correct 38 ms 10256 KB Output is correct
67 Correct 46 ms 10248 KB Output is correct
68 Correct 35 ms 10308 KB Output is correct
69 Correct 38 ms 10264 KB Output is correct
70 Correct 398 ms 94012 KB Output is correct
71 Correct 46 ms 10344 KB Output is correct
72 Correct 359 ms 95604 KB Output is correct
73 Correct 346 ms 93824 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 1 ms 344 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 4 ms 1356 KB Output is correct
80 Correct 469 ms 94076 KB Output is correct
81 Correct 413 ms 94456 KB Output is correct
82 Correct 375 ms 94100 KB Output is correct
83 Correct 392 ms 94792 KB Output is correct
84 Correct 434 ms 93512 KB Output is correct
85 Correct 418 ms 93180 KB Output is correct
86 Correct 121 ms 27516 KB Output is correct
87 Correct 1 ms 344 KB Output is correct