Submission #881482

# Submission time Handle Problem Language Result Execution time Memory
881482 2023-12-01T09:42:13 Z mgl_diamond Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
618 ms 39308 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
 
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"

template<class T> bool minimize(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> bool maximize(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }

void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  if (fopen(file".inp", "r")) {
    freopen(file".inp", "r", stdin);
    freopen(file".out", "w", stdout);
  }
}

const int N = 20;

int n, q, full;
char a[1<<20];
int sum[1<<20], rsum[1<<20];

int main() {
  setIO();

  cin >> n >> q;
  full = (1<<n)-1;
  foru(i, 0, full) cin >> a[i];
  foru(i, 0, full) {
    a[i] -= '0';
    sum[i] = a[i];
    rsum[i] = a[i];
  }

  foru(bt, 0, n-1) {
    foru(i, 0, full) {
      if (i >> bt & 1)
        sum[i] += sum[i-(1<<bt)];
      else
        rsum[i] += rsum[i+(1<<bt)];
    }
  }

  int cap = n/3;
  while (q--) {
    string s;
    cin >> s;
    reverse(all(s));

    int cnt[3] = {}, ans = 0;
    foru(i, 0, n-1) {
      if (s[i] == '?') cnt[0] += 1;
      else if (s[i] == '1') cnt[1] += 1;
      else cnt[2] += 1;
    }

    if (cnt[0] <= cap) {
      int num = 0, mask = 0;
      foru(i, 0, n-1) 
        if (s[i] == '?') mask += 1 << i;
        else if (s[i] == '1') num += 1 << i;
      for(int i=mask; ; i=(i-1)&mask) {
        ans += a[i|num];
        if (i == 0) break;
      }
      cout << ans << "\n";
    }
    else if (cnt[1] <= cap) {
      int num = 0, mask = 0;
      foru(i, 0, n-1) 
        if (s[i] == '?') num += 1 << i;
        else if (s[i] == '1') mask += 1 << i;
      for(int i=mask; ; i=(i-1)&mask) {
        if ((cnt[1]-__builtin_popcount(i)) & 1) ans -= sum[num|i];
        else ans += sum[num|i];
        if (i == 0) break;
      }
      cout << ans << "\n";
    }
    else {
      int num = 0, mask = 0;
      foru(i, 0, n-1) 
        if (s[i] == '1') num += 1 << i;
        else if (s[i] == '0') mask += 1 << i;
      for(int i = mask; ; i=(i-1)&mask) {
        if (__builtin_popcount(i) & 1) ans -= rsum[num|i];
        else ans += rsum[num|i];
        if (i == 0) break;
      }
      cout << ans << "\n";
    }
  }
}

Compilation message

snake_escaping.cpp: In function 'void setIO()':
snake_escaping.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     freopen(file".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     freopen(file".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4564 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4564 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 183 ms 19280 KB Output is correct
12 Correct 200 ms 18880 KB Output is correct
13 Correct 205 ms 18128 KB Output is correct
14 Correct 178 ms 18568 KB Output is correct
15 Correct 195 ms 19280 KB Output is correct
16 Correct 204 ms 18456 KB Output is correct
17 Correct 200 ms 18560 KB Output is correct
18 Correct 147 ms 20136 KB Output is correct
19 Correct 182 ms 17232 KB Output is correct
20 Correct 217 ms 18776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4564 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 183 ms 19280 KB Output is correct
12 Correct 200 ms 18880 KB Output is correct
13 Correct 205 ms 18128 KB Output is correct
14 Correct 178 ms 18568 KB Output is correct
15 Correct 195 ms 19280 KB Output is correct
16 Correct 204 ms 18456 KB Output is correct
17 Correct 200 ms 18560 KB Output is correct
18 Correct 147 ms 20136 KB Output is correct
19 Correct 182 ms 17232 KB Output is correct
20 Correct 217 ms 18776 KB Output is correct
21 Correct 251 ms 22232 KB Output is correct
22 Correct 273 ms 22472 KB Output is correct
23 Correct 237 ms 21328 KB Output is correct
24 Correct 226 ms 21456 KB Output is correct
25 Correct 269 ms 23076 KB Output is correct
26 Correct 292 ms 21584 KB Output is correct
27 Correct 295 ms 21592 KB Output is correct
28 Correct 164 ms 24008 KB Output is correct
29 Correct 162 ms 20224 KB Output is correct
30 Correct 269 ms 22352 KB Output is correct
31 Correct 292 ms 22348 KB Output is correct
32 Correct 282 ms 22096 KB Output is correct
33 Correct 236 ms 21104 KB Output is correct
34 Correct 228 ms 21072 KB Output is correct
35 Correct 272 ms 21520 KB Output is correct
36 Correct 156 ms 20052 KB Output is correct
37 Correct 237 ms 22128 KB Output is correct
38 Correct 160 ms 20304 KB Output is correct
39 Correct 241 ms 21544 KB Output is correct
40 Correct 220 ms 21196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4564 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 44 ms 11860 KB Output is correct
12 Correct 55 ms 12116 KB Output is correct
13 Correct 44 ms 11860 KB Output is correct
14 Correct 56 ms 11856 KB Output is correct
15 Correct 49 ms 11988 KB Output is correct
16 Correct 55 ms 11856 KB Output is correct
17 Correct 48 ms 11860 KB Output is correct
18 Correct 36 ms 12088 KB Output is correct
19 Correct 45 ms 11716 KB Output is correct
20 Correct 46 ms 11756 KB Output is correct
21 Correct 50 ms 11976 KB Output is correct
22 Correct 54 ms 11736 KB Output is correct
23 Correct 41 ms 11860 KB Output is correct
24 Correct 46 ms 11860 KB Output is correct
25 Correct 47 ms 11856 KB Output is correct
26 Correct 36 ms 11852 KB Output is correct
27 Correct 54 ms 11856 KB Output is correct
28 Correct 42 ms 11856 KB Output is correct
29 Correct 44 ms 11860 KB Output is correct
30 Correct 55 ms 11856 KB Output is correct
31 Correct 52 ms 11716 KB Output is correct
32 Correct 60 ms 11892 KB Output is correct
33 Correct 59 ms 11928 KB Output is correct
34 Correct 36 ms 11604 KB Output is correct
35 Correct 45 ms 11856 KB Output is correct
36 Correct 45 ms 11856 KB Output is correct
37 Correct 47 ms 11856 KB Output is correct
38 Correct 57 ms 11820 KB Output is correct
39 Correct 49 ms 11864 KB Output is correct
40 Correct 45 ms 11720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4564 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 183 ms 19280 KB Output is correct
12 Correct 200 ms 18880 KB Output is correct
13 Correct 205 ms 18128 KB Output is correct
14 Correct 178 ms 18568 KB Output is correct
15 Correct 195 ms 19280 KB Output is correct
16 Correct 204 ms 18456 KB Output is correct
17 Correct 200 ms 18560 KB Output is correct
18 Correct 147 ms 20136 KB Output is correct
19 Correct 182 ms 17232 KB Output is correct
20 Correct 217 ms 18776 KB Output is correct
21 Correct 251 ms 22232 KB Output is correct
22 Correct 273 ms 22472 KB Output is correct
23 Correct 237 ms 21328 KB Output is correct
24 Correct 226 ms 21456 KB Output is correct
25 Correct 269 ms 23076 KB Output is correct
26 Correct 292 ms 21584 KB Output is correct
27 Correct 295 ms 21592 KB Output is correct
28 Correct 164 ms 24008 KB Output is correct
29 Correct 162 ms 20224 KB Output is correct
30 Correct 269 ms 22352 KB Output is correct
31 Correct 292 ms 22348 KB Output is correct
32 Correct 282 ms 22096 KB Output is correct
33 Correct 236 ms 21104 KB Output is correct
34 Correct 228 ms 21072 KB Output is correct
35 Correct 272 ms 21520 KB Output is correct
36 Correct 156 ms 20052 KB Output is correct
37 Correct 237 ms 22128 KB Output is correct
38 Correct 160 ms 20304 KB Output is correct
39 Correct 241 ms 21544 KB Output is correct
40 Correct 220 ms 21196 KB Output is correct
41 Correct 44 ms 11860 KB Output is correct
42 Correct 55 ms 12116 KB Output is correct
43 Correct 44 ms 11860 KB Output is correct
44 Correct 56 ms 11856 KB Output is correct
45 Correct 49 ms 11988 KB Output is correct
46 Correct 55 ms 11856 KB Output is correct
47 Correct 48 ms 11860 KB Output is correct
48 Correct 36 ms 12088 KB Output is correct
49 Correct 45 ms 11716 KB Output is correct
50 Correct 46 ms 11756 KB Output is correct
51 Correct 50 ms 11976 KB Output is correct
52 Correct 54 ms 11736 KB Output is correct
53 Correct 41 ms 11860 KB Output is correct
54 Correct 46 ms 11860 KB Output is correct
55 Correct 47 ms 11856 KB Output is correct
56 Correct 36 ms 11852 KB Output is correct
57 Correct 54 ms 11856 KB Output is correct
58 Correct 42 ms 11856 KB Output is correct
59 Correct 44 ms 11860 KB Output is correct
60 Correct 55 ms 11856 KB Output is correct
61 Correct 52 ms 11716 KB Output is correct
62 Correct 60 ms 11892 KB Output is correct
63 Correct 59 ms 11928 KB Output is correct
64 Correct 36 ms 11604 KB Output is correct
65 Correct 45 ms 11856 KB Output is correct
66 Correct 45 ms 11856 KB Output is correct
67 Correct 47 ms 11856 KB Output is correct
68 Correct 57 ms 11820 KB Output is correct
69 Correct 49 ms 11864 KB Output is correct
70 Correct 45 ms 11720 KB Output is correct
71 Correct 408 ms 36124 KB Output is correct
72 Correct 459 ms 36392 KB Output is correct
73 Correct 407 ms 34784 KB Output is correct
74 Correct 618 ms 35348 KB Output is correct
75 Correct 466 ms 37148 KB Output is correct
76 Correct 553 ms 35412 KB Output is correct
77 Correct 484 ms 35408 KB Output is correct
78 Correct 236 ms 39308 KB Output is correct
79 Correct 264 ms 33292 KB Output is correct
80 Correct 410 ms 36432 KB Output is correct
81 Correct 529 ms 36176 KB Output is correct
82 Correct 618 ms 35152 KB Output is correct
83 Correct 326 ms 34324 KB Output is correct
84 Correct 451 ms 35396 KB Output is correct
85 Correct 525 ms 35380 KB Output is correct
86 Correct 227 ms 33196 KB Output is correct
87 Correct 422 ms 36244 KB Output is correct
88 Correct 226 ms 33108 KB Output is correct
89 Correct 404 ms 34748 KB Output is correct
90 Correct 439 ms 35148 KB Output is correct
91 Correct 351 ms 34388 KB Output is correct
92 Correct 547 ms 35412 KB Output is correct
93 Correct 512 ms 35328 KB Output is correct
94 Correct 253 ms 32812 KB Output is correct
95 Correct 457 ms 35364 KB Output is correct
96 Correct 461 ms 35264 KB Output is correct
97 Correct 466 ms 35560 KB Output is correct
98 Correct 435 ms 35276 KB Output is correct
99 Correct 424 ms 35156 KB Output is correct
100 Correct 443 ms 35160 KB Output is correct