/*
Sviatoslav Bidzilia 2019
CF: anakib1
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <chrono>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <deque>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdarg.h>
#include <utility>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unisgned long long
#define ld long double
#define all(a) a.begin(), a.end()
#define SORT(a) sort(all(a))
#define pii pair<int, int>
#define tii triple<int, int, int>
#define e 1e-7
#define PI acos(-1)
#define inf 1e17
#define vi vector<int>
#define F first
#define S second
#define rng(x) for(int _ = 0; _ < (x); _++)
#define rngi(i, x) for(int i = 0; i < (x); i++)
#define rngsi(s, i, x) for(int i = (s); i <(x); i++)
#define int long long
template<typename A, typename B, typename C>
struct triple
{
A X;
B Y;
C Z;
triple(A a = 0, B b = 0, C c = 0) :X(a), Y(b), Z(c) {}
};
template<typename A, typename B, typename C>
triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0)
{
return triple<A, B, C>(a, b, c);
}
template<typename A, typename B, typename C>
bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b)
{
if (a.X != b.X)
return a.X < b.X;
if (a.Y != b.Y)
return a.Y < b.Y;
return a.Z < b.Z;
}
template<typename T, typename SS>
ostream& operator<<(ostream& ofs, const pair<T, SS>& p)
{
ofs << "( " << p.F << " , " << p.S << " )";
return ofs;
}
template<typename T>
void print(T a)
{
for (auto i : a)
cout << i << ' ';
cout << '\n';
}
template<typename T>
T max(T a, T b, T c)
{
return max(a, max(b, c));
}
template<typename T>
T min(T a, T b, T c)
{
return min(a, min(b, c));
}
struct custom_hash
{
static uint64_t splitmix64(uint64_t x)
{
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const
{
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int sz, flx, fly, d, s, d1, d2;
int n;
int m;
#define mxn (int)(1e5 + 11)
int X1[mxn], Y1[mxn], X2[mxn], Y2[mxn];
inline int sum1(int x, int y, int k)
{
y--, x--;
sz = k * k;
flx = (x + 1) / k;
fly = (y + 1) / k;
int ans = fly / 2 * sz * flx;
if (fly & 1)
{
ans += flx / 2 * sz;
if (flx & 1)
ans += sz;
}
if ((x + 1) % k)
{
d = (x + 1) % k;
s = (flx + 1) & 1;
ans += fly / 2 * k * d;
if (fly & 1 && s)
ans += k * d;
}
if ((y + 1) % k != 0)
{
d = (y + 1) % k;
s = (fly + 1) & 1;
ans += flx / 2 * k * d;
if (flx & 1 && s)
ans += k * d;
}
if ((x + 1) % k && (y + 1) % k)
{
d1 = (x + 1) % k;
d2 = (y + 1) % k;
s = (fly + flx) & 1;
s ^= 1;
if (s)
ans += d1 * d2;
}
return ans;
}
inline int sum(int x, int y, int t, int k)
{
if (t)
return sum1(x, y, k);
else
return x * y - sum1(x, y, k);
}
inline int sumr(int x1, int y1, int x2, int y2, int t, int k)
{
if (x1 == x2 && y1 == y2)
{
int x = x1 / k - 1;
int y = y1 / k - 1;
if (x1 % k)
x++;
if (y1 % k)
y++;
if (!t)
return (x + y) & 1;
else
return (x + y + 1) & 1;
}
return sum(x2, y2, t, k) - (x1 > 1 ? sum(x1 - 1, y2, t, k) : 0) - (y1 > 1 ? sum(x2, y1 - 1, t, k) : 0) + (x1 > 1 && y1 > 1 ? sum(x1 - 1, y1 - 1, t, k) : 0);
}
inline int solve(int k, int t)
{
int ans = 0;
int cntb = 0;
int cntw = 0;
for(int i =0; i < m; i++)
{
int r = sumr(X1[i], Y1[i], X2[i], Y2[i], t, k);
cntb += r;
cntw += (Y2[i] - Y1[i] + 1) * (X2[i] - X1[i] + 1) - r;
}
int allb = sumr(1, 1, n, n, t, k);
cntw += allb - cntb;
return cntw;
}
signed main()
{
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
srand(time(NULL));
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
/*rng(20)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << sumr(a, b, c, d, 0, 3) << '\n';
}*/
scanf("%d%d", &n, &m);
for (int i =0; i <m ;i++)
scanf("%d%d%d%d", &Y1[i], &X1[i], &Y2[i], &X2[i]);
int ans = min(solve(1, 0), solve(1,1));
for (int d = 2; d * d <= n; d++)
if (n % d == 0)
{
ans = min(ans, solve(d, 0), solve(d, 1));
ans = min(ans, solve(n/d, 0), solve(n/d, 1));
}
cout << ans << '\n';
}
Compilation message
chessboard.cpp: In function 'long long int solve(long long int, long long int)':
chessboard.cpp:187:6: warning: unused variable 'ans' [-Wunused-variable]
int ans = 0;
^~~
chessboard.cpp: In function 'int main()':
chessboard.cpp:214:22: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
scanf("%d%d", &n, &m);
~~ ^
chessboard.cpp:214:22: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
chessboard.cpp:216:51: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
scanf("%d%d%d%d", &Y1[i], &X1[i], &Y2[i], &X2[i]);
~~~~~~ ^
chessboard.cpp:216:51: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
chessboard.cpp:216:51: warning: format '%d' expects argument of type 'int*', but argument 4 has type 'long long int*' [-Wformat=]
chessboard.cpp:216:51: warning: format '%d' expects argument of type 'int*', but argument 5 has type 'long long int*' [-Wformat=]
chessboard.cpp:214:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
chessboard.cpp:216:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &Y1[i], &X1[i], &Y2[i], &X2[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
2424 KB |
Output is correct |
2 |
Correct |
11 ms |
860 KB |
Output is correct |
3 |
Correct |
23 ms |
1628 KB |
Output is correct |
4 |
Correct |
25 ms |
1912 KB |
Output is correct |
5 |
Correct |
31 ms |
2040 KB |
Output is correct |
6 |
Correct |
21 ms |
1388 KB |
Output is correct |
7 |
Correct |
6 ms |
632 KB |
Output is correct |
8 |
Correct |
20 ms |
1404 KB |
Output is correct |
9 |
Correct |
50 ms |
3196 KB |
Output is correct |
10 |
Correct |
29 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
18 ms |
1272 KB |
Output is correct |
17 |
Correct |
41 ms |
2920 KB |
Output is correct |
18 |
Correct |
59 ms |
3704 KB |
Output is correct |
19 |
Correct |
169 ms |
3200 KB |
Output is correct |
20 |
Correct |
190 ms |
3448 KB |
Output is correct |
21 |
Correct |
40 ms |
2936 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
35 ms |
1656 KB |
Output is correct |
24 |
Correct |
53 ms |
3212 KB |
Output is correct |
25 |
Correct |
9 ms |
632 KB |
Output is correct |
26 |
Correct |
35 ms |
2168 KB |
Output is correct |
27 |
Correct |
48 ms |
2424 KB |
Output is correct |
28 |
Correct |
56 ms |
3292 KB |
Output is correct |
29 |
Correct |
17 ms |
1400 KB |
Output is correct |
30 |
Correct |
4 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
2424 KB |
Output is correct |
2 |
Correct |
11 ms |
860 KB |
Output is correct |
3 |
Correct |
23 ms |
1628 KB |
Output is correct |
4 |
Correct |
25 ms |
1912 KB |
Output is correct |
5 |
Correct |
31 ms |
2040 KB |
Output is correct |
6 |
Correct |
21 ms |
1388 KB |
Output is correct |
7 |
Correct |
6 ms |
632 KB |
Output is correct |
8 |
Correct |
20 ms |
1404 KB |
Output is correct |
9 |
Correct |
50 ms |
3196 KB |
Output is correct |
10 |
Correct |
29 ms |
1912 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
18 ms |
1272 KB |
Output is correct |
27 |
Correct |
41 ms |
2920 KB |
Output is correct |
28 |
Correct |
59 ms |
3704 KB |
Output is correct |
29 |
Correct |
169 ms |
3200 KB |
Output is correct |
30 |
Correct |
190 ms |
3448 KB |
Output is correct |
31 |
Correct |
40 ms |
2936 KB |
Output is correct |
32 |
Correct |
3 ms |
376 KB |
Output is correct |
33 |
Correct |
35 ms |
1656 KB |
Output is correct |
34 |
Correct |
53 ms |
3212 KB |
Output is correct |
35 |
Correct |
9 ms |
632 KB |
Output is correct |
36 |
Correct |
35 ms |
2168 KB |
Output is correct |
37 |
Correct |
48 ms |
2424 KB |
Output is correct |
38 |
Correct |
56 ms |
3292 KB |
Output is correct |
39 |
Correct |
17 ms |
1400 KB |
Output is correct |
40 |
Correct |
4 ms |
504 KB |
Output is correct |
41 |
Correct |
146 ms |
2936 KB |
Output is correct |
42 |
Correct |
63 ms |
3308 KB |
Output is correct |
43 |
Correct |
93 ms |
3068 KB |
Output is correct |
44 |
Correct |
61 ms |
3192 KB |
Output is correct |
45 |
Correct |
55 ms |
3472 KB |
Output is correct |
46 |
Correct |
163 ms |
3192 KB |
Output is correct |
47 |
Correct |
48 ms |
3064 KB |
Output is correct |
48 |
Correct |
77 ms |
3064 KB |
Output is correct |
49 |
Correct |
57 ms |
2976 KB |
Output is correct |
50 |
Correct |
675 ms |
3240 KB |
Output is correct |
51 |
Correct |
719 ms |
3416 KB |
Output is correct |
52 |
Correct |
670 ms |
3320 KB |
Output is correct |
53 |
Correct |
714 ms |
3420 KB |
Output is correct |
54 |
Correct |
660 ms |
3192 KB |
Output is correct |
55 |
Correct |
742 ms |
3576 KB |
Output is correct |
56 |
Correct |
642 ms |
3192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
35 ms |
2424 KB |
Output is correct |
10 |
Correct |
11 ms |
860 KB |
Output is correct |
11 |
Correct |
23 ms |
1628 KB |
Output is correct |
12 |
Correct |
25 ms |
1912 KB |
Output is correct |
13 |
Correct |
31 ms |
2040 KB |
Output is correct |
14 |
Correct |
21 ms |
1388 KB |
Output is correct |
15 |
Correct |
6 ms |
632 KB |
Output is correct |
16 |
Correct |
20 ms |
1404 KB |
Output is correct |
17 |
Correct |
50 ms |
3196 KB |
Output is correct |
18 |
Correct |
29 ms |
1912 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
3 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
376 KB |
Output is correct |
33 |
Correct |
3 ms |
376 KB |
Output is correct |
34 |
Correct |
18 ms |
1272 KB |
Output is correct |
35 |
Correct |
41 ms |
2920 KB |
Output is correct |
36 |
Correct |
59 ms |
3704 KB |
Output is correct |
37 |
Correct |
169 ms |
3200 KB |
Output is correct |
38 |
Correct |
190 ms |
3448 KB |
Output is correct |
39 |
Correct |
40 ms |
2936 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
35 ms |
1656 KB |
Output is correct |
42 |
Correct |
53 ms |
3212 KB |
Output is correct |
43 |
Correct |
9 ms |
632 KB |
Output is correct |
44 |
Correct |
35 ms |
2168 KB |
Output is correct |
45 |
Correct |
48 ms |
2424 KB |
Output is correct |
46 |
Correct |
56 ms |
3292 KB |
Output is correct |
47 |
Correct |
17 ms |
1400 KB |
Output is correct |
48 |
Correct |
4 ms |
504 KB |
Output is correct |
49 |
Correct |
146 ms |
2936 KB |
Output is correct |
50 |
Correct |
63 ms |
3308 KB |
Output is correct |
51 |
Correct |
93 ms |
3068 KB |
Output is correct |
52 |
Correct |
61 ms |
3192 KB |
Output is correct |
53 |
Correct |
55 ms |
3472 KB |
Output is correct |
54 |
Correct |
163 ms |
3192 KB |
Output is correct |
55 |
Correct |
48 ms |
3064 KB |
Output is correct |
56 |
Correct |
77 ms |
3064 KB |
Output is correct |
57 |
Correct |
57 ms |
2976 KB |
Output is correct |
58 |
Correct |
675 ms |
3240 KB |
Output is correct |
59 |
Correct |
719 ms |
3416 KB |
Output is correct |
60 |
Correct |
670 ms |
3320 KB |
Output is correct |
61 |
Correct |
714 ms |
3420 KB |
Output is correct |
62 |
Correct |
660 ms |
3192 KB |
Output is correct |
63 |
Correct |
742 ms |
3576 KB |
Output is correct |
64 |
Correct |
642 ms |
3192 KB |
Output is correct |
65 |
Correct |
2 ms |
376 KB |
Output is correct |
66 |
Correct |
2 ms |
376 KB |
Output is correct |
67 |
Execution timed out |
2054 ms |
3324 KB |
Time limit exceeded |
68 |
Halted |
0 ms |
0 KB |
- |