Submission #385601

# Submission time Handle Problem Language Result Execution time Memory
385601 2021-04-04T15:52:19 Z patrikpavic2 Chessboard (IZhO18_chessboard) C++17
100 / 100
433 ms 6548 KB
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

const ll N = 2e5 + 500;

ll n, k, xx1[N], xx2[N], yy1[N], yy2[N], P[N];
ll pov = 0;

ll stranica(ll st, ll x1, ll x2){
	return P[x2] - (x1 ? P[x1 - 1] : 0);
}

bool boja(ll st, ll x, ll y){
	return ((x / st) + (y / st)) & 1;
}

ll calc(ll st, ll x1, ll y1, ll x2, ll y2){
	ll dx = stranica(st, x1, x2);
	ll dy = stranica(st, y1, y2);
	if(x1 / st == x2 / st)
		return (x2 - x1 + 1) * (boja(st, x1, 0) ? (y2 - y1 + 1 - dy) : dy);
	if(y1 / st == y2 / st)
		return (y2 - y1 + 1) * (boja(st, y1, 0) ? (x2 - x1 + 1 - dx) : dx);
	ll ux1 = x1 + (st - x1 % st) % st;
	ll uy1 = y1 + (st - y1 % st) % st;
	ll ux2 = x2 - (x2 % st  + 1) % st;
	ll uy2 = y2 - (y2 % st + 1) % st;
	ll rub = 0;
	rub += (ll)(boja(st, 0, y1) ? (x2 - x1 + 1) - dx : dx) * (uy1 - y1);
	rub += (ll)(boja(st, 0, y2) ? (x2 - x1 + 1) - dx : dx) * (y2 - uy2);
	rub += (ll)(boja(st, x1, 0) ? (y2 - y1 + 1) - dy : dy) * (ux1 - x1);
	rub += (ll)(boja(st, x2, 0) ? (y2 - y1 + 1) - dy : dy) * (x2 - ux2);
	if(boja(st, x1, y1)) rub -= (ll)(ux1 - x1) * (uy1 - y1);	
	if(boja(st, x1, y2)) rub -= (ll)(ux1 - x1) * (y2 - uy2);	
	if(boja(st, x2, y1)) rub -= (ll)(x2 - ux2) * (uy1 - y1);	
	if(boja(st, x2, y2)) rub -= (ll)(x2 - ux2) * (y2 - uy2);	
//	prllf("rub = %lld\n", rub);
	ll sred = 0;
	ll Dx = (ux2 - ux1 + 1) / st;
	ll Dy = (uy2 - uy1 + 1) / st;
	//prllf("Dx = %d Dy = %d\n", Dx, Dy);
	sred += (ll)st * st * ((ll)Dx * Dy / 2LL + (boja(st, ux1, uy1) && ((Dx & 1) && (Dy & 1))));
	return rub + sred;	
}

int main(){
	scanf("%lld%lld", &n, &k);	
	for(ll i = 0;i < k;i++){
		scanf("%lld%lld%lld%lld", xx1 + i, yy1 + i, xx2 + i, yy2 + i);
		xx1[i]--, xx2[i]--, yy1[i]--, yy2[i]--;
		pov += (ll)(xx2[i] - xx1[i] + 1) * (yy2[i] - yy1[i] + 1);
	}
	ll sol = (ll)n * n;
	for(ll i = 1;i < n;i++){
		if(n % i) continue;
		for(ll j = 0;j < n;j++)
			P[j] = (j ? P[j - 1] : 0) + ((j / i) & 1);
		ll cur = 0;
		for(ll j = 0;j < k;j++)
			cur += calc(i, xx1[j], yy1[j], xx2[j], yy2[j]);
		//printf("		cur = %lld\n", cur);
		ll crnih = (ll)i * i * (((ll)(n / i) * (n / i)) / 2LL);
		//printf("crnih = %lld\n", crnih);
		cur = crnih - cur + pov - cur;
		//printf("cur = %lld\n", cur);
		sol = min(sol, cur);
		sol = min(sol, (ll)n * n - cur);
	}
	printf("%lld\n", sol);
	return 0;
}

Compilation message

chessboard.cpp: In function 'int main()':
chessboard.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |  scanf("%lld%lld", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~
chessboard.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   54 |   scanf("%lld%lld%lld%lld", xx1 + i, yy1 + i, xx2 + i, yy2 + i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 4204 KB Output is correct
2 Correct 8 ms 1260 KB Output is correct
3 Correct 26 ms 3180 KB Output is correct
4 Correct 27 ms 2668 KB Output is correct
5 Correct 38 ms 3948 KB Output is correct
6 Correct 16 ms 2668 KB Output is correct
7 Correct 5 ms 876 KB Output is correct
8 Correct 18 ms 2668 KB Output is correct
9 Correct 56 ms 5484 KB Output is correct
10 Correct 23 ms 3308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 3 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 4 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 4 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 3 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 4 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 4 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
16 Correct 11 ms 1644 KB Output is correct
17 Correct 30 ms 4332 KB Output is correct
18 Correct 39 ms 4972 KB Output is correct
19 Correct 65 ms 4588 KB Output is correct
20 Correct 95 ms 5100 KB Output is correct
21 Correct 29 ms 4204 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 20 ms 2412 KB Output is correct
24 Correct 76 ms 4588 KB Output is correct
25 Correct 9 ms 748 KB Output is correct
26 Correct 22 ms 3052 KB Output is correct
27 Correct 57 ms 3564 KB Output is correct
28 Correct 37 ms 4844 KB Output is correct
29 Correct 13 ms 1900 KB Output is correct
30 Correct 3 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 4204 KB Output is correct
2 Correct 8 ms 1260 KB Output is correct
3 Correct 26 ms 3180 KB Output is correct
4 Correct 27 ms 2668 KB Output is correct
5 Correct 38 ms 3948 KB Output is correct
6 Correct 16 ms 2668 KB Output is correct
7 Correct 5 ms 876 KB Output is correct
8 Correct 18 ms 2668 KB Output is correct
9 Correct 56 ms 5484 KB Output is correct
10 Correct 23 ms 3308 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 3 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 4 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 4 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 11 ms 1644 KB Output is correct
27 Correct 30 ms 4332 KB Output is correct
28 Correct 39 ms 4972 KB Output is correct
29 Correct 65 ms 4588 KB Output is correct
30 Correct 95 ms 5100 KB Output is correct
31 Correct 29 ms 4204 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 20 ms 2412 KB Output is correct
34 Correct 76 ms 4588 KB Output is correct
35 Correct 9 ms 748 KB Output is correct
36 Correct 22 ms 3052 KB Output is correct
37 Correct 57 ms 3564 KB Output is correct
38 Correct 37 ms 4844 KB Output is correct
39 Correct 13 ms 1900 KB Output is correct
40 Correct 3 ms 492 KB Output is correct
41 Correct 83 ms 5740 KB Output is correct
42 Correct 48 ms 6252 KB Output is correct
43 Correct 58 ms 5740 KB Output is correct
44 Correct 44 ms 6188 KB Output is correct
45 Correct 57 ms 6508 KB Output is correct
46 Correct 150 ms 6284 KB Output is correct
47 Correct 39 ms 5868 KB Output is correct
48 Correct 62 ms 5868 KB Output is correct
49 Correct 49 ms 5868 KB Output is correct
50 Correct 295 ms 6036 KB Output is correct
51 Correct 341 ms 6380 KB Output is correct
52 Correct 305 ms 6000 KB Output is correct
53 Correct 315 ms 6380 KB Output is correct
54 Correct 293 ms 5924 KB Output is correct
55 Correct 329 ms 6536 KB Output is correct
56 Correct 273 ms 5740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 36 ms 4204 KB Output is correct
10 Correct 8 ms 1260 KB Output is correct
11 Correct 26 ms 3180 KB Output is correct
12 Correct 27 ms 2668 KB Output is correct
13 Correct 38 ms 3948 KB Output is correct
14 Correct 16 ms 2668 KB Output is correct
15 Correct 5 ms 876 KB Output is correct
16 Correct 18 ms 2668 KB Output is correct
17 Correct 56 ms 5484 KB Output is correct
18 Correct 23 ms 3308 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 3 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 4 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 4 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 2 ms 364 KB Output is correct
34 Correct 11 ms 1644 KB Output is correct
35 Correct 30 ms 4332 KB Output is correct
36 Correct 39 ms 4972 KB Output is correct
37 Correct 65 ms 4588 KB Output is correct
38 Correct 95 ms 5100 KB Output is correct
39 Correct 29 ms 4204 KB Output is correct
40 Correct 2 ms 364 KB Output is correct
41 Correct 20 ms 2412 KB Output is correct
42 Correct 76 ms 4588 KB Output is correct
43 Correct 9 ms 748 KB Output is correct
44 Correct 22 ms 3052 KB Output is correct
45 Correct 57 ms 3564 KB Output is correct
46 Correct 37 ms 4844 KB Output is correct
47 Correct 13 ms 1900 KB Output is correct
48 Correct 3 ms 492 KB Output is correct
49 Correct 83 ms 5740 KB Output is correct
50 Correct 48 ms 6252 KB Output is correct
51 Correct 58 ms 5740 KB Output is correct
52 Correct 44 ms 6188 KB Output is correct
53 Correct 57 ms 6508 KB Output is correct
54 Correct 150 ms 6284 KB Output is correct
55 Correct 39 ms 5868 KB Output is correct
56 Correct 62 ms 5868 KB Output is correct
57 Correct 49 ms 5868 KB Output is correct
58 Correct 295 ms 6036 KB Output is correct
59 Correct 341 ms 6380 KB Output is correct
60 Correct 305 ms 6000 KB Output is correct
61 Correct 315 ms 6380 KB Output is correct
62 Correct 293 ms 5924 KB Output is correct
63 Correct 329 ms 6536 KB Output is correct
64 Correct 273 ms 5740 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 1 ms 364 KB Output is correct
67 Correct 367 ms 6124 KB Output is correct
68 Correct 372 ms 6252 KB Output is correct
69 Correct 304 ms 5612 KB Output is correct
70 Correct 317 ms 6004 KB Output is correct
71 Correct 339 ms 5932 KB Output is correct
72 Correct 356 ms 5740 KB Output is correct
73 Correct 337 ms 5720 KB Output is correct
74 Correct 433 ms 6184 KB Output is correct
75 Correct 338 ms 5892 KB Output is correct
76 Correct 348 ms 6252 KB Output is correct
77 Correct 86 ms 6508 KB Output is correct
78 Correct 51 ms 6144 KB Output is correct
79 Correct 66 ms 5740 KB Output is correct
80 Correct 84 ms 5996 KB Output is correct
81 Correct 72 ms 5612 KB Output is correct
82 Correct 62 ms 6252 KB Output is correct
83 Correct 53 ms 5868 KB Output is correct
84 Correct 244 ms 6508 KB Output is correct
85 Correct 386 ms 6380 KB Output is correct
86 Correct 70 ms 1004 KB Output is correct
87 Correct 72 ms 1044 KB Output is correct
88 Correct 389 ms 6548 KB Output is correct
89 Correct 134 ms 2156 KB Output is correct
90 Correct 74 ms 1132 KB Output is correct