Submission #378738

# Submission time Handle Problem Language Result Execution time Memory
378738 2021-03-17T03:38:36 Z abc864197532 Chessboard (IZhO18_chessboard) C++17
47 / 100
2000 ms 224832 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int, int>
#define pll pair<lli, lli>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define test(x) cout << #x << ' ' << x << endl
#define printv(x) {\
    for (auto a : x) cout << x << ' ';\
    cout << endl;\
}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 2000000;

struct range {
    int l, r, v;
};

struct Seg {
    int l, r, m, tag, sum, lz;
    Seg* ch[2];
    Seg (int _l, int _r, int k) : l(_l), r(_r), m(l + r >> 1), sum(0), lz(0) {
        if (r - l > 1) {
            ch[0] = new Seg(l, m, k);
            ch[1] = new Seg(m, r, k);
            tag = ch[0]->tag + ch[1]->tag;
        } else {
            tag = (l / k) & 1 ? -1 : 1;
        }
    }
    void pull() {
        if (lz) sum = tag;
        else if (r - l == 1) sum = 0;
        else sum = ch[0]->sum + ch[1]->sum;
    }
    void modify(int a, int b, int v) {
        if (a <= l && r <= b) lz += v;
        else {
            if (a < m) ch[0]->modify(a, b, v);
            if (m < b) ch[1]->modify(a, b, v);
        }
        pull();
    }
};

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector <int> p;
    for (int i = 1; i < n; ++i) if (n % i == 0) p.pb(i);
    vector <range> block[n + 1];
    for (int i = 0, x1, y1, x2, y2; i < m; ++i) {
        cin >> x1 >> y1 >> x2 >> y2, --x1, --y1;
        block[x1].pb({y1, y2, 1});
        block[x2].pb({y1, y2, -1});
    }
    lli ans = 1ll * n * n;
    for (int k : p) {
        lli cur = 1ll * k * k * (1ll * (n / k) * (n / k) / 2);
        Seg root(0, n, k);
        for (int i = 0; i <= n; ++i) {
            for (range &j : block[i]) {
                root.modify(j.l, j.r, j.v);
            }
            if ((i / k) & 1) {
                cur -= root.sum;
            } else {
                cur += root.sum;
            }
        }
        ans = min({ans, cur, 1ll * n * n - cur});
    }
    cout << ans << endl;
}

/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
 */

Compilation message

chessboard.cpp: In constructor 'Seg::Seg(int, int, int)':
chessboard.cpp:27:53: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   27 |     Seg (int _l, int _r, int k) : l(_l), r(_r), m(l + r >> 1), sum(0), lz(0) {
      |                                                   ~~^~~
chessboard.cpp: In function 'int main()':
chessboard.cpp:39:37: warning: 'root.Seg::ch[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |         else sum = ch[0]->sum + ch[1]->sum;
      |                                 ~~~~^
chessboard.cpp:67:13: note: 'root.Seg::ch[1]' was declared here
   67 |         Seg root(0, n, k);
      |             ^~~~
chessboard.cpp:39:37: warning: 'root.Seg::ch[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |         else sum = ch[0]->sum + ch[1]->sum;
      |                                 ~~~~^
chessboard.cpp:67:13: note: 'root.Seg::ch[0]' was declared here
   67 |         Seg root(0, n, k);
      |             ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 10220 KB Output is correct
2 Correct 17 ms 2540 KB Output is correct
3 Correct 65 ms 11884 KB Output is correct
4 Correct 35 ms 2284 KB Output is correct
5 Correct 73 ms 9196 KB Output is correct
6 Correct 45 ms 7404 KB Output is correct
7 Correct 12 ms 3308 KB Output is correct
8 Correct 50 ms 7660 KB Output is correct
9 Correct 128 ms 7276 KB Output is correct
10 Correct 64 ms 6892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 31 ms 1516 KB Output is correct
17 Correct 54 ms 3692 KB Output is correct
18 Correct 113 ms 3780 KB Output is correct
19 Correct 619 ms 5752 KB Output is correct
20 Correct 692 ms 6076 KB Output is correct
21 Correct 50 ms 3692 KB Output is correct
22 Correct 4 ms 1772 KB Output is correct
23 Correct 90 ms 2540 KB Output is correct
24 Correct 103 ms 3692 KB Output is correct
25 Correct 19 ms 1260 KB Output is correct
26 Correct 63 ms 2284 KB Output is correct
27 Correct 111 ms 3564 KB Output is correct
28 Correct 106 ms 3564 KB Output is correct
29 Correct 22 ms 1772 KB Output is correct
30 Correct 4 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 10220 KB Output is correct
2 Correct 17 ms 2540 KB Output is correct
3 Correct 65 ms 11884 KB Output is correct
4 Correct 35 ms 2284 KB Output is correct
5 Correct 73 ms 9196 KB Output is correct
6 Correct 45 ms 7404 KB Output is correct
7 Correct 12 ms 3308 KB Output is correct
8 Correct 50 ms 7660 KB Output is correct
9 Correct 128 ms 7276 KB Output is correct
10 Correct 64 ms 6892 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 31 ms 1516 KB Output is correct
27 Correct 54 ms 3692 KB Output is correct
28 Correct 113 ms 3780 KB Output is correct
29 Correct 619 ms 5752 KB Output is correct
30 Correct 692 ms 6076 KB Output is correct
31 Correct 50 ms 3692 KB Output is correct
32 Correct 4 ms 1772 KB Output is correct
33 Correct 90 ms 2540 KB Output is correct
34 Correct 103 ms 3692 KB Output is correct
35 Correct 19 ms 1260 KB Output is correct
36 Correct 63 ms 2284 KB Output is correct
37 Correct 111 ms 3564 KB Output is correct
38 Correct 106 ms 3564 KB Output is correct
39 Correct 22 ms 1772 KB Output is correct
40 Correct 4 ms 748 KB Output is correct
41 Correct 1856 ms 221600 KB Output is correct
42 Correct 332 ms 34672 KB Output is correct
43 Correct 916 ms 109292 KB Output is correct
44 Correct 303 ms 34540 KB Output is correct
45 Correct 149 ms 15980 KB Output is correct
46 Correct 1999 ms 222328 KB Output is correct
47 Correct 132 ms 15596 KB Output is correct
48 Correct 634 ms 71916 KB Output is correct
49 Correct 282 ms 34284 KB Output is correct
50 Execution timed out 2108 ms 224832 KB Time limit exceeded
51 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 86 ms 10220 KB Output is correct
10 Correct 17 ms 2540 KB Output is correct
11 Correct 65 ms 11884 KB Output is correct
12 Correct 35 ms 2284 KB Output is correct
13 Correct 73 ms 9196 KB Output is correct
14 Correct 45 ms 7404 KB Output is correct
15 Correct 12 ms 3308 KB Output is correct
16 Correct 50 ms 7660 KB Output is correct
17 Correct 128 ms 7276 KB Output is correct
18 Correct 64 ms 6892 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 31 ms 1516 KB Output is correct
35 Correct 54 ms 3692 KB Output is correct
36 Correct 113 ms 3780 KB Output is correct
37 Correct 619 ms 5752 KB Output is correct
38 Correct 692 ms 6076 KB Output is correct
39 Correct 50 ms 3692 KB Output is correct
40 Correct 4 ms 1772 KB Output is correct
41 Correct 90 ms 2540 KB Output is correct
42 Correct 103 ms 3692 KB Output is correct
43 Correct 19 ms 1260 KB Output is correct
44 Correct 63 ms 2284 KB Output is correct
45 Correct 111 ms 3564 KB Output is correct
46 Correct 106 ms 3564 KB Output is correct
47 Correct 22 ms 1772 KB Output is correct
48 Correct 4 ms 748 KB Output is correct
49 Correct 1856 ms 221600 KB Output is correct
50 Correct 332 ms 34672 KB Output is correct
51 Correct 916 ms 109292 KB Output is correct
52 Correct 303 ms 34540 KB Output is correct
53 Correct 149 ms 15980 KB Output is correct
54 Correct 1999 ms 222328 KB Output is correct
55 Correct 132 ms 15596 KB Output is correct
56 Correct 634 ms 71916 KB Output is correct
57 Correct 282 ms 34284 KB Output is correct
58 Execution timed out 2108 ms 224832 KB Time limit exceeded
59 Halted 0 ms 0 KB -