답안 #954422

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
954422 2024-03-27T20:53:22 Z mat_jur Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
520 ms 42068 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else 
#define cerr if(0)cout
#define debug(X) ;
#endif
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back

bool is_digit(char c) {
	return c >= '0';
}

int fastin(){
	int x = 0;
	char c;
	while (is_digit(c = getchar_unlocked())) {
		x = x * 10 + c - '0';
	}
	return x;
}

void write_int(unsigned long x) {
	char t[19];
	int i = 0;
	do {
		int d = x%10;
		t[i++] = '0'+d;
		x /= 10;
	} while (x > 0) ;

	while (--i >= 0) {
		putchar_unlocked(t[i]);
	}
	putchar_unlocked('\n');
}

int main() {
	int l = fastin(), q = fastin();
	debug(l);
	debug(q);
	int n = (1<<l);

	vector<int> a(n);
	for(int i = 0; i < n; ++i) {
		char c = getchar_unlocked();
		debug(c);
		a[i] = c-'0';
	}

	char nl = getchar_unlocked();
	debug(a);

	vector<int> sub(n), sup(n);
	for(int i = 0; i < n; ++i) sub[i] = sup[i] = a[i];

	for(int i = 0; i < l; ++i) {
		for(int j = 0; j < n; ++j) {
			if(j&(1<<i)) sub[j] += sub[j^(1<<i)];
			else sup[j] += sup[j^(1<<i)];
		}
	}

	while (q--) {
		int mask[3];
		mask[0] = mask[1] = mask[2] = 0;
		for(int i = l-1; i >= 0; --i) {
			char c = getchar_unlocked();
			if(c == '?') mask[2] |= (1<<i);
			else mask[c-'0'] |= (1<<i);
		}
		nl = getchar_unlocked();
		
		int res = 0;
		int sz0 = __builtin_popcount(mask[0]), sz1 = __builtin_popcount(mask[1]), sz2 = __builtin_popcount(mask[2]);
		if(sz1 <= sz2 && sz1 <= sz0) {
			for(int s = mask[1]; ; s = (s-1)&mask[1]) {
				res += sub[s|mask[2]] * (__builtin_popcount(s) % 2 == sz1 % 2 ? 1 : -1);
				if(!s) break;
			}
		}
		else if(sz0 <= sz1 && sz0 <= sz2) {
			for(int s = mask[0]; ; s = (s-1)&mask[0]) {
				res += sup[s|mask[1]] * ((sz0-__builtin_popcount(s)) % 2 == sz0 % 2 ? 1 : -1);
				if(!s) break;
			}
		} else {
			for(int s = mask[2]; ; s = (s-1)&mask[2]) {
				res += a[s|mask[1]];
				if(!s) break;
			}
		}

		write_int(res);
	}

	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:60:7: warning: variable 'nl' set but not used [-Wunused-but-set-variable]
   60 |  char nl = getchar_unlocked();
      |       ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 71 ms 15016 KB Output is correct
12 Correct 84 ms 15008 KB Output is correct
13 Correct 68 ms 13908 KB Output is correct
14 Correct 75 ms 14160 KB Output is correct
15 Correct 78 ms 15008 KB Output is correct
16 Correct 88 ms 14320 KB Output is correct
17 Correct 87 ms 14160 KB Output is correct
18 Correct 49 ms 15956 KB Output is correct
19 Correct 32 ms 13148 KB Output is correct
20 Correct 83 ms 14684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 71 ms 15016 KB Output is correct
12 Correct 84 ms 15008 KB Output is correct
13 Correct 68 ms 13908 KB Output is correct
14 Correct 75 ms 14160 KB Output is correct
15 Correct 78 ms 15008 KB Output is correct
16 Correct 88 ms 14320 KB Output is correct
17 Correct 87 ms 14160 KB Output is correct
18 Correct 49 ms 15956 KB Output is correct
19 Correct 32 ms 13148 KB Output is correct
20 Correct 83 ms 14684 KB Output is correct
21 Correct 92 ms 18200 KB Output is correct
22 Correct 106 ms 18364 KB Output is correct
23 Correct 92 ms 17340 KB Output is correct
24 Correct 116 ms 17184 KB Output is correct
25 Correct 110 ms 19016 KB Output is correct
26 Correct 120 ms 17492 KB Output is correct
27 Correct 116 ms 17572 KB Output is correct
28 Correct 62 ms 20264 KB Output is correct
29 Correct 41 ms 16128 KB Output is correct
30 Correct 101 ms 18272 KB Output is correct
31 Correct 115 ms 18004 KB Output is correct
32 Correct 126 ms 18004 KB Output is correct
33 Correct 80 ms 16940 KB Output is correct
34 Correct 123 ms 16976 KB Output is correct
35 Correct 127 ms 17488 KB Output is correct
36 Correct 52 ms 16212 KB Output is correct
37 Correct 100 ms 18000 KB Output is correct
38 Correct 43 ms 16040 KB Output is correct
39 Correct 97 ms 17164 KB Output is correct
40 Correct 124 ms 16980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 34 ms 14928 KB Output is correct
12 Correct 29 ms 15056 KB Output is correct
13 Correct 31 ms 14736 KB Output is correct
14 Correct 41 ms 14936 KB Output is correct
15 Correct 29 ms 15044 KB Output is correct
16 Correct 51 ms 14932 KB Output is correct
17 Correct 47 ms 14932 KB Output is correct
18 Correct 32 ms 15192 KB Output is correct
19 Correct 28 ms 14676 KB Output is correct
20 Correct 29 ms 14940 KB Output is correct
21 Correct 34 ms 14928 KB Output is correct
22 Correct 48 ms 14980 KB Output is correct
23 Correct 34 ms 14928 KB Output is correct
24 Correct 46 ms 14932 KB Output is correct
25 Correct 43 ms 14788 KB Output is correct
26 Correct 26 ms 14672 KB Output is correct
27 Correct 30 ms 15028 KB Output is correct
28 Correct 28 ms 14780 KB Output is correct
29 Correct 38 ms 14928 KB Output is correct
30 Correct 31 ms 14932 KB Output is correct
31 Correct 29 ms 14940 KB Output is correct
32 Correct 40 ms 14984 KB Output is correct
33 Correct 39 ms 14940 KB Output is correct
34 Correct 24 ms 14676 KB Output is correct
35 Correct 37 ms 14744 KB Output is correct
36 Correct 36 ms 14952 KB Output is correct
37 Correct 38 ms 14784 KB Output is correct
38 Correct 34 ms 14932 KB Output is correct
39 Correct 48 ms 15188 KB Output is correct
40 Correct 34 ms 14932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 71 ms 15016 KB Output is correct
12 Correct 84 ms 15008 KB Output is correct
13 Correct 68 ms 13908 KB Output is correct
14 Correct 75 ms 14160 KB Output is correct
15 Correct 78 ms 15008 KB Output is correct
16 Correct 88 ms 14320 KB Output is correct
17 Correct 87 ms 14160 KB Output is correct
18 Correct 49 ms 15956 KB Output is correct
19 Correct 32 ms 13148 KB Output is correct
20 Correct 83 ms 14684 KB Output is correct
21 Correct 92 ms 18200 KB Output is correct
22 Correct 106 ms 18364 KB Output is correct
23 Correct 92 ms 17340 KB Output is correct
24 Correct 116 ms 17184 KB Output is correct
25 Correct 110 ms 19016 KB Output is correct
26 Correct 120 ms 17492 KB Output is correct
27 Correct 116 ms 17572 KB Output is correct
28 Correct 62 ms 20264 KB Output is correct
29 Correct 41 ms 16128 KB Output is correct
30 Correct 101 ms 18272 KB Output is correct
31 Correct 115 ms 18004 KB Output is correct
32 Correct 126 ms 18004 KB Output is correct
33 Correct 80 ms 16940 KB Output is correct
34 Correct 123 ms 16976 KB Output is correct
35 Correct 127 ms 17488 KB Output is correct
36 Correct 52 ms 16212 KB Output is correct
37 Correct 100 ms 18000 KB Output is correct
38 Correct 43 ms 16040 KB Output is correct
39 Correct 97 ms 17164 KB Output is correct
40 Correct 124 ms 16980 KB Output is correct
41 Correct 34 ms 14928 KB Output is correct
42 Correct 29 ms 15056 KB Output is correct
43 Correct 31 ms 14736 KB Output is correct
44 Correct 41 ms 14936 KB Output is correct
45 Correct 29 ms 15044 KB Output is correct
46 Correct 51 ms 14932 KB Output is correct
47 Correct 47 ms 14932 KB Output is correct
48 Correct 32 ms 15192 KB Output is correct
49 Correct 28 ms 14676 KB Output is correct
50 Correct 29 ms 14940 KB Output is correct
51 Correct 34 ms 14928 KB Output is correct
52 Correct 48 ms 14980 KB Output is correct
53 Correct 34 ms 14928 KB Output is correct
54 Correct 46 ms 14932 KB Output is correct
55 Correct 43 ms 14788 KB Output is correct
56 Correct 26 ms 14672 KB Output is correct
57 Correct 30 ms 15028 KB Output is correct
58 Correct 28 ms 14780 KB Output is correct
59 Correct 38 ms 14928 KB Output is correct
60 Correct 31 ms 14932 KB Output is correct
61 Correct 29 ms 14940 KB Output is correct
62 Correct 40 ms 14984 KB Output is correct
63 Correct 39 ms 14940 KB Output is correct
64 Correct 24 ms 14676 KB Output is correct
65 Correct 37 ms 14744 KB Output is correct
66 Correct 36 ms 14952 KB Output is correct
67 Correct 38 ms 14784 KB Output is correct
68 Correct 34 ms 14932 KB Output is correct
69 Correct 48 ms 15188 KB Output is correct
70 Correct 34 ms 14932 KB Output is correct
71 Correct 168 ms 39272 KB Output is correct
72 Correct 185 ms 39528 KB Output is correct
73 Correct 199 ms 37712 KB Output is correct
74 Correct 389 ms 38268 KB Output is correct
75 Correct 176 ms 40056 KB Output is correct
76 Correct 470 ms 38536 KB Output is correct
77 Correct 371 ms 38456 KB Output is correct
78 Correct 113 ms 42068 KB Output is correct
79 Correct 93 ms 36600 KB Output is correct
80 Correct 191 ms 39248 KB Output is correct
81 Correct 272 ms 39324 KB Output is correct
82 Correct 520 ms 38324 KB Output is correct
83 Correct 142 ms 37272 KB Output is correct
84 Correct 465 ms 38316 KB Output is correct
85 Correct 375 ms 38452 KB Output is correct
86 Correct 87 ms 36140 KB Output is correct
87 Correct 154 ms 39140 KB Output is correct
88 Correct 85 ms 36188 KB Output is correct
89 Correct 199 ms 37716 KB Output is correct
90 Correct 232 ms 38068 KB Output is correct
91 Correct 136 ms 37460 KB Output is correct
92 Correct 420 ms 38772 KB Output is correct
93 Correct 347 ms 38400 KB Output is correct
94 Correct 100 ms 36176 KB Output is correct
95 Correct 304 ms 38724 KB Output is correct
96 Correct 353 ms 38228 KB Output is correct
97 Correct 293 ms 38292 KB Output is correct
98 Correct 299 ms 38192 KB Output is correct
99 Correct 286 ms 38232 KB Output is correct
100 Correct 305 ms 38252 KB Output is correct