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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

typedef unsigned long long ll;

int N;
int blH, blW;
int bdH, bdW;
int block[4][101][101];
int field[101][101];

ll bk[4][101];
ll bd[101];
ll bits[65];

void RotateBlock() {
  for (int k = 0; k < 3; k++)
    for (int j = 0; j < blW; j++)
      for (int i = 0; i < blH; i++)
        block[k + 1][j][blH - 1 - i] = block[k][i][j];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < blH; j++) {
      bk[i][j] = 0;
      for (int k = 0; k < blW; k++)
        if (block[i][j][k])
          bk[i][j] |= (1ULL << k);
    }
  }
}

int main() {
  bits[1] = 1;
  for (int i = 2; i <= 64; i++)
    bits[i] = (bits[i - 1] << 1) + 1;
  cin >> N;
  int prv = -2;
  while (N--) {
    memset(block, 0, sizeof(block));
    cin >> blH >> blW;
    for (int i = 0; i < blH; i++) {
      for (int j = 0; j < blW; j++) {
        char ch;
        cin >> ch;
        if (ch == '#')
          block[0][i][j] = 1;
        else
          block[0][i][j] = 0;
      }
    }
    blH = max(blH, blW);
    blW = blH;
    cin >> bdH >> bdW;
    for (int i = 0; i < bdH; i++) {
      bd[i] = 0;
      for (int j = 0; j < bdW; j++) {
        char ch;
        cin >> ch;
        if (ch == '#') {
          field[i][j] = 1;
          bd[i] |= (1ULL << j);
        } else
          field[i][j] = 0;
      }
    }
    // ubNðñ]³¹ÂÂSêð·
    // ubNÌEºÌÀWª{[hãÌÇÌÀWÉêv·é©H
    // ñ]
    RotateBlock();
    int res = -1;
    for (int k = 0; k < 4; k++) {
      for (int i = 0; i < bdH + blH - 1; i++) {
        for (int j = 0; j < bdW + blW - 1; j++) {
          int cnt = 0;
          // ubNªÍÝoéàµ­ÍdÈè`FbN
          bool isSetable = true;
          for (int l = 0; l < blH; l++) {
            int bdy = -blH + l + 1 + i;
            if (bdy < 0 || bdy >= bdH) {
              if (bk[k][l] != 0) {
                isSetable = false;
                break;
              }
            }
            // ¡ñÌsªdÈéê
            else {
              // ñÌ¤¿dÈéêÆdÈçÈ¢êðª
              int sbdx = -blW + 0 + 1 + j;
              int fbdx = j;
              if (sbdx >= 0 && sbdx < bdW && fbdx >= 0 && fbdx < bdW) {
                // bitZ
                ll a = bk[k][l] << (sbdx);
                // êv·éÈçAcnt
                if ((a ^ bd[bdy]) == bits[bdW])
                  cnt++;
                else if ((a & bd[bdy]) != 0)
                  isSetable = false;
              }
              // ¼[Oêé
              else if ((sbdx < 0 || sbdx >= bdW) && (fbdx < 0 || fbdx >= bdW)) {
                ll a = ((bk[k][l] >> abs(sbdx)) << (64 - bdW)) >> (64 - bdW);
                ll b1 = (bk[k][l] << (64 - abs(sbdx))) >> (64 - abs(sbdx));
                ll b2 = (bk[k][l] >> abs(sbdx) + bdW);
                if (b1 == 0 && b2 == 0) {
                  if ((a ^ bd[bdy]) == bits[bdW])
                    cnt++;
                  else if (((a & bd[bdy]) != 0))
                    isSetable = false;
                } else
                  isSetable = false;
              }
              // E[ªÍÝoé
              else if (sbdx >= 0 && sbdx < bdW) {
                // ÍÝoéêð0Åmask
                int mv = (64 - (bdW - sbdx));
                ll a = ((bk[k][l] << mv) >> mv) << (sbdx);
                // ÍÝo½ê
                ll b = bk[k][l] >> (bdW - sbdx);
                if (b == 0) {
                  if ((a ^ bd[bdy]) == (bits[bdW]))
                    cnt++;
                  else if ((a & bd[bdy]) != 0)
                    isSetable = false;
                } else
                  isSetable = false;
              }
              // ¶[ªÍÝoé
              else {
                ll a = (bk[k][l] >> (blW - fbdx - 1));
                int mv = (64 - (blW - fbdx - 1));
                ll b = (bk[k][l] << mv) >> mv;
                if (b == 0) {
                  if ((a ^ bd[bdy]) == (bits[bdW]))
                    cnt++;
                  else if ((a & bd[bdy]) != 0)
                    isSetable = false;
                } else
                  isSetable = false;
              }
            }
          }
          if (isSetable)
            res = max(res, cnt);
        }
      }
    }
    cout << res << endl;
    prv = res;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int N;
int blH, blW;
int bdH, bdW;
int block[4][101][101];
int field[101][101];
int lineEmpNum[101];

void RotateBlock() {
  for (int k = 0; k < 3; k++)
    for (int j = 0; j < blW; j++)
      for (int i = 0; i < blH; i++)
        block[k + 1][j][blH - 1 - i] = block[k][i][j];
}

int main() {
  cin >> N;
  while (N--) {
    memset(block, 0, sizeof(block));
    cin >> blH >> blW;
    for (int i = 0; i < blH; i++) {
      for (int j = 0; j < blW; j++) {
        char ch;
        cin >> ch;
        if (ch == '#')
          block[0][i][j] = 1;
        else
          block[0][i][j] = 0;
      }
    }
    blH = max(blH, blW);
    blW = blH;
    cin >> bdH >> bdW;
    for (int i = 0; i < bdH; i++) {
      lineEmpNum[i] = 0;
      for (int j = 0; j < bdW; j++) {
        char ch;
        cin >> ch;
        if (ch == '#')
          field[i][j] = 1;
        else {
          lineEmpNum[i]++;
          field[i][j] = 0;
        }
      }
    }
    // ÂuÂÂÂbÂNÂÃ°ÂÃ±Â]ÂÂ³ÂÂ¹ÂÃÂÃÂSÂÃªÂÂÂÃ°ÂÂÂÂ·
    // ÂuÂÂÂbÂNÂÃÂEÂÂºÂÃÂÃÂWÂÂªÂ{Â[ÂhÂÃ£ÂÃÂÃÂÃÂÃÂWÂÃÂÃªÂvÂÂ·ÂÃ©ÂÂ©ÂH
    // ÂÃ±Â]
    RotateBlock();
    int res = 0;
    for (int k = 0; k < 4; k++) {
      for (int i = 0; i < bdH + blH - 1; i++) {
        for (int j = 0; j < bdW + blW - 1; j++) {
          int cnt = 0;
          // ÂuÂÂÂbÂNÂÂªÂÃÂÃÂoÂÃ©ÂÃ ÂÂµÂÂ­ÂÃÂdÂÃÂÃ¨Â`ÂFÂbÂN
          bool isSetable = true;
          for (int l = 0; l < blH; l++) {
            int bdy = -blH + l + 1 + i;
            int a = 0;
            for (int m = 0; m < blW; m++) {
              int bdx = -blW + m + 1 + j;
              if (bdx >= 0 && bdy >= 0 && bdx < bdW && bdy < bdH) {
                if (block[k][l][m] && field[bdy][bdx])
                  isSetable = false;
                else {
                  // ÂÂ¡ÂÃ°ÂÂÂÃÂÃ©
                  if (block[k][l][m])
                    a++;
                }
              } else if (block[k][l][m])
                isSetable = false;
              if (!isSetable)
                break;
            }
            // bdyÂÃÂsÂÂªÂÂÂÃÂÃªÂÃcntÂÃ°ÂCÂÂÂNÂÂÂÂÂÂÂg
            if (!isSetable)
              break;
            else if (cnt + (blH - (l + 1)) <= res) {
              isSetable = false;
              break;
            } else if (bdy >= 0 && bdy < bdH && a == lineEmpNum[bdy])
              cnt++;
          }
          if (isSetable)
            res = max(res, cnt);
        }
      }
    }
    if (res == 0)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
  return 0;
}