Problem p00574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
int h, w, n, m, k, s, t, q, sum, last, cnt, ans, a[1000000], dp0[1 << 20],
    dp1[1 << 20];
string str, ss;
bool f;
char c;
int di[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct Edge {
  LL to, cost;
};
vector<Edge> vec[10000];
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
unsigned int popcnt(unsigned long long x) {
  x = (x & 0x5555555555555555ULL) + ((x >> 1) & 0x5555555555555555ULL);
  x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
  x = (x & 0x0F0F0F0F0F0F0F0FULL) + ((x >> 4) & 0x0F0F0F0F0F0F0F0FULL);
  return (x * 0x0101010101010101ULL) >> 56;
}
int main() {
  cin >> n >> m;
  cin >> str;
  rep(i, 1 << n) {
    dp0[((1 << n) - 1) ^ i] = str[i] - '0';
    dp1[i] = str[i] - '0';
  }
  rep(i, n) rep(s, 1LL << n) {
    if ((s & (1 << i)) == 0) {
      dp0[s] += dp0[s | (1 << i)];
      dp1[s] += dp1[s | (1 << i)];
    }
  }
  rep(i, m) {
    int ma0 = 0, ma1 = 0, ma2 = 0;
    int cnt[3] = {};
    ans = 0;
    cin >> ss;
    rep(j, n) if (ss[n - 1 - j] == '0') ma0 += 1 << j, cnt[0]++;
    else if (ss[n - 1 - j] == '1') ma1 += 1 << j, cnt[1]++;
    else ma2 += 1 << j, cnt[2]++;

    if (cnt[0] <= n / 3) {
      Mfor(k, ma0 + 1) {
        k &= ma0;
        if (popcnt(k) % 2)
          ans -= dp1[k | ma1];
        else
          ans += dp1[k | ma1];
      }
    } else if (cnt[1] <= n / 3) {
      Mfor(k, ma1 + 1) {
        k &= ma1;
        if (popcnt(k) % 2)
          ans -= dp0[k | ma0];
        else
          ans += dp0[k | ma0];
      }
    } else {
      Mfor(k, ma2 + 1) {
        k &= ma2;
        ans += str[ma1 + k] - '0';
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, s, t, q, sum, last, cnt, ans, a[1000000], dp0[1 << 20],
    dp1[1 << 20];
string str, ss;
bool f;
char c;
int di[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct Edge {
  LL to, cost;
};
vector<Edge> vec[10000];
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
unsigned int popcnt(unsigned long long x) {
  x = (x & 0x5555555555555555ULL) + ((x >> 1) & 0x5555555555555555ULL);
  x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
  x = (x & 0x0F0F0F0F0F0F0F0FULL) + ((x >> 4) & 0x0F0F0F0F0F0F0F0FULL);
  return (x * 0x0101010101010101ULL) >> 56;
}
int main() {
  cin >> n >> m;
  cin >> str;
  rep(i, 1LL << n) {
    dp0[((1LL << n) - 1) ^ i] = str[i] - '0';
    dp1[i] = str[i] - '0';
  }
  rep(i, n) rep(s, 1LL << n) {
    if ((s & (1LL << i)) == 0) {
      dp0[s] += dp0[s | (1LL << i)];
      dp1[s] += dp1[s | (1LL << i)];
    }
  }
  rep(i, m) {
    LL ma0 = 0, ma1 = 0, ma2 = 0;
    LL cnt[3] = {};
    ans = 0;
    cin >> ss;
    rep(j, n) if (ss[n - 1 - j] == '0') ma0 |= 1LL << j, cnt[0]++;
    else if (ss[n - 1 - j] == '1') ma1 |= 1LL << j, cnt[1]++;
    else ma2 |= 1LL << j, cnt[2]++;

    if (cnt[0] <= n / 3) {
      Mfor(k, ma0 + 1) {
        k &= ma0;
        if (popcnt(k) % 2)
          ans -= dp1[k | ma1];
        else
          ans += dp1[k | ma1];
      }
    } else if (cnt[1] <= n / 3) {
      Mfor(k, ma1 + 1) {
        k &= ma1;
        if (popcnt(k) % 2)
          ans -= dp0[k | ma0];
        else
          ans += dp0[k | ma0];
      }
    } else {
      Mfor(k, ma2 + 1) {
        k &= ma2;
        ans += str[ma1 + k] - '0';
      }
    }
    cout << ans << endl;
  }
  return 0;
}