Submission #497898

#TimeUsernameProblemLanguageResultExecution timeMemory
497898AQ0212Chessboard (IZhO18_chessboard)C++17
100 / 100
322 ms5816 KiB
#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 ], cnt_move[ 100111 ], ans = inf2;
vector < ll > divv;
map < ll , ll > mp;
 
int main(){
	ll n, m;
 
	scanf("%lld%lld", &n, &m);
 
	for(int i = 1; i <= m; i ++){
		scanf("%lld%lld%lld%lld", &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 ] ++;
			}
		}
	}
 
	for(int i = 0; i < divv.size(); i ++){
		ll sqrs = n / divv[ i ];
		cnt_move[ i ] = (sqrs & 1) ? ((((sqrs >> 1) * ((sqrs >> 1) + 1)) << 1) * (divv[ i ] * divv[ i ])) : (sqrs >> 1) * (sqrs) * (divv[ i ] * divv[ i ]);
		cnt_move[ i ] = (n * n) - cnt_move[ i ];
	}
 
	for(int i = 1; i <= m; i ++){
		for(int j = 0; j < divv.size(); j ++){
			ll xstart = (x1[ i ] - 1) / (divv[ j ] * 2) * divv[ j ];
			ll ystart = (y11[ i ] - 1) / (divv[ j ] * 2) * divv[ j ];
			ll xend = x2[ i ] / (divv[ j ] * 2) * divv[ j ];
			ll yend = y2[ i ] / (divv[ j ] * 2) * divv[ j ];

			xstart += min((x1[ i ] - 1) % (divv[ j ] * 2), divv[ j ]);
			ystart += min((y11[ i ] - 1) % (divv[ j ] * 2), divv[ j ]);
			xend += min(x2[ i ] % (divv[ j ] * 2), divv[ j ]);
			yend += min(y2[ i ] % (divv[ j ] * 2), divv[ j ]);
			ll xtot = xend - xstart;
			ll ytot = yend - ystart;
			
			ll xadd = (x2[ i ] - x1[ i ] + 1) - xtot;
			ll yadd = (y2[ i ] - y11[ i ] + 1) - ytot;

			cnt_move[ j ] -= (xtot * ytot) + (xadd * yadd);
			cnt_move[ j ] += (xtot * yadd) + (xadd * ytot);
		}
	}
	
	for(int i = 0; i < divv.size(); i ++){
		ans = min(ans, min(cnt_move[ i ], (n * n) - cnt_move[ i ]));
	}
	printf("%lld", ans);
}

/*

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

6 5
1 2 1 2
5 5 5 5
3 3 4 4
2 1 2 1
3 6 3 6

*/

Compilation message (stderr)

chessboard.cpp: In function 'int main()':
chessboard.cpp:47:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for(int i = 0; i < divv.size(); i ++){
      |                 ~~^~~~~~~~~~~~~
chessboard.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int j = 0; j < divv.size(); j ++){
      |                  ~~^~~~~~~~~~~~~
chessboard.cpp:75:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |  for(int i = 0; i < divv.size(); i ++){
      |                 ~~^~~~~~~~~~~~~
chessboard.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |  scanf("%lld%lld", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~
chessboard.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |   scanf("%lld%lld%lld%lld", &x1[ i ], &y11[ i ], &x2[ i ], &y2[ i ]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...