답안 #287800

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
287800 2020-09-01T01:17:43 Z rama_pang Chessboard (IZhO18_chessboard) C++14
100 / 100
528 ms 4460 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 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 2936 KB Output is correct
2 Correct 10 ms 1280 KB Output is correct
3 Correct 18 ms 2428 KB Output is correct
4 Correct 25 ms 2300 KB Output is correct
5 Correct 24 ms 2556 KB Output is correct
6 Correct 20 ms 2300 KB Output is correct
7 Correct 4 ms 768 KB Output is correct
8 Correct 16 ms 2300 KB Output is correct
9 Correct 42 ms 4216 KB Output is correct
10 Correct 27 ms 2556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 360 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 360 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 16 ms 1408 KB Output is correct
17 Correct 32 ms 3824 KB Output is correct
18 Correct 50 ms 3824 KB Output is correct
19 Correct 101 ms 3824 KB Output is correct
20 Correct 104 ms 3832 KB Output is correct
21 Correct 30 ms 3832 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 22 ms 2172 KB Output is correct
24 Correct 46 ms 3740 KB Output is correct
25 Correct 5 ms 896 KB Output is correct
26 Correct 24 ms 2300 KB Output is correct
27 Correct 35 ms 3568 KB Output is correct
28 Correct 43 ms 3832 KB Output is correct
29 Correct 13 ms 2044 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 2936 KB Output is correct
2 Correct 10 ms 1280 KB Output is correct
3 Correct 18 ms 2428 KB Output is correct
4 Correct 25 ms 2300 KB Output is correct
5 Correct 24 ms 2556 KB Output is correct
6 Correct 20 ms 2300 KB Output is correct
7 Correct 4 ms 768 KB Output is correct
8 Correct 16 ms 2300 KB Output is correct
9 Correct 42 ms 4216 KB Output is correct
10 Correct 27 ms 2556 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 360 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 16 ms 1408 KB Output is correct
27 Correct 32 ms 3824 KB Output is correct
28 Correct 50 ms 3824 KB Output is correct
29 Correct 101 ms 3824 KB Output is correct
30 Correct 104 ms 3832 KB Output is correct
31 Correct 30 ms 3832 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 22 ms 2172 KB Output is correct
34 Correct 46 ms 3740 KB Output is correct
35 Correct 5 ms 896 KB Output is correct
36 Correct 24 ms 2300 KB Output is correct
37 Correct 35 ms 3568 KB Output is correct
38 Correct 43 ms 3832 KB Output is correct
39 Correct 13 ms 2044 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 92 ms 4328 KB Output is correct
42 Correct 50 ms 4308 KB Output is correct
43 Correct 61 ms 4288 KB Output is correct
44 Correct 48 ms 4336 KB Output is correct
45 Correct 42 ms 4404 KB Output is correct
46 Correct 97 ms 4340 KB Output is correct
47 Correct 38 ms 4336 KB Output is correct
48 Correct 54 ms 4336 KB Output is correct
49 Correct 42 ms 4364 KB Output is correct
50 Correct 471 ms 4332 KB Output is correct
51 Correct 373 ms 4344 KB Output is correct
52 Correct 349 ms 4332 KB Output is correct
53 Correct 381 ms 4340 KB Output is correct
54 Correct 350 ms 4344 KB Output is correct
55 Correct 396 ms 4460 KB Output is correct
56 Correct 356 ms 4336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 28 ms 2936 KB Output is correct
10 Correct 10 ms 1280 KB Output is correct
11 Correct 18 ms 2428 KB Output is correct
12 Correct 25 ms 2300 KB Output is correct
13 Correct 24 ms 2556 KB Output is correct
14 Correct 20 ms 2300 KB Output is correct
15 Correct 4 ms 768 KB Output is correct
16 Correct 16 ms 2300 KB Output is correct
17 Correct 42 ms 4216 KB Output is correct
18 Correct 27 ms 2556 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 360 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 16 ms 1408 KB Output is correct
35 Correct 32 ms 3824 KB Output is correct
36 Correct 50 ms 3824 KB Output is correct
37 Correct 101 ms 3824 KB Output is correct
38 Correct 104 ms 3832 KB Output is correct
39 Correct 30 ms 3832 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 22 ms 2172 KB Output is correct
42 Correct 46 ms 3740 KB Output is correct
43 Correct 5 ms 896 KB Output is correct
44 Correct 24 ms 2300 KB Output is correct
45 Correct 35 ms 3568 KB Output is correct
46 Correct 43 ms 3832 KB Output is correct
47 Correct 13 ms 2044 KB Output is correct
48 Correct 2 ms 512 KB Output is correct
49 Correct 92 ms 4328 KB Output is correct
50 Correct 50 ms 4308 KB Output is correct
51 Correct 61 ms 4288 KB Output is correct
52 Correct 48 ms 4336 KB Output is correct
53 Correct 42 ms 4404 KB Output is correct
54 Correct 97 ms 4340 KB Output is correct
55 Correct 38 ms 4336 KB Output is correct
56 Correct 54 ms 4336 KB Output is correct
57 Correct 42 ms 4364 KB Output is correct
58 Correct 471 ms 4332 KB Output is correct
59 Correct 373 ms 4344 KB Output is correct
60 Correct 349 ms 4332 KB Output is correct
61 Correct 381 ms 4340 KB Output is correct
62 Correct 350 ms 4344 KB Output is correct
63 Correct 396 ms 4460 KB Output is correct
64 Correct 356 ms 4336 KB Output is correct
65 Correct 1 ms 384 KB Output is correct
66 Correct 1 ms 384 KB Output is correct
67 Correct 463 ms 4336 KB Output is correct
68 Correct 463 ms 4336 KB Output is correct
69 Correct 483 ms 4336 KB Output is correct
70 Correct 435 ms 4336 KB Output is correct
71 Correct 443 ms 4368 KB Output is correct
72 Correct 422 ms 4336 KB Output is correct
73 Correct 423 ms 4364 KB Output is correct
74 Correct 473 ms 4320 KB Output is correct
75 Correct 460 ms 4344 KB Output is correct
76 Correct 476 ms 4380 KB Output is correct
77 Correct 110 ms 4336 KB Output is correct
78 Correct 49 ms 4216 KB Output is correct
79 Correct 79 ms 4336 KB Output is correct
80 Correct 88 ms 4256 KB Output is correct
81 Correct 84 ms 4280 KB Output is correct
82 Correct 78 ms 4344 KB Output is correct
83 Correct 57 ms 4336 KB Output is correct
84 Correct 316 ms 4336 KB Output is correct
85 Correct 528 ms 4328 KB Output is correct
86 Correct 2 ms 384 KB Output is correct
87 Correct 1 ms 384 KB Output is correct
88 Correct 517 ms 4460 KB Output is correct
89 Correct 103 ms 1408 KB Output is correct
90 Correct 2 ms 384 KB Output is correct