답안 #895801

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895801 2023-12-30T21:09:45 Z MilosMilutinovic Chessboard (IZhO18_chessboard) C++14
70 / 100
1207 ms 4180 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 * 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;
}

/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6

4 1
4 1 4 4
*/
# 결과 실행 시간 메모리 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 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 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 1372 KB Output is correct
2 Correct 6 ms 604 KB Output is correct
3 Correct 15 ms 1092 KB Output is correct
4 Correct 13 ms 1196 KB Output is correct
5 Correct 17 ms 1116 KB Output is correct
6 Correct 11 ms 1008 KB Output is correct
7 Correct 3 ms 572 KB Output is correct
8 Correct 11 ms 856 KB Output is correct
9 Correct 27 ms 1880 KB Output is correct
10 Correct 16 ms 1112 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 348 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 348 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 348 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 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 1 ms 348 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 348 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 348 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 860 KB Output is correct
17 Correct 22 ms 1876 KB Output is correct
18 Correct 36 ms 2136 KB Output is correct
19 Correct 137 ms 1832 KB Output is correct
20 Correct 153 ms 1992 KB Output is correct
21 Correct 26 ms 1628 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 23 ms 1136 KB Output is correct
24 Correct 31 ms 1628 KB Output is correct
25 Correct 5 ms 348 KB Output is correct
26 Correct 20 ms 1360 KB Output is correct
27 Correct 31 ms 1368 KB Output is correct
28 Correct 33 ms 1884 KB Output is correct
29 Correct 8 ms 860 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 1372 KB Output is correct
2 Correct 6 ms 604 KB Output is correct
3 Correct 15 ms 1092 KB Output is correct
4 Correct 13 ms 1196 KB Output is correct
5 Correct 17 ms 1116 KB Output is correct
6 Correct 11 ms 1008 KB Output is correct
7 Correct 3 ms 572 KB Output is correct
8 Correct 11 ms 856 KB Output is correct
9 Correct 27 ms 1880 KB Output is correct
10 Correct 16 ms 1112 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 1 ms 348 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 348 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 348 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 860 KB Output is correct
27 Correct 22 ms 1876 KB Output is correct
28 Correct 36 ms 2136 KB Output is correct
29 Correct 137 ms 1832 KB Output is correct
30 Correct 153 ms 1992 KB Output is correct
31 Correct 26 ms 1628 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 23 ms 1136 KB Output is correct
34 Correct 31 ms 1628 KB Output is correct
35 Correct 5 ms 348 KB Output is correct
36 Correct 20 ms 1360 KB Output is correct
37 Correct 31 ms 1368 KB Output is correct
38 Correct 33 ms 1884 KB Output is correct
39 Correct 8 ms 860 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 124 ms 1872 KB Output is correct
42 Correct 43 ms 1880 KB Output is correct
43 Correct 71 ms 1628 KB Output is correct
44 Correct 38 ms 1884 KB Output is correct
45 Correct 29 ms 2136 KB Output is correct
46 Correct 138 ms 1904 KB Output is correct
47 Correct 27 ms 1624 KB Output is correct
48 Correct 56 ms 1628 KB Output is correct
49 Correct 34 ms 1624 KB Output is correct
50 Correct 636 ms 1884 KB Output is correct
51 Correct 684 ms 1880 KB Output is correct
52 Correct 638 ms 1628 KB Output is correct
53 Correct 675 ms 1952 KB Output is correct
54 Correct 625 ms 1876 KB Output is correct
55 Correct 702 ms 2016 KB Output is correct
56 Correct 603 ms 1808 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 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 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 22 ms 1372 KB Output is correct
10 Correct 6 ms 604 KB Output is correct
11 Correct 15 ms 1092 KB Output is correct
12 Correct 13 ms 1196 KB Output is correct
13 Correct 17 ms 1116 KB Output is correct
14 Correct 11 ms 1008 KB Output is correct
15 Correct 3 ms 572 KB Output is correct
16 Correct 11 ms 856 KB Output is correct
17 Correct 27 ms 1880 KB Output is correct
18 Correct 16 ms 1112 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 1 ms 348 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 348 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 348 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 860 KB Output is correct
35 Correct 22 ms 1876 KB Output is correct
36 Correct 36 ms 2136 KB Output is correct
37 Correct 137 ms 1832 KB Output is correct
38 Correct 153 ms 1992 KB Output is correct
39 Correct 26 ms 1628 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 23 ms 1136 KB Output is correct
42 Correct 31 ms 1628 KB Output is correct
43 Correct 5 ms 348 KB Output is correct
44 Correct 20 ms 1360 KB Output is correct
45 Correct 31 ms 1368 KB Output is correct
46 Correct 33 ms 1884 KB Output is correct
47 Correct 8 ms 860 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 124 ms 1872 KB Output is correct
50 Correct 43 ms 1880 KB Output is correct
51 Correct 71 ms 1628 KB Output is correct
52 Correct 38 ms 1884 KB Output is correct
53 Correct 29 ms 2136 KB Output is correct
54 Correct 138 ms 1904 KB Output is correct
55 Correct 27 ms 1624 KB Output is correct
56 Correct 56 ms 1628 KB Output is correct
57 Correct 34 ms 1624 KB Output is correct
58 Correct 636 ms 1884 KB Output is correct
59 Correct 684 ms 1880 KB Output is correct
60 Correct 638 ms 1628 KB Output is correct
61 Correct 675 ms 1952 KB Output is correct
62 Correct 625 ms 1876 KB Output is correct
63 Correct 702 ms 2016 KB Output is correct
64 Correct 603 ms 1808 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 1204 ms 1916 KB Output is correct
68 Correct 1207 ms 4120 KB Output is correct
69 Correct 1025 ms 3676 KB Output is correct
70 Incorrect 1002 ms 4180 KB Output isn't correct
71 Halted 0 ms 0 KB -