제출 #1269749

#제출 시각아이디문제언어결과실행 시간메모리
1269749thdh__Chessboard (IZhO18_chessboard)C++20
100 / 100
196 ms632 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll

using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

/*
Competitive Programming notes that I need to study & fix my dumbass self:

1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you

2. Stress: 
- Always try generating big testcases and try if they run

3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time

Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'

+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'

I hate offline because I am dumb
*/

typedef pair<int, int> ii;
const int N = 3e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} 

int n,k;
int cw[N], cb[N];
vector<int> divisor;

int block(int i, int d) 
{
	return (i-1) / d;
}

int st(int i, int d) 
{
	return i*d + 1;
}

int ed(int i, int d) 
{
	return i*d + d;
}

int odd(int l, int r, int d) 
{
	int bl = block(l, d), br = block(r, d);
	if (bl == br) 
	{
		if (bl % 2 == 1) return r-l+1;
		else return 0;
	} else if (br == bl+1) 
	{
		if (bl % 2 == 1) return ed(bl, d) - l + 1;
		else return r - st(br, d) + 1;
	} else 
	{
		int ret = 0;
		if (bl % 2 == 1) ret += ed(bl, d) - l + 1;
		if (br % 2 == 1) ret += r - st(br, d) + 1;
		int lo = bl+1, hi = br-1;
		lo += 1 - (lo % 2); hi -= 1 - (hi % 2);
		if (lo <= hi) ret += ((hi-lo) / 2 + 1) * d;
		return ret;
	}
}

int even(int l, int r, int d) 
{
	int bl = block(l, d), br = block(r, d);
	if (bl == br) 
	{
		if (bl % 2 == 0) return r-l+1;
		else return 0;
	} else if (br == bl+1) 
	{
		if (bl % 2 == 0) return ed(bl, d) - l + 1;
		else return r - st(br, d) + 1;
	} else 
	{
		int ret = 0;
		if (bl % 2 == 0) ret += ed(bl, d) - l + 1;
		if (br % 2 == 0) ret += r - st(br, d) + 1;
		int lo = bl+1, hi = br-1;
		lo += (lo % 2); hi -= (hi % 2);
		if (lo <= hi) ret += ((hi-lo) / 2 + 1) * d;
		return ret;
	}
}

void solve()
{
	cin>>n>>k;
	for (int i = 1; i < n; i++) if (n % i == 0) divisor.pb(i);
	int sum = 0;
	while (k--) 
	{
		int x1, x2, y1, y2;
		cin>>x1>>y1>>x2>>y2;
		int h = (x2-x1+1), w = (y2-y1+1);
		int a = h*w;
		sum += a;
		for (auto i : divisor) 
		{
			int x = odd(x1, x2, i), y = even(y1, y2, i);
			int bl = x*y + (h-x) * (w-y);
			cb[i] += bl;
			cw[i] += a - bl;
		}
	}
	// for (auto i : divisor) 
	// {
	// 	cout<<i<<" "<<cb[i]<<" "<<cw[i]<<endl;
	// }
	int ans = inf;
	for (auto i : divisor) 
	{
		int j = n/i;
		int b = (j*j + 1) / 2, w = j*j - b;
		ans = min({ans, sum - cw[i] + b*i*i - cw[i], sum - cb[i] + w*i*i - cb[i]});
	}
	cout<<ans;
}

/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/

signed main()
{
	bruh
	//freopen("input.inp","r",stdin);
	//freopen("output.inp","w",stdout);
	int t = 1;
	// cin>>t;
	while (t--)
	{
		solve();
		cout<<"\n";
	}
}
#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...