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

#include <iostream>
#include <stdio.h>

using namespace std;

int N, k, a, result, tate, yoko;

int main() {
  cin >> N >> k;
  if (N % 2 == 1) {
    a = N / 2 + 1;
    for (int i = 0; i < k; i++) {
      cin >> yoko >> tate;
      if (yoko > a) {
        yoko = N - yoko + 1;
      } else if (tate > a) {
        tate = N - tate + 1;
      }
      // printf("[%d][%d]\\n",yoko,tate);
      if (yoko < tate) {
        result = (yoko + 3) % 3;
        if (result == 0)
          result = 3;
        cout << result << endl;
      } else if (tate < yoko) {
        result = (tate + 3) % 3;
        if (result == 0)
          result = 3;
        cout << result << endl;
      } else if (yoko == tate) {
        result = (yoko + 3) % 3;
        if (result == 0)
          result = 3;
        cout << result << endl;
      }
    }
  } else if (N % 2 == 0) {
    a = N / 2;
    for (int i = 0; i < k; i++) {
      cin >> yoko >> tate;
      if (yoko > a) {
        yoko = N - yoko + 1;
      }
      if (tate > a) {
        tate = N - tate + 1;
      }
      // printf("[%d][%d]\\n",yoko,tate);
      if (yoko < tate) {
        result = (yoko + 3) % 3;
        if (result == 0)
          result = 3;
        cout << result << endl;
      } else if (tate < yoko) {
        result = (tate + 3) % 3;
        if (result == 0)
          result = 3;
        cout << result << endl;
      } else if (yoko == tate) {
        result = (yoko + 3) % 3;
        if (result == 0)
          result = 3;
        cout << result << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int N, k, yoko[50000003], tate[50000003], result[50000000];
int main() {
  int a;
  cin >> N >> k;
  if (N % 2 == 1) {
    a = N / 2 + 1;
    for (int i = 0; i < k; i++) {
      cin >> yoko[i] >> tate[i];
      if (yoko[i] > a) {
        for (int j = 0; j < a; j++) {
          if (a == (yoko[i] + j) / 2) {
            yoko[i] = j;
            break;
          }
        }
      }
      if (tate[i] > a) {
        for (int j = 0; j < a; j++) {
          if (a == (tate[i] + j) / 2) {
            tate[i] = j;
            break;
          }
        }
      }
      // cout << yoko[i] << " = " << tate[i] << endl;
      if (yoko[i] > tate[i]) {
        result[i] = (tate[i] + 3) % 3;
        if (result[i] == 0)
          result[i] = 3;
      } else if (yoko[i] == tate[i]) {
        result[i] = (tate[i] + 3) % 3;
        if (result[i] == 0)
          result[i] = 3;
      } else if (yoko[i] < tate[i]) {
        result[i] = (yoko[i] + 3) % 3;
        if (result[i] == 0)
          result[i] = 3;
      }
    }
    for (int i = 0; i < k; i++)
      cout << result[i] << endl;
  } else if (N % 2 == 0) {
    a = N / 2;
    for (int i = 0; i < k; i++) {
      cin >> yoko[i] >> tate[i];
      if (yoko[i] > a) {
        for (int j = 0; j < a; j++) {
          if (a == (yoko[i] + j) / 2) {
            yoko[i] = j + 1;
            break;
          }
        }
      }
      if (tate[i] > a) {
        for (int j = 0; j < a; j++) {
          if (a == (tate[i] + j) / 2) {
            tate[i] = j + 1;
            break;
          }
        }
      }
      // cout << yoko[i] << " = " << tate[i] << endl;
      if (yoko[i] > tate[i]) {
        result[i] = (tate[i] + 3) % 3;
        if (result[i] == 0)
          result[i] = 3;
      } else if (yoko[i] == tate[i]) {
        result[i] = (tate[i] + 3) % 3;
        if (result[i] == 0)
          result[i] = 3;
      } else if (yoko[i] < tate[i]) {
        result[i] = (yoko[i] + 3) % 3;
        if (result[i] == 0)
          result[i] = 3;
      }
    }
    for (int i = 0; i < k; i++)
      cout << result[i] << endl;
  }
  return 0;
}