Submission #45519

#TimeUsernameProblemLanguageResultExecution timeMemory
45519realitySnake Escaping (JOI18_snake_escaping)C++17
100 / 100
1159 ms65536 KiB
#include "bits/stdc++.h" using namespace std; #define fi first #define se second #define ll long long #define dbg(v) cerr<<#v<<" = "<<v<<'\n' #define vi vector<int> #define vl vector <ll> #define pii pair<int,int> #define mp make_pair #define db long double #define pb push_back #define all(s) s.begin(),s.end() template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} int D0[1 << 20]; int D1[1 << 20]; char str[1 << 20]; int coef[1 << 20]; inline int readChar(); template <class T = int> inline T readInt(); template <class T> inline void writeInt( T x, char end = 0 ); inline void writeChar( int x ); inline void writeWord( const char *s ); /** Read */ static const int buf_size = 4096; inline int getChar() { static char buf[buf_size]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin); if (pos == len) return -1; return buf[pos++]; } inline int readChar() { int c = getChar(); while (c <= 32) c = getChar(); return c; } template <class T> inline T readInt() { int s = 1, c = readChar(); T x = 0; if (c == '-') s = -1, c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return s == 1 ? x : -x; } /** Write */ static int write_pos = 0; static char write_buf[buf_size]; inline void writeChar( int x ) { if (write_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; write_buf[write_pos++] = x; } template <class T> inline void writeInt( T x, char end ) { if (x < 0) writeChar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n--) writeChar(s[n]); if (end) writeChar(end); } inline void writeWord( const char *s ) { while (*s) writeChar(*s++); } struct Flusher { ~Flusher() { if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; } } flusher; char s[64]; int main(void) { int n,q; scanf("%d%d%s",&n,&q,str); const int N = 1 << n; for (int i = 0;i < N;++i) str[i] -= '0'; for (int i = 0;i < N;++i) coef[i] = __builtin_popcount(i) & 1 ? -1 : 1; for (int i = 0;i < N;++i) D0[i] = D1[i] = str[i]; for (int i = 0;i < n;++i) for (int mask = 0;mask < N;++mask) if ((mask >> i) & 1) D0[mask ^ (1 << i)] += D0[mask]; else D1[mask ^ (1 << i)] += D1[mask]; while (q --) { scanf("%s",s); reverse(s,s + n); int a = 0,b = 0,c = 0; int la = 0,lb = 0,lc = 0; for (int i = 0;i < n;++i) { if (s[i] == '0') a |= 1 << i,++la; else if (s[i] == '1') b |= 1 << i,++lb; else c |= 1 << i,++lc; } int ans = 0; if (la <= lb && la <= lc) { for (int mask = a;mask >= 0;mask = !mask ? -1 : ((mask - 1) & a)) ans += coef[mask] * D0[(N - 1) ^ mask ^ c]; } else if (lb <= la && lb <= lc) { for (int mask = b;mask >= 0;mask = !mask ? -1 : ((mask - 1) & b)) ans += coef[mask] * D1[mask ^ c]; } else { for (int mask = c;mask >= 0;mask = !mask ? -1 : ((mask - 1) & c)) ans += str[mask ^ b]; } ans = abs(ans); cout << ans << '\n'; } return 0; }

Compilation message (stderr)

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:107:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for (int i = 0;i < n;++i)
     ^~~
snake_escaping.cpp:113:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     while (q --) {
     ^~~~~
snake_escaping.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%s",&n,&q,str);
     ~~~~~^~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:114:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%s",s);
      ~~~~~^~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...