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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int fqm[4][4] = {{1, 2, 3, 4}, {2, -1, 4, -3}, {3, -4, -1, 2}, {4, 3, -2, -1}};
int judge(int a, int b) { return fqm[a][b]; }

int main(void) {
  int n;
  while (cin >> n, n) {
    rep(t, n) {
      int cA[4], cB[4], cal = 0;
      int xx = 0, yy = 0, zz = 0, ww = 0;
      rep(d, 4) { cin >> cA[d]; }
      rep(d, 4) { cin >> cB[d]; }
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          int calc = cA[j] * cB[k];
          int ff = judge(j, k);
          int fl = abs(ff);
          if (fl == 1) {
            xx += (calc * ff);
          } else if (fl == 2) {
            yy += (calc * ff) / 2;
          } else if (fl == 3) {
            zz += (calc * ff) / 3;
          } else if (fl == 4) {
            ww += (calc * ff) / 4;
          }
        }
      }
      cout << xx << " " << yy << " " << zz << " " << ww << endl;
    }
  }

  return 0;
}

int StrToInt(string s) {
  stringstream ss;
  ss << s;
  int val;
  ss >> val;
  return val;
}
string IntToStr(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int fqm[4][4] = {{1, 2, 3, 4}, {2, -1, 4, -3}, {3, -4, -1, 2}, {4, 3, -2, -1}};
int xx, yy, zz, ww;
int judge(int a, int b) { return fqm[a][b]; }

int main(void) {
  int n;
  while (cin >> n, n) {
    int cA[4], cB[4], cal = 0;
    rep(d, 4) { cin >> cA[d]; }
    rep(d, 4) { cin >> cB[d]; }
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        int calc = cA[j] * cB[k];
        int ff = judge(j, k);
        int fl = abs(ff);
        if (fl == 1) {
          xx += (calc * ff);
        } else if (fl == 2) {
          yy += (calc * ff) / 2;
        } else if (fl == 3) {
          zz += (calc * ff) / 3;
        } else if (fl == 4) {
          ww += (calc * ff) / 4;
        }
      }
    }
    cout << xx << " " << yy << " " << zz << " " << ww << endl;
    xx = 0, yy = 0, zz = 0, ww = 0;
  }

  return 0;
}

int StrToInt(string s) {
  stringstream ss;
  ss << s;
  int val;
  ss >> val;
  return val;
}
string IntToStr(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}