답안 #395271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395271 2021-04-28T04:47:20 Z rama_pang Chessboard (IZhO18_chessboard) C++17
100 / 100
542 ms 4372 KB
#include <bits/stdc++.h>
using namespace std;

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

  int n, k;
  cin >> n >> k;
  vector<array<int, 4>> rect;
  for (int i = 0; i < k; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--, b--, c--, d--;
    rect.push_back({a, b, c, d});
  }

  auto Solve = [&](int s) -> long long {
    long long res = 1ll * (n / s) * (n / s) / 2 * s * s;
    if (n / s % 2 == 1) {
      res += 1ll * s * s;
    }

    auto CountCorner = [&](int sx, int sy, int ex, int ey) -> long long {
      if (sx > ex || sy > ey) return 0;
      assert(sx / s == ex / s && sy / s == ey / s);
      if ((sx / s + sy / s) & 1) {
        return +1ll * (ex - sx + 1) * (ey - sy + 1);
      } else {
        return -1ll * (ex - sx + 1) * (ey - sy + 1);
      }
      return 0;
    };

    auto CountVertical = [&](int sx, int sy, int ex, int ey) -> long long {
      if (sx > ex || sy > ey) return 0;
      assert(sy / s == ey / s);
      assert((ex - sx + 1) % s == 0);
      int cnt = (ex - sx + 1) / s;
      if (cnt & 1) {
        if ((sx / s + sy / s) % 2 == 1) {
          return +1ll * (ey - sy + 1) * s;
        } else {
          return -1ll * (ey - sy + 1) * s;
        }
      }
      return 0;
    };

    auto CountHorizontal = [&](int sx, int sy, int ex, int ey) -> long long {
      if (sx > ex || sy > ey) return 0;
      assert(sx / s == ex / s);
      assert((ey - sy + 1) % s == 0);
      int cnt = (ey - sy + 1) / s;
      if (cnt & 1) {
        if ((sx / s + sy / s) % 2 == 1) {
          return +1ll * (ex - sx + 1) * s;
        } else {
          return -1ll * (ex - sx + 1) * s;
        }
      }
      return 0;
    };

    auto CountInside = [&](int sx, int sy, int ex, int ey) -> long long {
      if (sx > ex || sy > ey) return 0;
      int x = (ex - sx + 1) / s;
      int y = (ey - sy + 1) / s;
      if (x % 2 == 1 && y % 2 == 1) {
        if ((sx / s + sy / s) % 2 == 1) {
          return +1ll * s * s;
        } else {
          return -1ll * s * s;
        }
      }
      return 0;
    };

    for (int i = 0; i < k; i++) {
      if (rect[i][0] / s == rect[i][2] / s && rect[i][1] / s == rect[i][3] / s) {
        res += CountCorner(rect[i][0], rect[i][1], rect[i][2], rect[i][3]);
      } else if (rect[i][0] / s == rect[i][2] / s) {
        int sy = (rect[i][1] + s - 1) / s;
        int ey = (rect[i][3] + 1) / s;

        res += CountCorner(rect[i][0], rect[i][1], rect[i][2], s * sy - 1);
        res += CountCorner(rect[i][0], s * ey + 0, rect[i][2], rect[i][3]);

        res += CountHorizontal(rect[i][0], s * sy + 0, rect[i][2], s * ey - 1);
      } else if (rect[i][1] / s == rect[i][3] / s) {
        int sx = (rect[i][0] + s - 1) / s;
        int ex = (rect[i][2] + 1) / s;

        res += CountCorner(rect[i][0], rect[i][1], s * sx - 1, rect[i][3]);
        res += CountCorner(s * ex + 0, rect[i][1], rect[i][2], rect[i][3]);

        res += CountVertical(s * sx + 0, rect[i][1], s * ex - 1, rect[i][3]);
      } else {
        int sx = (rect[i][0] + s - 1) / s;
        int sy = (rect[i][1] + s - 1) / s;
        int ex = (rect[i][2] + 1) / s;
        int ey = (rect[i][3] + 1) / s;

        res += CountCorner(rect[i][0], rect[i][1], s * sx - 1, s * sy - 1);
        res += CountCorner(rect[i][0], s * ey + 0, s * sx - 1, rect[i][3]);
        res += CountCorner(s * ex + 0, rect[i][1], rect[i][2], s * sy - 1);
        res += CountCorner(s * ex + 0, s * ey + 0, rect[i][2], rect[i][3]);

        res += CountHorizontal(rect[i][0], s * sy + 0, s * sx - 1, s * ey - 1);
        res += CountHorizontal(s * ex + 0, s * sy + 0, rect[i][2], s * ey - 1);

        res += CountVertical(s * sx + 0, rect[i][1], s * ex - 1, s * sy - 1);
        res += CountVertical(s * sx + 0, s * ey + 0, s * ex - 1, rect[i][3]);

        res += CountInside(s * sx + 0, s * sy + 0, s * ex - 1, s * ey - 1);
      }
    }
    return min(res, 1ll * n * n - res);
  };

  long long ans = 1ll * n * n;
  for (int i = 1; i < n; i++) {
    if (n % i == 0) {
      ans = min(ans, Solve(i));
    }
  }
  cout << ans << "\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 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 2860 KB Output is correct
2 Correct 8 ms 1232 KB Output is correct
3 Correct 18 ms 2376 KB Output is correct
4 Correct 19 ms 2252 KB Output is correct
5 Correct 29 ms 2536 KB Output is correct
6 Correct 16 ms 2252 KB Output is correct
7 Correct 4 ms 720 KB Output is correct
8 Correct 16 ms 2252 KB Output is correct
9 Correct 39 ms 4072 KB Output is correct
10 Correct 22 ms 2468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 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 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 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 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 12 ms 1360 KB Output is correct
17 Correct 31 ms 3736 KB Output is correct
18 Correct 41 ms 3648 KB Output is correct
19 Correct 110 ms 3644 KB Output is correct
20 Correct 119 ms 3656 KB Output is correct
21 Correct 30 ms 3768 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 24 ms 2124 KB Output is correct
24 Correct 41 ms 3776 KB Output is correct
25 Correct 5 ms 844 KB Output is correct
26 Correct 28 ms 2252 KB Output is correct
27 Correct 38 ms 3528 KB Output is correct
28 Correct 43 ms 3648 KB Output is correct
29 Correct 13 ms 2000 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 2860 KB Output is correct
2 Correct 8 ms 1232 KB Output is correct
3 Correct 18 ms 2376 KB Output is correct
4 Correct 19 ms 2252 KB Output is correct
5 Correct 29 ms 2536 KB Output is correct
6 Correct 16 ms 2252 KB Output is correct
7 Correct 4 ms 720 KB Output is correct
8 Correct 16 ms 2252 KB Output is correct
9 Correct 39 ms 4072 KB Output is correct
10 Correct 22 ms 2468 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 12 ms 1360 KB Output is correct
27 Correct 31 ms 3736 KB Output is correct
28 Correct 41 ms 3648 KB Output is correct
29 Correct 110 ms 3644 KB Output is correct
30 Correct 119 ms 3656 KB Output is correct
31 Correct 30 ms 3768 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 24 ms 2124 KB Output is correct
34 Correct 41 ms 3776 KB Output is correct
35 Correct 5 ms 844 KB Output is correct
36 Correct 28 ms 2252 KB Output is correct
37 Correct 38 ms 3528 KB Output is correct
38 Correct 43 ms 3648 KB Output is correct
39 Correct 13 ms 2000 KB Output is correct
40 Correct 2 ms 460 KB Output is correct
41 Correct 102 ms 4248 KB Output is correct
42 Correct 47 ms 4268 KB Output is correct
43 Correct 62 ms 4172 KB Output is correct
44 Correct 45 ms 4220 KB Output is correct
45 Correct 47 ms 4344 KB Output is correct
46 Correct 110 ms 4160 KB Output is correct
47 Correct 37 ms 4180 KB Output is correct
48 Correct 54 ms 4208 KB Output is correct
49 Correct 42 ms 4372 KB Output is correct
50 Correct 403 ms 4160 KB Output is correct
51 Correct 457 ms 4340 KB Output is correct
52 Correct 401 ms 4156 KB Output is correct
53 Correct 425 ms 4260 KB Output is correct
54 Correct 393 ms 4276 KB Output is correct
55 Correct 445 ms 4308 KB Output is correct
56 Correct 386 ms 4160 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 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 27 ms 2860 KB Output is correct
10 Correct 8 ms 1232 KB Output is correct
11 Correct 18 ms 2376 KB Output is correct
12 Correct 19 ms 2252 KB Output is correct
13 Correct 29 ms 2536 KB Output is correct
14 Correct 16 ms 2252 KB Output is correct
15 Correct 4 ms 720 KB Output is correct
16 Correct 16 ms 2252 KB Output is correct
17 Correct 39 ms 4072 KB Output is correct
18 Correct 22 ms 2468 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 12 ms 1360 KB Output is correct
35 Correct 31 ms 3736 KB Output is correct
36 Correct 41 ms 3648 KB Output is correct
37 Correct 110 ms 3644 KB Output is correct
38 Correct 119 ms 3656 KB Output is correct
39 Correct 30 ms 3768 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 24 ms 2124 KB Output is correct
42 Correct 41 ms 3776 KB Output is correct
43 Correct 5 ms 844 KB Output is correct
44 Correct 28 ms 2252 KB Output is correct
45 Correct 38 ms 3528 KB Output is correct
46 Correct 43 ms 3648 KB Output is correct
47 Correct 13 ms 2000 KB Output is correct
48 Correct 2 ms 460 KB Output is correct
49 Correct 102 ms 4248 KB Output is correct
50 Correct 47 ms 4268 KB Output is correct
51 Correct 62 ms 4172 KB Output is correct
52 Correct 45 ms 4220 KB Output is correct
53 Correct 47 ms 4344 KB Output is correct
54 Correct 110 ms 4160 KB Output is correct
55 Correct 37 ms 4180 KB Output is correct
56 Correct 54 ms 4208 KB Output is correct
57 Correct 42 ms 4372 KB Output is correct
58 Correct 403 ms 4160 KB Output is correct
59 Correct 457 ms 4340 KB Output is correct
60 Correct 401 ms 4156 KB Output is correct
61 Correct 425 ms 4260 KB Output is correct
62 Correct 393 ms 4276 KB Output is correct
63 Correct 445 ms 4308 KB Output is correct
64 Correct 386 ms 4160 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 1 ms 204 KB Output is correct
67 Correct 506 ms 4268 KB Output is correct
68 Correct 498 ms 4296 KB Output is correct
69 Correct 433 ms 4212 KB Output is correct
70 Correct 480 ms 4160 KB Output is correct
71 Correct 469 ms 4192 KB Output is correct
72 Correct 456 ms 4196 KB Output is correct
73 Correct 452 ms 4256 KB Output is correct
74 Correct 501 ms 4160 KB Output is correct
75 Correct 476 ms 4160 KB Output is correct
76 Correct 527 ms 4264 KB Output is correct
77 Correct 109 ms 4328 KB Output is correct
78 Correct 51 ms 4172 KB Output is correct
79 Correct 79 ms 4280 KB Output is correct
80 Correct 90 ms 4280 KB Output is correct
81 Correct 85 ms 4240 KB Output is correct
82 Correct 69 ms 4296 KB Output is correct
83 Correct 58 ms 4296 KB Output is correct
84 Correct 316 ms 4156 KB Output is correct
85 Correct 542 ms 4300 KB Output is correct
86 Correct 2 ms 204 KB Output is correct
87 Correct 2 ms 224 KB Output is correct
88 Correct 522 ms 4316 KB Output is correct
89 Correct 166 ms 1360 KB Output is correct
90 Correct 1 ms 220 KB Output is correct