Submission #1049090

# Submission time Handle Problem Language Result Execution time Memory
1049090 2024-08-08T13:18:12 Z LaviniaTornaghi Vision Program (IOI19_vision) C++14
100 / 100
9 ms 2136 KB
#include <bits/stdc++.h>
#include "vision.h"
using namespace std;

void construct_network(int H, int W, int K) {
	int total = H * W;
	int bits = ceil(log2(max(H, W)));

	auto add_not = [&](int x) {
		::add_not(x);
		return total++;
	};
	auto add_or = [&](vector<int> x) {
		::add_or(x);
		return total++;
	};
	auto add_xor = [&](vector<int> x) {
		::add_xor(x);
		return total++;
	};
	auto add_and = [&](vector<int> x) {
		::add_and(x);
		return total++;
	};

	auto add_adder = [&](int bits, vector<int> a, vector<int> b) {
		assert(a.size() == bits);
		assert(b.size() == bits);

		vector<int> out(bits);

		out[0] = add_xor({a[0], b[0]});
		int carry = add_and({a[0], b[0]});

		for (int i = 1; i < bits; i++) {
			out[i] = add_xor({a[i], b[i], carry});
			carry = add_or({add_and({a[i], b[i]}), add_and({a[i], carry}), add_and({b[i], carry})});
		}

		return out;
	};
	auto add_one = [&](int bits, vector<int> a) {
		assert(a.size() == bits);

		vector<int> out(bits);
		
		out[0] = add_not(a[0]);
		int carry = add_and({a[0]});

		for (int i = 1; i < bits; i++) {
			out[i] = add_xor({a[i], carry});
			carry = add_and({a[i], carry});
		}

		return out;
	};

	// Grid
	vector<vector<int>> grid(H, vector<int>(W));
	for (int i = 0; i < H; i++)
		for (int j = 0; j < W; j++)
			grid[i][j] = i * W + j;

	// Column OR
	vector<int> col(W);
	for (int i = 0; i < W; i++) {
		vector<int> cells;
		for (int j = 0; j < H; j++)
			cells.push_back(grid[j][i]);
		
		col[i] = add_or(cells);
	}

	// Column MASKS
	vector<vector<int>> col_xor(W, vector<int>(bits));
	vector<vector<int>> col_last(W, vector<int>(bits));
	vector<int> found_before_col(W);

	for (int i = 0; i < W; i++) {
		for (int b = 0; b < bits; b++) {
			if ((i >> b) & 1) {
				if (i == 0) {
					col_xor[i][b] = add_and({col[i]});
					col_last[i][b] = add_xor({0, 0}); // Always ZERO
				} else {
					col_xor[i][b] = add_xor({col_xor[i - 1][b], col[i]});
					int temp = add_and({col[i], found_before_col[i - 1]});
					col_last[i][b] = add_or({col_last[i - 1][b], temp});
				}
			} else {
				if (i == 0) {
					col_xor[i][b] = add_xor({0, 0}); // Always ZERO
					col_last[i][b] = add_xor({0, 0}); // Always ZERO
				} else {
					col_xor[i][b] = add_and({col_xor[i - 1][b]});
					col_last[i][b] = add_and({col_last[i - 1][b], found_before_col[i - 1]});
				}
			}
		}

		if (i == 0) {
			found_before_col[i] = add_and({col[i]});
		} else {
			found_before_col[i] = add_or({col[i], found_before_col[i - 1]});
		}
	}

	// Column DISTINCT
	int distinct_col;
	{
		vector<int> cells;
		for (int i = 0; i < W; i++)
			cells.push_back(col[i]);
		distinct_col = add_not(add_xor(cells));
	}

	// Column MATH
	vector<int> neg_col_first(bits);
	for (int i = 0; i < bits; i++) {
		neg_col_first[i] = add_not(add_xor({col_last[W - 1][i], col_xor[W - 1][i]}));
	}

	vector<int> col_pre_diff = add_adder(bits, col_last[W - 1], add_one(bits, neg_col_first));

	vector<int> col_diff(bits);
	for (int i = 0; i < bits; i++) {
		col_diff[i] = add_and({distinct_col, col_pre_diff[i]});
	}

	// Row OR
	vector<int> row(H);
	for (int i = 0; i < H; i++) {
		vector<int> cells;
		for (int j = 0; j < W; j++)
			cells.push_back(grid[i][j]);
		
		row[i] = add_or(cells);
	}

	// Row MASKS
	vector<vector<int>> row_xor(H, vector<int>(bits));
	vector<vector<int>> row_last(H, vector<int>(bits));
	vector<int> found_before_row(H);

	for (int i = 0; i < H; i++) {
		for (int b = 0; b < bits; b++) {
			if ((i >> b) & 1) {
				if (i == 0) {
					row_xor[i][b] = add_and({row[i]});
					row_last[i][b] = add_xor({0, 0}); // Always ZERO
				} else {
					row_xor[i][b] = add_xor({row_xor[i - 1][b], row[i]});
					int temp = add_and({row[i], found_before_row[i - 1]});
					row_last[i][b] = add_or({row_last[i - 1][b], temp});
				}
			} else {
				if (i == 0) {
					row_xor[i][b] = add_xor({0, 0}); // Always ZERO
					row_last[i][b] = add_xor({0, 0}); // Always ZERO
				} else {
					row_xor[i][b] = add_and({row_xor[i - 1][b]});
					row_last[i][b] = add_and({row_last[i - 1][b], found_before_row[i - 1]});
				}
			}
		}

		if (i == 0) {
			found_before_row[i] = add_and({row[i]});
		} else {
			found_before_row[i] = add_or({row[i], found_before_row[i - 1]});
		}
	}

	// Row DISTINCT
	int distinct_row;
	{
		vector<int> cells;
		for (int i = 0; i < H; i++)
			cells.push_back(row[i]);
		distinct_row = add_not(add_xor(cells));
	}

	// Row MATH
	vector<int> neg_row_first(bits);
	for (int i = 0; i < bits; i++) {
		neg_row_first[i] = add_not(add_xor({row_last[H - 1][i], row_xor[H - 1][i]}));
	}

	vector<int> row_pre_diff = add_adder(bits, row_last[H - 1], add_one(bits, neg_row_first));

	vector<int> row_diff(bits);
	for (int i = 0; i < bits; i++) {
		row_diff[i] = add_and({distinct_row, row_pre_diff[i]});
	}

	col_diff.push_back(add_xor({0, 0}));
	row_diff.push_back(add_xor({0, 0}));

	// Total distance
	vector<int> sum = add_adder(bits + 1, col_diff, row_diff);

	// Equality check
	vector<int> diffs(bits + 1);
	for (int i = 0; i <= bits; i++) {
		if ((K >> i) & 1) {
			diffs[i] = add_and({sum[i]});
		} else {
			diffs[i] = add_not(sum[i]);
		}
	}

	add_and(diffs);

	cerr << total - H * W << endl;
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from vision.cpp:1:
vision.cpp: In lambda function:
vision.cpp:27:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   27 |   assert(a.size() == bits);
      |          ~~~~~~~~~^~~~~~~
vision.cpp:28:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   28 |   assert(b.size() == bits);
      |          ~~~~~~~~~^~~~~~~
vision.cpp: In lambda function:
vision.cpp:43:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   43 |   assert(a.size() == bits);
      |          ~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 436 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 436 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 436 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 2 ms 860 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 856 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 2 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 728 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 2 ms 860 KB Output is correct
10 Correct 2 ms 860 KB Output is correct
11 Correct 2 ms 860 KB Output is correct
12 Correct 2 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 2 ms 860 KB Output is correct
20 Correct 5 ms 1232 KB Output is correct
21 Correct 8 ms 1236 KB Output is correct
22 Correct 4 ms 1244 KB Output is correct
23 Correct 4 ms 1240 KB Output is correct
24 Correct 4 ms 1240 KB Output is correct
25 Correct 8 ms 1240 KB Output is correct
26 Correct 9 ms 1236 KB Output is correct
27 Correct 7 ms 1960 KB Output is correct
28 Correct 9 ms 1880 KB Output is correct
29 Correct 8 ms 1880 KB Output is correct
30 Correct 6 ms 2136 KB Output is correct
31 Correct 6 ms 1880 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2132 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 4 ms 1240 KB Output is correct
8 Correct 4 ms 1240 KB Output is correct
9 Correct 6 ms 2136 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 436 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 2 ms 860 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 856 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
48 Correct 2 ms 860 KB Output is correct
49 Correct 1 ms 860 KB Output is correct
50 Correct 1 ms 860 KB Output is correct
51 Correct 1 ms 860 KB Output is correct
52 Correct 1 ms 860 KB Output is correct
53 Correct 1 ms 856 KB Output is correct
54 Correct 1 ms 860 KB Output is correct
55 Correct 1 ms 860 KB Output is correct
56 Correct 1 ms 860 KB Output is correct
57 Correct 1 ms 860 KB Output is correct
58 Correct 2 ms 860 KB Output is correct
59 Correct 1 ms 860 KB Output is correct
60 Correct 1 ms 860 KB Output is correct
61 Correct 1 ms 860 KB Output is correct
62 Correct 1 ms 860 KB Output is correct
63 Correct 1 ms 860 KB Output is correct
64 Correct 1 ms 860 KB Output is correct
65 Correct 1 ms 860 KB Output is correct
66 Correct 1 ms 860 KB Output is correct
67 Correct 2 ms 860 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 0 ms 604 KB Output is correct
70 Correct 0 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 2 ms 728 KB Output is correct
73 Correct 1 ms 604 KB Output is correct
74 Correct 1 ms 604 KB Output is correct
75 Correct 1 ms 604 KB Output is correct
76 Correct 1 ms 604 KB Output is correct
77 Correct 2 ms 860 KB Output is correct
78 Correct 2 ms 860 KB Output is correct
79 Correct 2 ms 860 KB Output is correct
80 Correct 2 ms 860 KB Output is correct
81 Correct 2 ms 860 KB Output is correct
82 Correct 1 ms 860 KB Output is correct
83 Correct 1 ms 860 KB Output is correct
84 Correct 1 ms 860 KB Output is correct
85 Correct 2 ms 860 KB Output is correct
86 Correct 1 ms 860 KB Output is correct
87 Correct 1 ms 860 KB Output is correct
88 Correct 2 ms 860 KB Output is correct
89 Correct 5 ms 1232 KB Output is correct
90 Correct 8 ms 1236 KB Output is correct
91 Correct 4 ms 1244 KB Output is correct
92 Correct 4 ms 1240 KB Output is correct
93 Correct 4 ms 1240 KB Output is correct
94 Correct 8 ms 1240 KB Output is correct
95 Correct 9 ms 1236 KB Output is correct
96 Correct 7 ms 1960 KB Output is correct
97 Correct 9 ms 1880 KB Output is correct
98 Correct 8 ms 1880 KB Output is correct
99 Correct 6 ms 2136 KB Output is correct
100 Correct 6 ms 1880 KB Output is correct
101 Correct 0 ms 348 KB Output is correct
102 Correct 1 ms 348 KB Output is correct
103 Correct 6 ms 2132 KB Output is correct
104 Correct 0 ms 348 KB Output is correct
105 Correct 1 ms 604 KB Output is correct
106 Correct 2 ms 860 KB Output is correct
107 Correct 1 ms 860 KB Output is correct
108 Correct 1 ms 860 KB Output is correct
109 Correct 4 ms 1240 KB Output is correct
110 Correct 4 ms 1240 KB Output is correct
111 Correct 6 ms 2136 KB Output is correct
112 Correct 1 ms 348 KB Output is correct
113 Correct 0 ms 440 KB Output is correct
114 Correct 6 ms 2076 KB Output is correct
115 Correct 2 ms 860 KB Output is correct
116 Correct 1 ms 860 KB Output is correct
117 Correct 4 ms 1240 KB Output is correct
118 Correct 4 ms 1280 KB Output is correct
119 Correct 6 ms 1880 KB Output is correct
120 Correct 8 ms 2112 KB Output is correct
121 Correct 6 ms 1876 KB Output is correct
122 Correct 6 ms 1968 KB Output is correct
123 Correct 6 ms 2048 KB Output is correct
124 Correct 7 ms 2136 KB Output is correct
125 Correct 6 ms 2136 KB Output is correct
126 Correct 6 ms 2136 KB Output is correct
127 Correct 6 ms 2136 KB Output is correct
128 Correct 6 ms 1968 KB Output is correct