Submission #495516

# Submission time Handle Problem Language Result Execution time Memory
495516 2021-12-19T08:37:12 Z AQ0212 Chessboard (IZhO18_chessboard) C++17
70 / 100
2000 ms 5844 KB
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>

#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(x) x.begin(), x.end()

using namespace std;

ll inf2 = 3e18;

ll x1[ 100111 ], x2[ 100111 ];
ll y11[ 100111 ], y2[ 100111 ], ans = inf2;
vector < ll > divv;
map < ll , ll > mp;

ll white_first(ll sz, ll n, ll m){
	ll sqrs = n / sz;
	ll cnt_move = (sqrs & 1) ? ((((sqrs >> 1) * ((sqrs >> 1) + 1)) << 1) * (sz * sz)) : (sqrs >> 1) * (sqrs) * (sz * sz);
	// cout << "w: " << cnt_move << " sz: " << sz << "\n";

	for(int i = 1; i <= m; i ++){
		if(x1[ i ] == x2[ i ] && y11[ i ] == y2[ i ]){
			if(((x1[ i ] + sz - 1) / sz) & 1)
				if(((y11[ i ] + sz - 1) / sz) & 1)
					cnt_move ++;
				else
					cnt_move --;
			else
				if(((y11[ i ] + sz - 1) / sz) & 1)
					cnt_move --;
				else
					cnt_move ++;
			continue;
		}
		for(int j = 1, j2 = 1; j <= n; j += sz, j2 ++){

			if(j <= x1[ i ] && x2[ i ] <= (j2 * sz)){
				for(int k = 1, k2 = 1; k <= n; k += sz, k2 ++){

					if(k <= y11[ i ] && y2[ i ] <= (k2 * sz)){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move += (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
						break;
					}else if(k <= y11[ i ] && y11[ i ] <= (k2 * sz) && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move += (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);

					}else if(y11[ i ] <= k && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - k + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move += (x2[ i ] - x1[ i ] + 1) * ((k2 * sz) - k + 1);

					}else{
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - k + 1);
							else
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - k + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - k + 1);
							else
								cnt_move += (x2[ i ] - x1[ i ] + 1) * (y2[ i ] - k + 1);
						// break;
					}
				}

			}else if(j <= x1[ i ] && x1[ i ] <= (j2 * sz) && (j2 * sz) <= x2[ i ]){
				for(int k = 1, k2 = 1; k <= n; k += sz, k2 ++){

					if(k <= y11[ i ] && y2[ i ] <= (k2 * sz)){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - y11[ i ] + 1);
						break;
					}else if(k <= y11[ i ] && y11[ i ] <= (k2 * sz) && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - y11[ i ] + 1);

					}else if(y11[ i ] <= k && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - k + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * ((k2 * sz) - k + 1);

					}else{
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - k + 1);
							else
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - k + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - k + 1);
							else
								cnt_move += ((j2 * sz) - x1[ i ] + 1) * (y2[ i ] - k + 1);
						// break;
					}
				}

			}else if(x1[ i ] <= j && (j2 * sz) <= x2[ i ]){
				for(int k = 1, k2 = 1; k <= n; k += sz, k2 ++){

					if(k <= y11[ i ] && y2[ i ] <= (k2 * sz)){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - j + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move -= ((j2 * sz) - j + 1) * (y2[ i ] - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - j + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move += ((j2 * sz) - j + 1) * (y2[ i ] - y11[ i ] + 1);
						break;
					}else if(k <= y11[ i ] && y11[ i ] <= (k2 * sz) && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - j + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move -= ((j2 * sz) - j + 1) * ((k2 * sz) - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - j + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move += ((j2 * sz) - j + 1) * ((k2 * sz) - y11[ i ] + 1);

					}else if(y11[ i ] <= k && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - j + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move -= ((j2 * sz) - j + 1) * ((k2 * sz) - k + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - j + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move += ((j2 * sz) - j + 1) * ((k2 * sz) - k + 1);

					}else{
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += ((j2 * sz) - j + 1) * (y2[ i ] - k + 1);
							else
								cnt_move -= ((j2 * sz) - j + 1) * (y2[ i ] - k + 1);
						else
							if(k2 & 1)
								cnt_move -= ((j2 * sz) - j + 1) * (y2[ i ] - k + 1);
							else
								cnt_move += ((j2 * sz) - j + 1) * (y2[ i ] - k + 1);
						// break;
					}
				}

			}else{
				for(int k = 1, k2 = 1; k <= n; k += sz, k2 ++){

					if(k <= y11[ i ] && y2[ i ] <= (k2 * sz)){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - j + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move -= (x2[ i ] - j + 1) * (y2[ i ] - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - j + 1) * (y2[ i ] - y11[ i ] + 1);
							else
								cnt_move += (x2[ i ] - j + 1) * (y2[ i ] - y11[ i ] + 1);
						break;
					}else if(k <= y11[ i ] && y11[ i ] <= (k2 * sz) && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - j + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move -= (x2[ i ] - j + 1) * ((k2 * sz) - y11[ i ] + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - j + 1) * ((k2 * sz) - y11[ i ] + 1);
							else
								cnt_move += (x2[ i ] - j + 1) * ((k2 * sz) - y11[ i ] + 1);

					}else if(y11[ i ] <= k && (k2 * sz) <= y2[ i ]){
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - j + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move -= (x2[ i ] - j + 1) * ((k2 * sz) - k + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - j + 1) * ((k2 * sz) - k + 1);
							else
								cnt_move += (x2[ i ] - j + 1) * ((k2 * sz) - k + 1);

					}else{
						if(j2 & 1)
							if(k2 & 1)
								cnt_move += (x2[ i ] - j + 1) * (y2[ i ] - k + 1);
							else
								cnt_move -= (x2[ i ] - j + 1) * (y2[ i ] - k + 1);
						else
							if(k2 & 1)
								cnt_move -= (x2[ i ] - j + 1) * (y2[ i ] - k + 1);
							else
								cnt_move += (x2[ i ] - j + 1) * (y2[ i ] - k + 1);
						// break;
					}
				}

			}
		}
	}

	return cnt_move;
}

int main(){
	ll n, m;

	cin >> n >> m;

	for(int i = 1; i <= m; i ++){
		cin >> x1[ i ] >> y11[ i ] >> x2[ i ] >> y2[ i ];
	}

	divv.pb(1);
	for(int i = 2; i <= ((ll)ceil(sqrt(n))); i ++){
		if(n % i == 0 && !mp[ i ] && i != n){
			divv.pb(i);
			mp[ i ] ++;
			if(!mp[ n / i ]){
				divv.pb(n / i);
				mp[ n / i ] ++;
			}
		}
	}

	sort(all(divv));

	for(int i = 0; i < divv.size(); i ++){
		ll white = white_first(divv[ i ], n, m);
		
		ans = min(ans, min((n * n) - white, white));
		// cout << white << " " << (n * n) - white << "\n";
	}

	cout << ans;
}

Compilation message

chessboard.cpp: In function 'int main()':
chessboard.cpp:294:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  294 |  for(int i = 0; i < divv.size(); i ++){
      |                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 292 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 3632 KB Output is correct
2 Correct 16 ms 1072 KB Output is correct
3 Correct 41 ms 2372 KB Output is correct
4 Correct 54 ms 2656 KB Output is correct
5 Correct 60 ms 3248 KB Output is correct
6 Correct 35 ms 2140 KB Output is correct
7 Correct 8 ms 580 KB Output is correct
8 Correct 38 ms 2108 KB Output is correct
9 Correct 91 ms 5172 KB Output is correct
10 Correct 52 ms 3012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 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 308 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 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 308 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 23 ms 1476 KB Output is correct
17 Correct 67 ms 4272 KB Output is correct
18 Correct 85 ms 4884 KB Output is correct
19 Correct 106 ms 4412 KB Output is correct
20 Correct 130 ms 4912 KB Output is correct
21 Correct 67 ms 4036 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 39 ms 2372 KB Output is correct
24 Correct 73 ms 4548 KB Output is correct
25 Correct 9 ms 692 KB Output is correct
26 Correct 47 ms 2920 KB Output is correct
27 Correct 62 ms 3484 KB Output is correct
28 Correct 83 ms 4812 KB Output is correct
29 Correct 28 ms 1856 KB Output is correct
30 Correct 3 ms 432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 3632 KB Output is correct
2 Correct 16 ms 1072 KB Output is correct
3 Correct 41 ms 2372 KB Output is correct
4 Correct 54 ms 2656 KB Output is correct
5 Correct 60 ms 3248 KB Output is correct
6 Correct 35 ms 2140 KB Output is correct
7 Correct 8 ms 580 KB Output is correct
8 Correct 38 ms 2108 KB Output is correct
9 Correct 91 ms 5172 KB Output is correct
10 Correct 52 ms 3012 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 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 308 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 308 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 23 ms 1476 KB Output is correct
27 Correct 67 ms 4272 KB Output is correct
28 Correct 85 ms 4884 KB Output is correct
29 Correct 106 ms 4412 KB Output is correct
30 Correct 130 ms 4912 KB Output is correct
31 Correct 67 ms 4036 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 39 ms 2372 KB Output is correct
34 Correct 73 ms 4548 KB Output is correct
35 Correct 9 ms 692 KB Output is correct
36 Correct 47 ms 2920 KB Output is correct
37 Correct 62 ms 3484 KB Output is correct
38 Correct 83 ms 4812 KB Output is correct
39 Correct 28 ms 1856 KB Output is correct
40 Correct 3 ms 432 KB Output is correct
41 Correct 117 ms 4852 KB Output is correct
42 Correct 98 ms 5412 KB Output is correct
43 Correct 114 ms 4892 KB Output is correct
44 Correct 98 ms 5316 KB Output is correct
45 Correct 99 ms 5664 KB Output is correct
46 Correct 136 ms 5408 KB Output is correct
47 Correct 87 ms 4912 KB Output is correct
48 Correct 107 ms 4992 KB Output is correct
49 Correct 95 ms 4808 KB Output is correct
50 Correct 294 ms 5296 KB Output is correct
51 Correct 326 ms 5668 KB Output is correct
52 Correct 295 ms 5188 KB Output is correct
53 Correct 333 ms 5672 KB Output is correct
54 Correct 294 ms 5204 KB Output is correct
55 Correct 328 ms 5844 KB Output is correct
56 Correct 278 ms 4932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 292 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
9 Correct 65 ms 3632 KB Output is correct
10 Correct 16 ms 1072 KB Output is correct
11 Correct 41 ms 2372 KB Output is correct
12 Correct 54 ms 2656 KB Output is correct
13 Correct 60 ms 3248 KB Output is correct
14 Correct 35 ms 2140 KB Output is correct
15 Correct 8 ms 580 KB Output is correct
16 Correct 38 ms 2108 KB Output is correct
17 Correct 91 ms 5172 KB Output is correct
18 Correct 52 ms 3012 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 328 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 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 308 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 308 KB Output is correct
32 Correct 1 ms 308 KB Output is correct
33 Correct 1 ms 308 KB Output is correct
34 Correct 23 ms 1476 KB Output is correct
35 Correct 67 ms 4272 KB Output is correct
36 Correct 85 ms 4884 KB Output is correct
37 Correct 106 ms 4412 KB Output is correct
38 Correct 130 ms 4912 KB Output is correct
39 Correct 67 ms 4036 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 39 ms 2372 KB Output is correct
42 Correct 73 ms 4548 KB Output is correct
43 Correct 9 ms 692 KB Output is correct
44 Correct 47 ms 2920 KB Output is correct
45 Correct 62 ms 3484 KB Output is correct
46 Correct 83 ms 4812 KB Output is correct
47 Correct 28 ms 1856 KB Output is correct
48 Correct 3 ms 432 KB Output is correct
49 Correct 117 ms 4852 KB Output is correct
50 Correct 98 ms 5412 KB Output is correct
51 Correct 114 ms 4892 KB Output is correct
52 Correct 98 ms 5316 KB Output is correct
53 Correct 99 ms 5664 KB Output is correct
54 Correct 136 ms 5408 KB Output is correct
55 Correct 87 ms 4912 KB Output is correct
56 Correct 107 ms 4992 KB Output is correct
57 Correct 95 ms 4808 KB Output is correct
58 Correct 294 ms 5296 KB Output is correct
59 Correct 326 ms 5668 KB Output is correct
60 Correct 295 ms 5188 KB Output is correct
61 Correct 333 ms 5672 KB Output is correct
62 Correct 294 ms 5204 KB Output is correct
63 Correct 328 ms 5844 KB Output is correct
64 Correct 278 ms 4932 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Execution timed out 2033 ms 5412 KB Time limit exceeded
68 Halted 0 ms 0 KB -