/***Farhan132***/
// #pragma GCC optimize("Ofast,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll, ll, ll > tp;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clz(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
const ll mod = (ll) 998244353;
// const ll mod = (ll) 1e9 + 7;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)2e18;
// ll dx[]={0,1,0,-1};
// ll dy[]={1,0,-1,0};
// ll dxx[]={0,1,0,-1,1,1,-1,-1};
// ll dyy[]={1,0,-1,0,1,-1,1,-1};
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 2005;
ii add(ii a, ii b){
return {a.ff + b.ff, a.ss + b.ss};
}
ii sub(ii a, ii b){
return {a.ff - b.ff, a.ss - b.ss};
}
ii f(ll col, ll h, ll sz, ll n){
ll nor = n / (2 * sz);
ii ans = {nor * sz, nor * sz};
n %= (2 * sz);
if(n > sz){
ans.ff += sz;
ans.ss += n - sz;
}else{
ans.ff += n;
}
ans.ff *= h;
ans.ss *= h;
if(!col) swap(ans.ff, ans.ss);
return ans;
}
ii corner(ll x, ll y, ll sz){
if(min(x, y) < 1) return {0, 0};
ll p1 = x / sz;
ll p2 = y / sz;
ll unit = sz * sz;
ii ans = {unit * ((p1 * p2 + 1) / 2), unit * ((p1 * p2) / 2)};
ans = add(ans, f(1 - p1%2, x % sz, sz, y));
ans = add(ans, f(1 - p2%2, y % sz, sz, p1 * sz));
return ans;
}
ii F(ll r1, ll c1, ll r2, ll c2, ll sz){
ii ans = corner(r2, c2, sz);
ans = sub(ans, corner(r2, c1 - 1, sz));
ans = sub(ans, corner(r1 - 1, c2, sz));
ans = add(ans, corner(r1 - 1, c1 - 1, sz));
return ans;
}
void solve(){
ll n; cin >> n;
vector < tuple < ll , ll , ll , ll > > Q;
ll q; cin >> q;
for(ll i = 0; i < q; i++){
ll r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
Q.push_back({r1, c1, r2, c2});
}
ll ans = n * n;
for(ll sz = 2; sz < 3; sz++){
if(n % sz != 0) continue;
ii cnt = {0, 0};
for(auto [r1, c1, r2, c2] : Q){
cnt = add(cnt, F(r1, c1, r2, c2, sz));
}
ii overall = corner(n, n, sz);
ans = min({ans, cnt.ss + overall.ff - cnt.ff, cnt.ff + overall.ss - cnt.ss});
}
cout << ans << '\n';
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
auto start_time = clock();
#else
// freopen("subsequence.in", "r", stdin);
// freopen("subsequence.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//precalc();
ll T = 1, CT = 0; //cin >> T;
while(T--){
// cout << "Case #" << ++CT << ": ";
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
#endif
return 0;
}
Compilation message
chessboard.cpp: In function 'int main()':
chessboard.cpp:130:15: warning: unused variable 'CT' [-Wunused-variable]
130 | ll T = 1, CT = 0; //cin >> T;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
3800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
3800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |