Submission #1020305

# Submission time Handle Problem Language Result Execution time Memory
1020305 2024-07-11T20:47:29 Z YassirSalama Vision Program (IOI19_vision) C++17
100 / 100
33 ms 5928 KB
#include "vision.h"
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define endl "\n"
using ull=unsigned long long;
using ll=long long;
using pii=pair<int,int>;
const int mod=1e9+7;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
    copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
 
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int mxn=2e5+100;
map<int,int> l;//diameter -> key
map<int,int> r;//diameter -> key
int n,m;
int dist(int a,int b,int c,int d){
	return abs(c-a)+abs(b-d);}
map<int,vector<int>> indl;
int rl=0;
void doleft(){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			indl[i-j+m-1].pb(i*m+j);
		}
	}
}
int lr=0;
map<int,vector<int>> indr;
void doright(){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			indr[i+j].pb(i*m+j);
			lr=max(lr,i+j);
		}
	}
}
vector<int> ans;
int op(int k){
	// dbg(k)
	vector<int> x1,x2,y1,y2;
	vector<int> ans;
	for(int i=0;i<=n+m-2;i++){
		// dbg(indl[i],indr[i])
		x1.push_back(add_or(indl[i]));
		y1.push_back(add_or(indr[i]));
		x2.push_back(add_or(x1));
		y2.push_back(add_or(y1));
		if(i>=k){
			ans.push_back(add_and({x1[i], x2[i-k]}));
			ans.push_back(add_and({y1[i], y2[i-k]})); 
		}
	}
	return add_or(ans);
}
void construct_network(int H, int W, int K) {
	n=H;
	m=W;
	vector<int> indices;
	doleft();
	doright();
	if(K==n+m-2){
		op(K);
		return;
	}
	int x=op(K);
	int y=op(K+1);
	add_xor({x,y});
}


#ifdef IOI
#include "vision.h"

using namespace std;

static const int MAX_INSTRUCTIONS = 10000;
static const int MAX_INPUTS = 1000000;

static const int _AND = 0;
static const int _OR = 1;
static const int _XOR = 2;
static const int _NOT = 3;

static inline bool increasing(int a, int b, int c) {
	return a <= b && b <= c;
}

[[noreturn]] static inline void error(string message) {
	printf("%s\n", message.c_str());
	exit(0);
}

class InstructionNetwork {

	struct Instruction {
		int type;
		vector<int> input_indexes;

		inline Instruction(int _type, const vector<int>& _input_indexes):
				type(_type), input_indexes(_input_indexes) {
		}

		inline int apply(int a, int b) const {
			switch (type) {
				case _AND:
					return a & b;
				case _OR:
					return a | b;
				case _XOR:
					return a ^ b;
				default:
					return 0;
			}
		}

		inline int compute(const vector<int>& memory_cells) const {
			int r = memory_cells[input_indexes[0]];
			if (type == _NOT)
				return 1 - r;
			for (int j = 1; j < (int)input_indexes.size(); j++)
				r = apply(r, memory_cells[input_indexes[j]]);
			return r;
		}
	};

	int input_size;
	int total_inputs;
	vector<Instruction> instructions;

public:

	inline void init(int _input_size) {
		this->input_size = _input_size;
		this->total_inputs = 0;
		this->instructions.clear();
	}

	inline int add_instruction(int type, const vector<int>& input_indexes) {
		if (input_indexes.size() == 0)
			error("Instruction with no inputs");

		if (instructions.size() + 1 > MAX_INSTRUCTIONS)
			error("Too many instructions");

		if (total_inputs + input_indexes.size() > MAX_INPUTS)
			error("Too many inputs");

		instructions.emplace_back(type, input_indexes);
		total_inputs += input_indexes.size();
		int new_index = input_size + (int)instructions.size() - 1;

		for (int input_index : input_indexes)
			if (!increasing(0, input_index, new_index-1))
				error("Invalid index");

		return new_index;
	}

	inline int compute(vector<int> &memory_cells) const {
		for (auto &instruction : instructions)
			memory_cells.push_back(instruction.compute(memory_cells));
		return memory_cells.back();
	}
};


static InstructionNetwork instructionNetwork;

int main() {
	int H, W, K;
	assert(3 == scanf("%d%d%d", &H, &W, &K));

	FILE *log_file = fopen("log.txt","w");

	instructionNetwork.init(H * W);
	construct_network(H, W, K);

	while (true) {
		int rowA, colA, rowB, colB;
		assert(1 == scanf("%d", &rowA));
		if (rowA == -1)
			break;
		assert(3 == scanf("%d%d%d", &colA, &rowB, &colB));

		if ((!increasing(0, rowA, H-1)) ||
			(!increasing(0, colA, W-1)) ||
			(!increasing(0, rowB, H-1)) ||
			(!increasing(0, colB, W-1)) ||
			(rowA == rowB && colA == colB)) {
			printf("-1\n");
			fprintf(log_file, "-1\n");
			fflush(stdout);
			fflush(log_file);
			continue;
		}

		vector<int> memory_cells;
		for (int row = 0; row < H; row++)
			for (int col = 0; col < W; col++) {
				bool active = (row == rowA && col == colA) || (row == rowB && col == colB);
				memory_cells.push_back(active ? 1 : 0);
			}
		int computation_result = instructionNetwork.compute(memory_cells);

		printf("%d\n", computation_result);
		fflush(stdout);

		for(int i = 0; i < (int)memory_cells.size(); i++)
			fprintf(log_file, (i ? " %d" : "%d"), memory_cells[i]);
		fprintf(log_file, "\n");
		fflush(log_file);
	}
	fclose(stdin);
}

int add_and(vector<int> Ns) {
	return instructionNetwork.add_instruction(_AND, Ns);
}

int add_or(vector<int> Ns) {
	return instructionNetwork.add_instruction(_OR, Ns);
}

int add_xor(vector<int> Ns) {
	return instructionNetwork.add_instruction(_XOR, Ns);
}

int add_not(int N) {
	vector<int> Ns = {N};
	return instructionNetwork.add_instruction(_NOT, Ns);
}

#endif
# 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 1 ms 348 KB Output is correct
4 Correct 0 ms 432 KB Output is correct
5 Correct 0 ms 348 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 348 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 1 ms 348 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 1 ms 348 KB Output is correct
4 Correct 0 ms 432 KB Output is correct
5 Correct 0 ms 348 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 348 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 1 ms 348 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 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 440 KB Output is correct
27 Correct 0 ms 344 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 1 ms 348 KB Output is correct
4 Correct 0 ms 432 KB Output is correct
5 Correct 0 ms 348 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 348 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 1 ms 348 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 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 440 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 508 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 432 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 1 ms 348 KB Output is correct
4 Correct 0 ms 432 KB Output is correct
5 Correct 0 ms 348 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 348 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 1 ms 348 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 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 440 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 508 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 432 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 7 ms 1884 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 3 ms 604 KB Output is correct
41 Correct 4 ms 988 KB Output is correct
42 Correct 4 ms 1168 KB Output is correct
43 Correct 8 ms 1884 KB Output is correct
44 Correct 8 ms 1884 KB Output is correct
45 Correct 7 ms 1724 KB Output is correct
46 Correct 7 ms 1628 KB Output is correct
47 Correct 4 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1116 KB Output is correct
2 Correct 5 ms 1116 KB Output is correct
3 Correct 5 ms 1300 KB Output is correct
4 Correct 2 ms 860 KB Output is correct
5 Correct 5 ms 1112 KB Output is correct
6 Correct 5 ms 1372 KB Output is correct
7 Correct 5 ms 1116 KB Output is correct
8 Correct 5 ms 1116 KB Output is correct
9 Correct 5 ms 1116 KB Output is correct
10 Correct 7 ms 1368 KB Output is correct
11 Correct 5 ms 1112 KB Output is correct
12 Correct 7 ms 1112 KB Output is correct
13 Correct 5 ms 1116 KB Output is correct
14 Correct 4 ms 860 KB Output is correct
15 Correct 5 ms 1116 KB Output is correct
16 Correct 5 ms 1116 KB Output is correct
17 Correct 5 ms 1112 KB Output is correct
18 Correct 5 ms 1116 KB Output is correct
19 Correct 5 ms 1252 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 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 3 ms 860 KB Output is correct
4 Correct 3 ms 1112 KB Output is correct
5 Correct 6 ms 860 KB Output is correct
6 Correct 3 ms 860 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 6 ms 1452 KB Output is correct
9 Correct 6 ms 1372 KB Output is correct
10 Correct 5 ms 1372 KB Output is correct
11 Correct 6 ms 1372 KB Output is correct
12 Correct 3 ms 932 KB Output is correct
13 Correct 9 ms 1116 KB Output is correct
14 Correct 5 ms 1372 KB Output is correct
15 Correct 9 ms 1112 KB Output is correct
16 Correct 2 ms 780 KB Output is correct
17 Correct 5 ms 1116 KB Output is correct
18 Correct 5 ms 1116 KB Output is correct
19 Correct 3 ms 860 KB Output is correct
20 Correct 16 ms 3396 KB Output is correct
21 Correct 15 ms 3416 KB Output is correct
22 Correct 15 ms 3424 KB Output is correct
23 Correct 14 ms 1884 KB Output is correct
24 Correct 16 ms 3420 KB Output is correct
25 Correct 16 ms 3420 KB Output is correct
26 Correct 8 ms 1884 KB Output is correct
27 Correct 29 ms 5928 KB Output is correct
28 Correct 28 ms 5836 KB Output is correct
29 Correct 28 ms 5712 KB Output is correct
30 Correct 27 ms 5724 KB Output is correct
31 Correct 15 ms 3416 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 5840 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
4 Correct 5 ms 1360 KB Output is correct
5 Correct 5 ms 1116 KB Output is correct
6 Correct 5 ms 1116 KB Output is correct
7 Correct 17 ms 3420 KB Output is correct
8 Correct 16 ms 3416 KB Output is correct
9 Correct 30 ms 5840 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 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 1 ms 348 KB Output is correct
4 Correct 0 ms 432 KB Output is correct
5 Correct 0 ms 348 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 348 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 1 ms 348 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 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 440 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 508 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 432 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 7 ms 1884 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 3 ms 604 KB Output is correct
41 Correct 4 ms 988 KB Output is correct
42 Correct 4 ms 1168 KB Output is correct
43 Correct 8 ms 1884 KB Output is correct
44 Correct 8 ms 1884 KB Output is correct
45 Correct 7 ms 1724 KB Output is correct
46 Correct 7 ms 1628 KB Output is correct
47 Correct 4 ms 1116 KB Output is correct
48 Correct 5 ms 1116 KB Output is correct
49 Correct 5 ms 1116 KB Output is correct
50 Correct 5 ms 1300 KB Output is correct
51 Correct 2 ms 860 KB Output is correct
52 Correct 5 ms 1112 KB Output is correct
53 Correct 5 ms 1372 KB Output is correct
54 Correct 5 ms 1116 KB Output is correct
55 Correct 5 ms 1116 KB Output is correct
56 Correct 5 ms 1116 KB Output is correct
57 Correct 7 ms 1368 KB Output is correct
58 Correct 5 ms 1112 KB Output is correct
59 Correct 7 ms 1112 KB Output is correct
60 Correct 5 ms 1116 KB Output is correct
61 Correct 4 ms 860 KB Output is correct
62 Correct 5 ms 1116 KB Output is correct
63 Correct 5 ms 1116 KB Output is correct
64 Correct 5 ms 1112 KB Output is correct
65 Correct 5 ms 1116 KB Output is correct
66 Correct 5 ms 1252 KB Output is correct
67 Correct 3 ms 860 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 3 ms 860 KB Output is correct
73 Correct 3 ms 1112 KB Output is correct
74 Correct 6 ms 860 KB Output is correct
75 Correct 3 ms 860 KB Output is correct
76 Correct 2 ms 604 KB Output is correct
77 Correct 6 ms 1452 KB Output is correct
78 Correct 6 ms 1372 KB Output is correct
79 Correct 5 ms 1372 KB Output is correct
80 Correct 6 ms 1372 KB Output is correct
81 Correct 3 ms 932 KB Output is correct
82 Correct 9 ms 1116 KB Output is correct
83 Correct 5 ms 1372 KB Output is correct
84 Correct 9 ms 1112 KB Output is correct
85 Correct 2 ms 780 KB Output is correct
86 Correct 5 ms 1116 KB Output is correct
87 Correct 5 ms 1116 KB Output is correct
88 Correct 3 ms 860 KB Output is correct
89 Correct 16 ms 3396 KB Output is correct
90 Correct 15 ms 3416 KB Output is correct
91 Correct 15 ms 3424 KB Output is correct
92 Correct 14 ms 1884 KB Output is correct
93 Correct 16 ms 3420 KB Output is correct
94 Correct 16 ms 3420 KB Output is correct
95 Correct 8 ms 1884 KB Output is correct
96 Correct 29 ms 5928 KB Output is correct
97 Correct 28 ms 5836 KB Output is correct
98 Correct 28 ms 5712 KB Output is correct
99 Correct 27 ms 5724 KB Output is correct
100 Correct 15 ms 3416 KB Output is correct
101 Correct 1 ms 344 KB Output is correct
102 Correct 0 ms 344 KB Output is correct
103 Correct 28 ms 5840 KB Output is correct
104 Correct 0 ms 344 KB Output is correct
105 Correct 3 ms 860 KB Output is correct
106 Correct 5 ms 1360 KB Output is correct
107 Correct 5 ms 1116 KB Output is correct
108 Correct 5 ms 1116 KB Output is correct
109 Correct 17 ms 3420 KB Output is correct
110 Correct 16 ms 3416 KB Output is correct
111 Correct 30 ms 5840 KB Output is correct
112 Correct 0 ms 344 KB Output is correct
113 Correct 0 ms 348 KB Output is correct
114 Correct 28 ms 5792 KB Output is correct
115 Correct 5 ms 1112 KB Output is correct
116 Correct 5 ms 1116 KB Output is correct
117 Correct 15 ms 3420 KB Output is correct
118 Correct 17 ms 3420 KB Output is correct
119 Correct 27 ms 5892 KB Output is correct
120 Correct 31 ms 5716 KB Output is correct
121 Correct 28 ms 5720 KB Output is correct
122 Correct 27 ms 5720 KB Output is correct
123 Correct 28 ms 5848 KB Output is correct
124 Correct 28 ms 5768 KB Output is correct
125 Correct 28 ms 5712 KB Output is correct
126 Correct 33 ms 5716 KB Output is correct
127 Correct 28 ms 5724 KB Output is correct
128 Correct 19 ms 3420 KB Output is correct