Submission #943782

# Submission time Handle Problem Language Result Execution time Memory
943782 2024-03-11T22:11:16 Z guechotjrhh Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 19256 KB
#include<iostream>
#include<string>
using namespace std;

#include<map>
#include<string>
#include<algorithm>
#include<vector>
#define maxp 1048580
using namespace std;
#define all(x) x.begin(),x.end()
string str;
int st[maxp];
int n,p;
int zer[maxp], on[maxp];
//vector<int> zer, on;//sum of those i contains, sum of those contains me i
void init(int N, int Q, string S) {
	n = N; p = 1 << n;
	str = S;
	for (int i = 0; i < p; i++) st[i] = str[i] - '0';
	//zer.resize(p); on.resize(p);
	for (int i = 0; i < p; i++) zer[i] = on[i] = S[i] - '0';
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < p; j++) {
			if (j & (1 << i)) zer[j] += zer[j - (1 << i)];
			else on[j] += on[j + (1 << i)];
		}
	}
}
int ab(int x) { return x > 0 ? x : -x; }
int res;
void rec1(vector<int>& o, int i, int c, int a, int b) {
	if (i == o.size()) {
		res += (a^zer[c]) + b;
		return;
	}

	rec1(o, i + 1, c, a, b);
	rec1(o, i + 1, c + o[i], ~a, 1-b);
}
void rec0(vector<int>& z, int i, int c, int a, int b) {
	if (i == z.size()) {
		res += (a^on[c]) + b;
		return;
	}

	rec0(z, i + 1, c, a, b);
	rec0(z, i + 1, c + z[i], ~a, 1-b);
}
void recq(vector<int>& q, int i, int c) {
	if (i == q.size()) { res += st[c]; return; }
	recq(q, i + 1, c);
	recq(q, i + 1, c + q[i]);
}
int query(string A) {
	vector<int> ze, o, q;
	for (int i = 0; i < n; i++) {
		if (A[i] == '0') ze.push_back(1 << (n-1-i));
		else if (A[i] == '1') o.push_back(1 << (n-1-i));
		else q.push_back(1 << (n-1-i));
	}
/*
3 5
12345678
000
0??
1?0
?11
???
*/
	if (ze.size() > o.size() && q.size() > o.size()) {
		int c = 0;
		for (int i : q) c += i;
		res = 0;
		rec1(o, 0, c, 0, 0);
		/*for (int i = 0; i < (1 << o.size()); i++) {
			int w = c, x=0;
			for (int j = 0; j < o.size(); j++) {
				if (i & (1 << j)) {
					w += o[j];
					x++;
				}
			}
			if (x & 1) res += zer[w];
			else res -= zer[w];
		}*/
		return ab(res);
	}
	else if(q.size() > ze.size()) {
		int c = 0;
		for (int i : o) c += i;
		res = 0;
		rec0(ze, 0, c, 0, 0);
		/*for (int i = 0; i < (1 << ze.size()); i++) {
			int w = c, x = 0;
			for (int j = 0; j < ze.size(); j++) {
				if (i & (1 << j)) {
					w += ze[j];
					x++;
				}
			}
			if (x & 1) res += on[w];
			else res -= on[w];
		}*/
		return ab(res);
	}
	else {
		res = 0;
		int c = 0;
		for (int i : o) c += i;
		recq(q, 0, c);
		return res;
	}
	return 0;
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, Q; cin >> N >> Q;
	string S; cin >> S;
	init(N, Q, move(S));
	
	for (int i = 0; i < Q; i++) {
		string A;
		cin >> A;
		cout << query(move(A)) << endl;
	}
}

Compilation message

snake_escaping.cpp: In function 'void rec1(std::vector<int>&, int, int, int, int)':
snake_escaping.cpp:33:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |  if (i == o.size()) {
      |      ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void rec0(std::vector<int>&, int, int, int, int)':
snake_escaping.cpp:42:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |  if (i == z.size()) {
      |      ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void recq(std::vector<int>&, int, int)':
snake_escaping.cpp:51:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |  if (i == q.size()) { res += st[c]; return; }
      |      ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4556 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4556 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 1437 ms 8564 KB Output is correct
12 Correct 1443 ms 8004 KB Output is correct
13 Correct 1500 ms 7396 KB Output is correct
14 Correct 1522 ms 7672 KB Output is correct
15 Correct 1461 ms 8528 KB Output is correct
16 Correct 1518 ms 7508 KB Output is correct
17 Correct 1516 ms 7712 KB Output is correct
18 Correct 1350 ms 9460 KB Output is correct
19 Correct 1485 ms 6504 KB Output is correct
20 Correct 1461 ms 8340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4556 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 1437 ms 8564 KB Output is correct
12 Correct 1443 ms 8004 KB Output is correct
13 Correct 1500 ms 7396 KB Output is correct
14 Correct 1522 ms 7672 KB Output is correct
15 Correct 1461 ms 8528 KB Output is correct
16 Correct 1518 ms 7508 KB Output is correct
17 Correct 1516 ms 7712 KB Output is correct
18 Correct 1350 ms 9460 KB Output is correct
19 Correct 1485 ms 6504 KB Output is correct
20 Correct 1461 ms 8340 KB Output is correct
21 Correct 1472 ms 8768 KB Output is correct
22 Correct 1490 ms 8552 KB Output is correct
23 Correct 1576 ms 7768 KB Output is correct
24 Correct 1597 ms 7716 KB Output is correct
25 Correct 1521 ms 9604 KB Output is correct
26 Correct 1599 ms 8080 KB Output is correct
27 Correct 1603 ms 8320 KB Output is correct
28 Correct 1354 ms 10624 KB Output is correct
29 Correct 1467 ms 6588 KB Output is correct
30 Correct 1500 ms 8904 KB Output is correct
31 Correct 1601 ms 8792 KB Output is correct
32 Correct 1604 ms 8592 KB Output is correct
33 Correct 1493 ms 7472 KB Output is correct
34 Correct 1662 ms 7904 KB Output is correct
35 Correct 1615 ms 7888 KB Output is correct
36 Correct 1359 ms 6492 KB Output is correct
37 Correct 1478 ms 8404 KB Output is correct
38 Correct 1493 ms 6816 KB Output is correct
39 Correct 1568 ms 7764 KB Output is correct
40 Correct 1596 ms 7948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4556 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 106 ms 14844 KB Output is correct
12 Correct 103 ms 14844 KB Output is correct
13 Correct 135 ms 14844 KB Output is correct
14 Correct 125 ms 14848 KB Output is correct
15 Correct 105 ms 14848 KB Output is correct
16 Correct 121 ms 14848 KB Output is correct
17 Correct 126 ms 14848 KB Output is correct
18 Correct 94 ms 14848 KB Output is correct
19 Correct 101 ms 14844 KB Output is correct
20 Correct 115 ms 14844 KB Output is correct
21 Correct 111 ms 14848 KB Output is correct
22 Correct 125 ms 14848 KB Output is correct
23 Correct 104 ms 14848 KB Output is correct
24 Correct 145 ms 14844 KB Output is correct
25 Correct 124 ms 14844 KB Output is correct
26 Correct 99 ms 14848 KB Output is correct
27 Correct 100 ms 14848 KB Output is correct
28 Correct 103 ms 14844 KB Output is correct
29 Correct 115 ms 14848 KB Output is correct
30 Correct 134 ms 14848 KB Output is correct
31 Correct 109 ms 14848 KB Output is correct
32 Correct 127 ms 14848 KB Output is correct
33 Correct 122 ms 14848 KB Output is correct
34 Correct 100 ms 14844 KB Output is correct
35 Correct 115 ms 14844 KB Output is correct
36 Correct 113 ms 14848 KB Output is correct
37 Correct 121 ms 14848 KB Output is correct
38 Correct 115 ms 14848 KB Output is correct
39 Correct 115 ms 14848 KB Output is correct
40 Correct 114 ms 14844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4556 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4440 KB Output is correct
10 Correct 2 ms 4440 KB Output is correct
11 Correct 1437 ms 8564 KB Output is correct
12 Correct 1443 ms 8004 KB Output is correct
13 Correct 1500 ms 7396 KB Output is correct
14 Correct 1522 ms 7672 KB Output is correct
15 Correct 1461 ms 8528 KB Output is correct
16 Correct 1518 ms 7508 KB Output is correct
17 Correct 1516 ms 7712 KB Output is correct
18 Correct 1350 ms 9460 KB Output is correct
19 Correct 1485 ms 6504 KB Output is correct
20 Correct 1461 ms 8340 KB Output is correct
21 Correct 1472 ms 8768 KB Output is correct
22 Correct 1490 ms 8552 KB Output is correct
23 Correct 1576 ms 7768 KB Output is correct
24 Correct 1597 ms 7716 KB Output is correct
25 Correct 1521 ms 9604 KB Output is correct
26 Correct 1599 ms 8080 KB Output is correct
27 Correct 1603 ms 8320 KB Output is correct
28 Correct 1354 ms 10624 KB Output is correct
29 Correct 1467 ms 6588 KB Output is correct
30 Correct 1500 ms 8904 KB Output is correct
31 Correct 1601 ms 8792 KB Output is correct
32 Correct 1604 ms 8592 KB Output is correct
33 Correct 1493 ms 7472 KB Output is correct
34 Correct 1662 ms 7904 KB Output is correct
35 Correct 1615 ms 7888 KB Output is correct
36 Correct 1359 ms 6492 KB Output is correct
37 Correct 1478 ms 8404 KB Output is correct
38 Correct 1493 ms 6816 KB Output is correct
39 Correct 1568 ms 7764 KB Output is correct
40 Correct 1596 ms 7948 KB Output is correct
41 Correct 106 ms 14844 KB Output is correct
42 Correct 103 ms 14844 KB Output is correct
43 Correct 135 ms 14844 KB Output is correct
44 Correct 125 ms 14848 KB Output is correct
45 Correct 105 ms 14848 KB Output is correct
46 Correct 121 ms 14848 KB Output is correct
47 Correct 126 ms 14848 KB Output is correct
48 Correct 94 ms 14848 KB Output is correct
49 Correct 101 ms 14844 KB Output is correct
50 Correct 115 ms 14844 KB Output is correct
51 Correct 111 ms 14848 KB Output is correct
52 Correct 125 ms 14848 KB Output is correct
53 Correct 104 ms 14848 KB Output is correct
54 Correct 145 ms 14844 KB Output is correct
55 Correct 124 ms 14844 KB Output is correct
56 Correct 99 ms 14848 KB Output is correct
57 Correct 100 ms 14848 KB Output is correct
58 Correct 103 ms 14844 KB Output is correct
59 Correct 115 ms 14848 KB Output is correct
60 Correct 134 ms 14848 KB Output is correct
61 Correct 109 ms 14848 KB Output is correct
62 Correct 127 ms 14848 KB Output is correct
63 Correct 122 ms 14848 KB Output is correct
64 Correct 100 ms 14844 KB Output is correct
65 Correct 115 ms 14844 KB Output is correct
66 Correct 113 ms 14848 KB Output is correct
67 Correct 121 ms 14848 KB Output is correct
68 Correct 115 ms 14848 KB Output is correct
69 Correct 115 ms 14848 KB Output is correct
70 Correct 114 ms 14844 KB Output is correct
71 Correct 1656 ms 18912 KB Output is correct
72 Correct 1676 ms 19256 KB Output is correct
73 Correct 1885 ms 17380 KB Output is correct
74 Execution timed out 2045 ms 17884 KB Time limit exceeded
75 Halted 0 ms 0 KB -