#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int N = 1e5 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
pair <int, int> a[N], b[N];
long long res[2];
int par[2 * N];
int lim[2 * N];
int n,k;
int getc(int i, int j, int l, int val) {
return ((par[i] ^ par[j]) & 1) ^ val;
}
int getv(int i, int j, int l, int val) {
return (getc(i, j, l, val) > 0) ? -1 : 1;
}
long long calc(int x, int y, int u, int v, int l, int val) {
if (x > u || y > v)
return 0;
long long res = 0;
int dx = (u - x + 1);
int dy = (v - y + 1);
int base = getv(x, y, l, val);
if (dx > dy)
swap(dx, dy);
if (dx < l) {
if (dy < l)
res = (long long) dx * dy;
else {
if (par[dy] & 1)
res = (long long) dx * l;
}
} else {
if (par[dx] & par[dy] & 1)
res = (long long) l * l;
}
if (base < 0)
res = -res;
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef ONLINE_JUDGE
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
#endif
cin >> n >> k;
for (int i = 1; i <= k; i++) {
cin >> a[i].fi >> a[i].se >> b[i].fi >> b[i].se;
a[i].fi--; a[i].se--;
b[i].fi--; b[i].se--;
}
long long ans = ooo;
for (int l = 1; l < n; l++) if (n % l == 0) {
for (int i = 0; i < 2 * n; i++) {
par[i] = (i / l);
lim[i] = par[i] * l;
}
int d = (n / l);
for (int i = 0; i < 2; i++)
res[i] = ((1LL * d * d + i) >> 1) * l * l;
for (int id = 1; id <= k; id++) {
int x = a[id].fi;
int y = a[id].se;
int u = b[id].fi;
int v = b[id].se;
int i = lim[x + l - 1];
int j = lim[y + l - 1];
int s = lim[u + 1] - 1;
int t = lim[v + 1] - 1;
vector <pair <int, int>> r, c;
if (par[x] == par[u])
r.push_back(mp(x, u));
else {
r.push_back(mp(x, i - 1));
if (i <= s)
r.push_back(mp(i, s));
r.push_back(mp(s + 1, u));
}
if (par[y] == par[v])
c.push_back(mp(y, v));
else {
c.push_back(mp(y, j - 1));
if (j <= t)
c.push_back(mp(j, t));
c.push_back(mp(t + 1, v));
}
for (int val = 0; val < 2; val++)
for (pair <int, int> a : r)
for (pair <int, int> b : c)
res[val] += calc(a.fi, b.fi, a.se, b.se, l, val);
}
mini(ans, min(res[0], res[1]));
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
2240 KB |
Output is correct |
2 |
Correct |
7 ms |
788 KB |
Output is correct |
3 |
Correct |
16 ms |
2216 KB |
Output is correct |
4 |
Correct |
16 ms |
1100 KB |
Output is correct |
5 |
Correct |
23 ms |
2008 KB |
Output is correct |
6 |
Correct |
13 ms |
1648 KB |
Output is correct |
7 |
Correct |
4 ms |
812 KB |
Output is correct |
8 |
Correct |
14 ms |
1616 KB |
Output is correct |
9 |
Correct |
34 ms |
2188 KB |
Output is correct |
10 |
Correct |
18 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
13 ms |
792 KB |
Output is correct |
17 |
Correct |
25 ms |
1632 KB |
Output is correct |
18 |
Correct |
41 ms |
1872 KB |
Output is correct |
19 |
Correct |
179 ms |
1724 KB |
Output is correct |
20 |
Correct |
182 ms |
1832 KB |
Output is correct |
21 |
Correct |
24 ms |
1604 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
29 ms |
1024 KB |
Output is correct |
24 |
Correct |
37 ms |
1748 KB |
Output is correct |
25 |
Correct |
6 ms |
468 KB |
Output is correct |
26 |
Correct |
27 ms |
1244 KB |
Output is correct |
27 |
Correct |
37 ms |
1396 KB |
Output is correct |
28 |
Correct |
39 ms |
1792 KB |
Output is correct |
29 |
Correct |
12 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
2240 KB |
Output is correct |
2 |
Correct |
7 ms |
788 KB |
Output is correct |
3 |
Correct |
16 ms |
2216 KB |
Output is correct |
4 |
Correct |
16 ms |
1100 KB |
Output is correct |
5 |
Correct |
23 ms |
2008 KB |
Output is correct |
6 |
Correct |
13 ms |
1648 KB |
Output is correct |
7 |
Correct |
4 ms |
812 KB |
Output is correct |
8 |
Correct |
14 ms |
1616 KB |
Output is correct |
9 |
Correct |
34 ms |
2188 KB |
Output is correct |
10 |
Correct |
18 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
13 ms |
792 KB |
Output is correct |
27 |
Correct |
25 ms |
1632 KB |
Output is correct |
28 |
Correct |
41 ms |
1872 KB |
Output is correct |
29 |
Correct |
179 ms |
1724 KB |
Output is correct |
30 |
Correct |
182 ms |
1832 KB |
Output is correct |
31 |
Correct |
24 ms |
1604 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
29 ms |
1024 KB |
Output is correct |
34 |
Correct |
37 ms |
1748 KB |
Output is correct |
35 |
Correct |
6 ms |
468 KB |
Output is correct |
36 |
Correct |
27 ms |
1244 KB |
Output is correct |
37 |
Correct |
37 ms |
1396 KB |
Output is correct |
38 |
Correct |
39 ms |
1792 KB |
Output is correct |
39 |
Correct |
12 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
192 ms |
3172 KB |
Output is correct |
42 |
Correct |
53 ms |
3348 KB |
Output is correct |
43 |
Correct |
119 ms |
3180 KB |
Output is correct |
44 |
Correct |
51 ms |
3212 KB |
Output is correct |
45 |
Correct |
40 ms |
3404 KB |
Output is correct |
46 |
Correct |
207 ms |
3336 KB |
Output is correct |
47 |
Correct |
32 ms |
3156 KB |
Output is correct |
48 |
Correct |
77 ms |
3240 KB |
Output is correct |
49 |
Correct |
50 ms |
3192 KB |
Output is correct |
50 |
Correct |
949 ms |
3044 KB |
Output is correct |
51 |
Correct |
1012 ms |
3132 KB |
Output is correct |
52 |
Correct |
1018 ms |
3028 KB |
Output is correct |
53 |
Correct |
1078 ms |
3128 KB |
Output is correct |
54 |
Correct |
1024 ms |
3008 KB |
Output is correct |
55 |
Correct |
1115 ms |
3180 KB |
Output is correct |
56 |
Correct |
965 ms |
2972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
24 ms |
2240 KB |
Output is correct |
10 |
Correct |
7 ms |
788 KB |
Output is correct |
11 |
Correct |
16 ms |
2216 KB |
Output is correct |
12 |
Correct |
16 ms |
1100 KB |
Output is correct |
13 |
Correct |
23 ms |
2008 KB |
Output is correct |
14 |
Correct |
13 ms |
1648 KB |
Output is correct |
15 |
Correct |
4 ms |
812 KB |
Output is correct |
16 |
Correct |
14 ms |
1616 KB |
Output is correct |
17 |
Correct |
34 ms |
2188 KB |
Output is correct |
18 |
Correct |
18 ms |
1620 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
13 ms |
792 KB |
Output is correct |
35 |
Correct |
25 ms |
1632 KB |
Output is correct |
36 |
Correct |
41 ms |
1872 KB |
Output is correct |
37 |
Correct |
179 ms |
1724 KB |
Output is correct |
38 |
Correct |
182 ms |
1832 KB |
Output is correct |
39 |
Correct |
24 ms |
1604 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
29 ms |
1024 KB |
Output is correct |
42 |
Correct |
37 ms |
1748 KB |
Output is correct |
43 |
Correct |
6 ms |
468 KB |
Output is correct |
44 |
Correct |
27 ms |
1244 KB |
Output is correct |
45 |
Correct |
37 ms |
1396 KB |
Output is correct |
46 |
Correct |
39 ms |
1792 KB |
Output is correct |
47 |
Correct |
12 ms |
852 KB |
Output is correct |
48 |
Correct |
2 ms |
340 KB |
Output is correct |
49 |
Correct |
192 ms |
3172 KB |
Output is correct |
50 |
Correct |
53 ms |
3348 KB |
Output is correct |
51 |
Correct |
119 ms |
3180 KB |
Output is correct |
52 |
Correct |
51 ms |
3212 KB |
Output is correct |
53 |
Correct |
40 ms |
3404 KB |
Output is correct |
54 |
Correct |
207 ms |
3336 KB |
Output is correct |
55 |
Correct |
32 ms |
3156 KB |
Output is correct |
56 |
Correct |
77 ms |
3240 KB |
Output is correct |
57 |
Correct |
50 ms |
3192 KB |
Output is correct |
58 |
Correct |
949 ms |
3044 KB |
Output is correct |
59 |
Correct |
1012 ms |
3132 KB |
Output is correct |
60 |
Correct |
1018 ms |
3028 KB |
Output is correct |
61 |
Correct |
1078 ms |
3128 KB |
Output is correct |
62 |
Correct |
1024 ms |
3008 KB |
Output is correct |
63 |
Correct |
1115 ms |
3180 KB |
Output is correct |
64 |
Correct |
965 ms |
2972 KB |
Output is correct |
65 |
Correct |
0 ms |
308 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1451 ms |
3080 KB |
Output is correct |
68 |
Correct |
1254 ms |
3148 KB |
Output is correct |
69 |
Correct |
1124 ms |
2960 KB |
Output is correct |
70 |
Correct |
1133 ms |
3096 KB |
Output is correct |
71 |
Correct |
1138 ms |
3080 KB |
Output is correct |
72 |
Correct |
1110 ms |
3048 KB |
Output is correct |
73 |
Correct |
1163 ms |
3024 KB |
Output is correct |
74 |
Correct |
1254 ms |
3144 KB |
Output is correct |
75 |
Correct |
1147 ms |
3064 KB |
Output is correct |
76 |
Correct |
1247 ms |
3152 KB |
Output is correct |
77 |
Correct |
205 ms |
3724 KB |
Output is correct |
78 |
Correct |
61 ms |
3480 KB |
Output is correct |
79 |
Correct |
142 ms |
3408 KB |
Output is correct |
80 |
Correct |
144 ms |
3504 KB |
Output is correct |
81 |
Correct |
140 ms |
3356 KB |
Output is correct |
82 |
Correct |
110 ms |
3532 KB |
Output is correct |
83 |
Correct |
88 ms |
3424 KB |
Output is correct |
84 |
Correct |
756 ms |
3588 KB |
Output is correct |
85 |
Correct |
1324 ms |
3376 KB |
Output is correct |
86 |
Correct |
131 ms |
1620 KB |
Output is correct |
87 |
Correct |
126 ms |
1628 KB |
Output is correct |
88 |
Correct |
1310 ms |
5588 KB |
Output is correct |
89 |
Correct |
373 ms |
2364 KB |
Output is correct |
90 |
Correct |
127 ms |
1620 KB |
Output is correct |