답안 #895799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895799 2023-12-30T21:02:33 Z MilosMilutinovic Chessboard (IZhO18_chessboard) C++14
70 / 100
1217 ms 4400 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> x1(m), y1(m), x2(m), y2(m);
  for (int i = 0; i < m; i++) {
    cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    --x1[i]; --y1[i]; --x2[i]; --y2[i];
  }
  long long ans = 1e18;
  for (int foo = 0; foo < 2; foo++) {
    for (int k = 1; k < n; k++) {
      if (n % k != 0) {
        continue;
      }
      long long res = 0, area = 0;
      function<long long(int, int, int, int)> Solve = [&](int x1, int y1, int x2, int y2) {
        if (x1 > x2 || y1 > y2) {
          return 0LL;
        }
        if ((x1 / k) == (x2 / k) && (y1 / k) == (y2 / k)) {
          if (foo == (x1 / k + y1 / k) % 2) {
            return 0LL;
          } else {
            return (x2 - x1 + 1) * 1LL * (y2 - y1 + 1);
          }
        }
        if ((x1 / k) == (x2 / k)) {
          long long ret = 0;
          if (y1 % k != 0) {
            ret += Solve(x1, y1, x2, y1 + (k - y1 % k) - 1);
            y1 += (k - y1 % k);
          }
          if (y2 % k != k - 1) {
            ret += Solve(x1, y2 - y2 % k, x2, y2);
            y2 -= (y2 % k) + 1;
          }
          long long blocks = (y2 - y1 + 1) / k;
          if ((x1 / k + y1 / k) % 2 == foo) {
            blocks = blocks / 2;
          } else {
            blocks = (blocks + 1) / 2;
          }
          return ret + blocks * k * (x2 - x1 + 1);
        }
        if ((y1 / k) == (y2 / k)) {
          long long ret = 0;
          if (x1 % k != 0) {
            ret += Solve(x1, y1, x1 + (k - x1 % k) - 1, y2);
            x1 += (k - x1 % k);
          }
          if (x2 % k != k - 1) {
            ret += Solve(x2 - x2 % k, y1, x2, y2);
            x2  -= (x2 % k) + 1;
          }
          long long blocks = (x2 - x1 + 1) / k;
          if ((x1 / k + y1 / k) % 2 == foo) {
            blocks = blocks / 2;
          } else {
            blocks = (blocks + 1) / 2;
          }
          return ret + blocks * k * (y2 - y1 + 1);
        }
        long long ret = 0;
        if (x1 % k != 0) {
          ret += Solve(x1, y1, x1 + (k - x1 % k) - 1, y2);
          x1 += (k - x1 % k);
        }
        if (x2 % k != k - 1) {
          ret += Solve(x2 - x2 % k, y1, x2, y2);
          x2 -= x2 % k + 1;
        }
        if (y1 % k != 0) {
          ret += Solve(x1, y1, x2, y1 + (k - y1 % k) - 1);
          y1 += (y1 % k);
        }
        if (y2 % k != k - 1) {
          ret += Solve(x1, y2 - (y2 % k), x2, y2);
          y2 -= (y2 % k) + 1;
        }
        long long blocks_x = (x2 - x1 + 1) / k;
        long long blocks_y = (y2 - y1 + 1) / k;
        if ((x1 / k + y1 / k) % 2 == foo) {
          return ret + ((blocks_y + 1) / 2) * (blocks_x / 2) * k + (blocks_y / 2) * ((blocks_x + 1) / 2) * k * k;
        } else {
          return ret + ((blocks_y + 1) / 2) * ((blocks_x + 1) / 2) * k * k + (blocks_y / 2) * (blocks_x / 2) * k * k;
        }
      };
      for (int i = 0; i < m; i++) {
        res += Solve(x1[i], y1[i], x2[i], y2[i]);
        area += (x2[i] - x1[i] + 1) * 1LL * (y2[i] - y1[i] + 1);
      }
      long long black = 0;
      long long v = n / k;
      if (foo == 0) {
        black = ((v + 1) / 2) * ((v + 1) / 2) * k * k + (v / 2) * (v / 2) * k * k;
      } else {
        black = ((v + 1) / 2) * (v / 2) * k * k + (v / 2) * ((v + 1) / 2) * k * k;
      }
      ans = min(ans, res + black - (area - res));
    }
  } 
  cout << ans << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 592 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2728 KB Output is correct
2 Correct 6 ms 1060 KB Output is correct
3 Correct 13 ms 1884 KB Output is correct
4 Correct 14 ms 2052 KB Output is correct
5 Correct 18 ms 2396 KB Output is correct
6 Correct 11 ms 1628 KB Output is correct
7 Correct 3 ms 604 KB Output is correct
8 Correct 12 ms 1628 KB Output is correct
9 Correct 28 ms 3928 KB Output is correct
10 Correct 17 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 10 ms 1116 KB Output is correct
17 Correct 22 ms 3164 KB Output is correct
18 Correct 37 ms 3416 KB Output is correct
19 Correct 139 ms 3260 KB Output is correct
20 Correct 158 ms 3416 KB Output is correct
21 Correct 21 ms 2904 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 25 ms 1828 KB Output is correct
24 Correct 32 ms 3160 KB Output is correct
25 Correct 5 ms 720 KB Output is correct
26 Correct 20 ms 2236 KB Output is correct
27 Correct 31 ms 2396 KB Output is correct
28 Correct 34 ms 3416 KB Output is correct
29 Correct 9 ms 1372 KB Output is correct
30 Correct 1 ms 540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2728 KB Output is correct
2 Correct 6 ms 1060 KB Output is correct
3 Correct 13 ms 1884 KB Output is correct
4 Correct 14 ms 2052 KB Output is correct
5 Correct 18 ms 2396 KB Output is correct
6 Correct 11 ms 1628 KB Output is correct
7 Correct 3 ms 604 KB Output is correct
8 Correct 12 ms 1628 KB Output is correct
9 Correct 28 ms 3928 KB Output is correct
10 Correct 17 ms 2396 KB Output is correct
11 Correct 1 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 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 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 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 10 ms 1116 KB Output is correct
27 Correct 22 ms 3164 KB Output is correct
28 Correct 37 ms 3416 KB Output is correct
29 Correct 139 ms 3260 KB Output is correct
30 Correct 158 ms 3416 KB Output is correct
31 Correct 21 ms 2904 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 25 ms 1828 KB Output is correct
34 Correct 32 ms 3160 KB Output is correct
35 Correct 5 ms 720 KB Output is correct
36 Correct 20 ms 2236 KB Output is correct
37 Correct 31 ms 2396 KB Output is correct
38 Correct 34 ms 3416 KB Output is correct
39 Correct 9 ms 1372 KB Output is correct
40 Correct 1 ms 540 KB Output is correct
41 Correct 127 ms 3676 KB Output is correct
42 Correct 39 ms 4096 KB Output is correct
43 Correct 71 ms 3676 KB Output is correct
44 Correct 40 ms 3920 KB Output is correct
45 Correct 31 ms 4176 KB Output is correct
46 Correct 139 ms 3924 KB Output is correct
47 Correct 33 ms 3692 KB Output is correct
48 Correct 57 ms 3676 KB Output is correct
49 Correct 35 ms 3664 KB Output is correct
50 Correct 642 ms 4044 KB Output is correct
51 Correct 677 ms 4280 KB Output is correct
52 Correct 635 ms 4008 KB Output is correct
53 Correct 688 ms 4252 KB Output is correct
54 Correct 624 ms 3924 KB Output is correct
55 Correct 702 ms 4400 KB Output is correct
56 Correct 607 ms 3868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 592 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 24 ms 2728 KB Output is correct
10 Correct 6 ms 1060 KB Output is correct
11 Correct 13 ms 1884 KB Output is correct
12 Correct 14 ms 2052 KB Output is correct
13 Correct 18 ms 2396 KB Output is correct
14 Correct 11 ms 1628 KB Output is correct
15 Correct 3 ms 604 KB Output is correct
16 Correct 12 ms 1628 KB Output is correct
17 Correct 28 ms 3928 KB Output is correct
18 Correct 17 ms 2396 KB Output is correct
19 Correct 1 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 1 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 10 ms 1116 KB Output is correct
35 Correct 22 ms 3164 KB Output is correct
36 Correct 37 ms 3416 KB Output is correct
37 Correct 139 ms 3260 KB Output is correct
38 Correct 158 ms 3416 KB Output is correct
39 Correct 21 ms 2904 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 25 ms 1828 KB Output is correct
42 Correct 32 ms 3160 KB Output is correct
43 Correct 5 ms 720 KB Output is correct
44 Correct 20 ms 2236 KB Output is correct
45 Correct 31 ms 2396 KB Output is correct
46 Correct 34 ms 3416 KB Output is correct
47 Correct 9 ms 1372 KB Output is correct
48 Correct 1 ms 540 KB Output is correct
49 Correct 127 ms 3676 KB Output is correct
50 Correct 39 ms 4096 KB Output is correct
51 Correct 71 ms 3676 KB Output is correct
52 Correct 40 ms 3920 KB Output is correct
53 Correct 31 ms 4176 KB Output is correct
54 Correct 139 ms 3924 KB Output is correct
55 Correct 33 ms 3692 KB Output is correct
56 Correct 57 ms 3676 KB Output is correct
57 Correct 35 ms 3664 KB Output is correct
58 Correct 642 ms 4044 KB Output is correct
59 Correct 677 ms 4280 KB Output is correct
60 Correct 635 ms 4008 KB Output is correct
61 Correct 688 ms 4252 KB Output is correct
62 Correct 624 ms 3924 KB Output is correct
63 Correct 702 ms 4400 KB Output is correct
64 Correct 607 ms 3868 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Incorrect 1217 ms 4152 KB Output isn't correct
68 Halted 0 ms 0 KB -