//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
//#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 3;
const ll inf = 1e18;
int k, p[2][N], rx[N], ry[N], rx2[N], ry2[N];
ll n, ans = inf, A, Z, S, W, B, L, sq_A;
int color(int x, int y) {
int z = (x - 1) / A;
int w = (y - 1) / A;
return ((z + w + Z) & 1);
}
int num(int x) {
int z = (x - 1) / A;
return (z & 1);
}
ll getL() {
int x = n / A;
ll a = ((x + 1) >> 1LL) * sq_A;
ll b = (x >> 1LL) * sq_A;
ll res = ((x + 1) >> 1LL) * a + (x >> 1LL) * b;
if (Z == 0) {
return res;
} else {
return n * n - res;
}
}
ll getval(int x, int a, int b) {
if (x < 1) {
return 0;
}
ll res = 0;
int q = (x - 1) / A;
int last = q * A + 1;
if (num(last) == 0) {
res += (x - last + 1) * a;
} else {
res += (x - last + 1) * b;
}
res += (((q + 1) >> 1LL) * a + (q >> 1LL) * b) * A;
return res;
}
ll getans() {
for (int i = 1; i <= n; i++) {
p[0][i] = p[0][i - 1];
p[1][i] = p[1][i - 1];
if (color(i, 1)) {
p[0][i]++;
} else {
p[1][i]++;
}
}
ll res = 0;
for (int i = 0; i < k; i++) {
int v1 = p[0][rx2[i]] - p[0][rx[i] - 1];
int v2 = p[1][rx2[i]] - p[1][rx[i] - 1];
res += getval(ry2[i], v1, v2) - getval(ry[i] - 1, v1, v2);
}
return res;
}
void solve(int tc) {
cin >> n >> k;
for (int i = 0; i < k; i++) {
cin >> rx[i] >> ry[i] >> rx2[i] >> ry2[i];
S += (ll)(rx2[i] - rx[i] + 1) * (ry2[i] - ry[i] + 1);
}
vector <int> factors = {};
for (int i = 1; i < n; i++) {
if (n % i == 0) {
factors.pb(i);
}
}
for (auto i : factors) {
A = i;
sq_A = A * A;
for (int j = 0; j < 2; j++) {
Z = j;
L = getL();
W = getans();
B = L - (S - W);
ans = min(ans, W + B);
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 0; i < tc; i++) {
solve(i);
// cleanup();
}
}
# |
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 |
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 |
40 ms |
1784 KB |
Output is correct |
2 |
Correct |
12 ms |
696 KB |
Output is correct |
3 |
Correct |
28 ms |
1628 KB |
Output is correct |
4 |
Correct |
26 ms |
1172 KB |
Output is correct |
5 |
Correct |
35 ms |
1656 KB |
Output is correct |
6 |
Correct |
24 ms |
1272 KB |
Output is correct |
7 |
Correct |
8 ms |
632 KB |
Output is correct |
8 |
Correct |
24 ms |
1272 KB |
Output is correct |
9 |
Correct |
55 ms |
2040 KB |
Output is correct |
10 |
Correct |
33 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 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 |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 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 |
23 ms |
760 KB |
Output is correct |
17 |
Correct |
44 ms |
1628 KB |
Output is correct |
18 |
Correct |
81 ms |
1884 KB |
Output is correct |
19 |
Correct |
392 ms |
1812 KB |
Output is correct |
20 |
Correct |
435 ms |
1964 KB |
Output is correct |
21 |
Correct |
41 ms |
1756 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
61 ms |
1144 KB |
Output is correct |
24 |
Correct |
72 ms |
1784 KB |
Output is correct |
25 |
Correct |
13 ms |
504 KB |
Output is correct |
26 |
Correct |
46 ms |
1244 KB |
Output is correct |
27 |
Correct |
77 ms |
1400 KB |
Output is correct |
28 |
Correct |
78 ms |
1912 KB |
Output is correct |
29 |
Correct |
18 ms |
888 KB |
Output is correct |
30 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
1784 KB |
Output is correct |
2 |
Correct |
12 ms |
696 KB |
Output is correct |
3 |
Correct |
28 ms |
1628 KB |
Output is correct |
4 |
Correct |
26 ms |
1172 KB |
Output is correct |
5 |
Correct |
35 ms |
1656 KB |
Output is correct |
6 |
Correct |
24 ms |
1272 KB |
Output is correct |
7 |
Correct |
8 ms |
632 KB |
Output is correct |
8 |
Correct |
24 ms |
1272 KB |
Output is correct |
9 |
Correct |
55 ms |
2040 KB |
Output is correct |
10 |
Correct |
33 ms |
1528 KB |
Output is correct |
11 |
Correct |
3 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 |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
380 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 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 |
23 ms |
760 KB |
Output is correct |
27 |
Correct |
44 ms |
1628 KB |
Output is correct |
28 |
Correct |
81 ms |
1884 KB |
Output is correct |
29 |
Correct |
392 ms |
1812 KB |
Output is correct |
30 |
Correct |
435 ms |
1964 KB |
Output is correct |
31 |
Correct |
41 ms |
1756 KB |
Output is correct |
32 |
Correct |
3 ms |
376 KB |
Output is correct |
33 |
Correct |
61 ms |
1144 KB |
Output is correct |
34 |
Correct |
72 ms |
1784 KB |
Output is correct |
35 |
Correct |
13 ms |
504 KB |
Output is correct |
36 |
Correct |
46 ms |
1244 KB |
Output is correct |
37 |
Correct |
77 ms |
1400 KB |
Output is correct |
38 |
Correct |
78 ms |
1912 KB |
Output is correct |
39 |
Correct |
18 ms |
888 KB |
Output is correct |
40 |
Correct |
4 ms |
376 KB |
Output is correct |
41 |
Correct |
384 ms |
2400 KB |
Output is correct |
42 |
Correct |
92 ms |
2552 KB |
Output is correct |
43 |
Correct |
203 ms |
2548 KB |
Output is correct |
44 |
Correct |
91 ms |
2552 KB |
Output is correct |
45 |
Correct |
64 ms |
2668 KB |
Output is correct |
46 |
Correct |
423 ms |
2704 KB |
Output is correct |
47 |
Correct |
56 ms |
2552 KB |
Output is correct |
48 |
Correct |
149 ms |
2424 KB |
Output is correct |
49 |
Correct |
83 ms |
2424 KB |
Output is correct |
50 |
Execution timed out |
2035 ms |
2592 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
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 |
40 ms |
1784 KB |
Output is correct |
10 |
Correct |
12 ms |
696 KB |
Output is correct |
11 |
Correct |
28 ms |
1628 KB |
Output is correct |
12 |
Correct |
26 ms |
1172 KB |
Output is correct |
13 |
Correct |
35 ms |
1656 KB |
Output is correct |
14 |
Correct |
24 ms |
1272 KB |
Output is correct |
15 |
Correct |
8 ms |
632 KB |
Output is correct |
16 |
Correct |
24 ms |
1272 KB |
Output is correct |
17 |
Correct |
55 ms |
2040 KB |
Output is correct |
18 |
Correct |
33 ms |
1528 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
3 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 |
3 ms |
380 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
3 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 |
23 ms |
760 KB |
Output is correct |
35 |
Correct |
44 ms |
1628 KB |
Output is correct |
36 |
Correct |
81 ms |
1884 KB |
Output is correct |
37 |
Correct |
392 ms |
1812 KB |
Output is correct |
38 |
Correct |
435 ms |
1964 KB |
Output is correct |
39 |
Correct |
41 ms |
1756 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
61 ms |
1144 KB |
Output is correct |
42 |
Correct |
72 ms |
1784 KB |
Output is correct |
43 |
Correct |
13 ms |
504 KB |
Output is correct |
44 |
Correct |
46 ms |
1244 KB |
Output is correct |
45 |
Correct |
77 ms |
1400 KB |
Output is correct |
46 |
Correct |
78 ms |
1912 KB |
Output is correct |
47 |
Correct |
18 ms |
888 KB |
Output is correct |
48 |
Correct |
4 ms |
376 KB |
Output is correct |
49 |
Correct |
384 ms |
2400 KB |
Output is correct |
50 |
Correct |
92 ms |
2552 KB |
Output is correct |
51 |
Correct |
203 ms |
2548 KB |
Output is correct |
52 |
Correct |
91 ms |
2552 KB |
Output is correct |
53 |
Correct |
64 ms |
2668 KB |
Output is correct |
54 |
Correct |
423 ms |
2704 KB |
Output is correct |
55 |
Correct |
56 ms |
2552 KB |
Output is correct |
56 |
Correct |
149 ms |
2424 KB |
Output is correct |
57 |
Correct |
83 ms |
2424 KB |
Output is correct |
58 |
Execution timed out |
2035 ms |
2592 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |