alexjercan/bugnet huggingface dataset:
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  int N;
  int K;
  int ans = 0;
  int x, y;
  while (scanf("%d", &N) != EOF) {
    scanf("%d", &K);

    for (int i = 0; i < K; i++) {
      scanf("%d %d", &x, &y);

      if (x <= N / 2 && y <= N / 2) {
        ans = min(x, y) % 3;
      } else if (x > N / 2 && y <= N / 2) {
        ans = min(N - x + 1, y) % 3;
      } else if (x <= N / 2 && y > N / 2) {
        ans = min(x, N - y + 1) % 3;
      } else {
        ans = min(N - x + 1, N - y + 1) % 3;
      }

      printf("%d\\n", ans == 0 ? 3 : ans);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int main(void) {
  int N;
  while (scanf("%d", &N) != EOF) {
    int n;
    int x, y;
    bool flg = false;
    if (N % 2) {
      flg = true;
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      int color = 0;
      scanf("%d %d", &x, &y);
      // 

      if (flg) {
        // ï
        if (x > (N + 1) / 2) {
          x = (N + 1) / 2 - (x - (N + 1) / 2);
        }
        if (y > (N + 1) / 2) {
          y = (N + 1) / 2 - (y - (N + 1) / 2);
        }
        //				printf("(%d %d)",x,y);
        while (x > 1 && y > 1) {
          x--;
          y--;
          if (N / 2 <= color)
            color--;
          else
            color++;
        }
      } else {
        // ô
        if (x > N / 2) {
          x = N / 2 - (x - (N + 1) / 2) + 1;
        }
        if (y > N / 2) {
          y = N / 2 - (y - (N + 1) / 2) + 1;
        }
        //				printf("(%d,%d)",x,y);
        while (x > 1 && y > 1) {
          x--;
          y--;
          if (color + 1 == N / 2) {
          } else if ((N - 1) / 2 <= color)
            color--;
          else
            color++;
        }
      }
      printf("%d\\n", color % 3 + 1);
    }
  }
  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define INF 2000000000
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
#define ll long long
#define PL pair<long long, long long>
#define P pair<int, int>
#define mk make_pair
#define en endl;
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0;

#define MAX 505
P p[1005];

int n, k;
int main() {
  cin >> n >> k;
  int border;
  if (n % 2 == 0)
    border = n / 2;
  else
    border = n / 2 + 1;
  for (int i = 0; i < k; i++) {
    int ans;
    int a, b;
    cin >> a >> b;
    if (b > border)
      b = n - b + 1;
    if (b <= a && a <= n - (b - 1))
      ans = b % 3;
    else if (a < b)
      ans = a % 3;
    else
      ans = (n - a + 1) % 3;

    if (ans == 0)
      ans = 3;
    cout << ans << en
  }
  RE
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

static const int RED = 1;
static const int BLUE = 2;
static const int YELLOW = 3;
static const int INFTY = (1 << 29);
int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;

  int col = 0;
  int N[INFTY][INFTY];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      N[i][j] = 0;
    }
  }
  int m = n / 2;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (j <= col) {
        if (j % 3 == 1) {
          N[i][j] = RED;
          N[i][n - j + 1] = RED;
        } else if (j % 3 == 2) {
          N[i][j] = BLUE;
          N[i][n - j + 1] = BLUE;
        } else if (j % 3 == 0) {
          N[i][j] = YELLOW;
          N[i][n - j + 1] = YELLOW;
        }
      } else {
        if (i % 3 == 1) {
          if (N[i][j] == 0)
            N[i][j] = RED;
        } else if (i % 3 == 2) {
          if (N[i][j] == 0)
            N[i][j] = BLUE;
        } else if (i % 3 == 0) {
          if (N[i][j] == 0)
            N[i][j] = YELLOW;
        }
      }
      col++;
    }
  }
  int e;
  if (n % 2 == 1) {
    for (int j = 1; j <= n; j++) {
      if (j % 3 == 1) {
        N[m + 1][j] = RED;
      } else if (j % 3 == 2) {
        N[m + 1][j] = BLUE;
      } else if (j % 3 == 0) {
        N[m + 1][j] = YELLOW;
      }
    }
    for (int j = m + 2, e = 1; j <= n; j++, e++) {
      for (int i = 1; i <= n; i++) {
        N[j][i] = N[e][i];
      }
    }
  } else {
    for (int j = m + 1, e = 1; j <= n; j++, e++) {
      for (int i = 1; i <= n; i++) {
        N[j][i] = N[e][i];
      }
    }
  }

  int a, b;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    cout << N[a][b] << endl;
  }
}
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    int tmp1 = min(a - 1, n - a);
    int tmp2 = min(b - 1, n - b);
    int num = min(tmp1, tmp2);
    int ans = num % 3;
    cout << ans + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int mid = n / 2;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    int cou = min(a, b);
    int tmp;
    if (cou <= mid)
      tmp = (cou + 2) % 3 + 1;
    else
      tmp = (n - cou + 3) % 3 + 1;
    // cout << tmp << endl;
    int num;
    if (a == b)
      cout << tmp << endl;
    else {
      if (a > b)
        num = a - b;
      else
        num = b - a;
      if (cou <= mid) {
        if (cou + num <= n - cou + 1) {
          cout << tmp << endl;
        } else {
          int nn = cou + num - (n - cou + 1);
          int tt = tmp;
          for (int j = 0; j < nn; j++) {
            tt = (tt + 4) % 3 + 1;
          }
          cout << tt << endl;
        }
      } else {
        // int nn = cou+num-(n-cou+1);
        int tt = tmp;
        for (int j = 0; j < num; j++) {
          tt = (tt + 4) % 3 + 1;
        }
        cout << tt << endl;
      }
    }
  }
}
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, k, x, y;
  cin >> n;
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    int pos = min(x, min(y, min(n - x + 1, n - y + 1)));
    switch (pos % 3) {
    case 0:
      cout << 3 << endl;
      break;
    case 1:
      cout << 1 << endl;
      break;
    case 2:
      cout << 2 << endl;
      break;
    default:
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int N;
const int MAX_K = 100;
int K;
int a, b;

int solve(int i) {
  int n = N + 1;
  for (int j = 1; j < n; j++) {
    if (a <= j || b <= j) {
      switch (min(a, b) % 3) {
      case 0:
        return 3;
      case 1:
        return 1;
      case 2:
        return 2;
      }
    } else if (a >= n - j || b <= j) {
      switch (min(n - a, b) % 3) {
      case 0:
        return 3;
      case 1:
        return 1;
      case 2:
        return 2;
      }
    } else if (a <= j || b >= n - j) {
      switch (min(a, n - b) % 3) {
      case 0:
        return 3;
      case 1:
        return 1;
      case 2:
        return 2;
      }
    } else if (a >= n - j || b >= n - j) {
      switch (min(n - a, n - b) % 3) {
      case 0:
        return 3;
      case 1:
        return 1;
      case 2:
        return 2;
      }
    }
  }
  return -1;
}

int main() {
  cin >> N;
  cin >> K;
  for (int i = 0; i < K; i++) {
    cin >> a >> b;
    cout << solve(i) << endl;
  }

  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {

  int n, k, ai, bi, data;
  vector<int> res;

  cin >> n >> k;

  rep(i, k) {
    cin >> ai >> bi;
    data = min(min(ai - 1, n - ai), min(bi - 1, n - bi));
    res.push_back(data % 3 + 1);
  }
  rep(i, res.size()) cout << res[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {

  int n, k, ai, bi, data;
  vector<int> res;

  while (cin >> n, n) {
    cin >> k;

    rep(i, k) {
      cin >> ai >> bi;

      data = min(min(ai - 1, n - ai), min(bi - 1, n - bi));
      res.push_back(data % 3 + 1);
    }
    rep(i, res.size()) cout << res[i] << endl;
  }
  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

namespace _ {
struct _ {
  _() { // initialize
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} _;
} // namespace _
template <typename T, size_t size> inline size_t len(const T (&)[size]) {
  return size;
}
#define println(s) cout << s << '\\n';
#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

int N, K;
// 1 <= K <= 1000
// 1 <= N <= 1000000000

// O(N/2)
int solve(int x, int y) {
  return min(min(x - 1, N - x), min(y - 1, N - y)) % 3 + 1;
  /*
  for (int k=0; k<(N+1)/2; k++) {
    if (x == k || x == N-1-k || y == k || y == N-1-k) {
      return (k % 3) + 1;
    }
  }
  return -1;
  */
}

int main() {
  cin >> N;
  cin >> K;

  rep(i, K) {
    int x, y;
    cin >> x >> y;
    //  red: 1 , blue : 2 , yellow : 3
    cout << solve(x, y) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

namespace _ {
struct _ {
  _() { // initialize
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} _;
} // namespace _
template <typename T, size_t size> inline size_t len(const T (&)[size]) {
  return size;
}
#define println(s) cout << s << '\\n';
#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

int N, K;

int solve(int x, int y) {
  for (int k = 0; k < (N + 1) / 2; k++) {
    if (x == k || x == N - 1 - k || y == k || y == N - 1 - k) {
      return (k % 3) + 1;
    }
  }
  return -1;
}

int main() {
  cin >> N;
  cin >> K;

  rep(i, K) {
    int x, y;
    cin >> x >> y;
    //  red: 1 , blue : 2 , yellow : 3
    cout << solve(x - 1, y - 1) << endl;
  }
  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
// int mapp[100001][100001];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > n - x + 1)
      x = n - x + 1;
    if (y > n - y + 1)
      y = n - y + 1;
    if (y < x) {
      // cout<<"??????";
      if (y % 3 == 0)
        cout << 3 << endl;
      else if (y % 3 == 1)
        cout << 1 << endl;
      else
        cout << 2 << endl;
    } else {
      if (x % 3 == 0)
        cout << 3 << endl;
      else if (x % 3 == 1)
        cout << 1 << endl;
      else
        cout << 2 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
long long int hideo, masao, koutay, nagase, a[1010][1010] = {0}, y = 0, z = 0;
long long int str[1011][1011] = {0};
int main() {
  scanf("%d", &hideo);
  for (int x = 0; x < hideo; x++) {
    for (int i = z; i < hideo - z; i++) {
      for (int j = z; j < hideo - z; j++) {
        if (y == 0) {
          str[i][j] = 1;
        } else if (y == 1) {
          str[i][j] = 2;
        } else {
          str[i][j] = 3;
        }
      }
    }
    z++;
    y++;
    if (y == 3)
      y = 0;
  }
  scanf("%d", &masao);
  for (int i = 0; i < masao; i++) {
    scanf("%d %d", &koutay, &nagase);
    printf("%d\\n", str[koutay - 1][nagase - 1]);
  }
}
Problem p00479 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <stdlib.h>
#include <vector>

using namespace std;

int getColor(int i);

int main() {
  int n, k;
  cin >> n;
  cin >> k;
  int t = n / 2 + n % 2;

  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    int lx = a, ly = b;
    if (a > t)
      lx = n - a + 1;
    if (b > t)
      ly = n - b + 1;

    int c;
    if (lx == t && ly == t)
      c = getColor(t);
    if (ly >= lx) {
      c = getColor(lx);
    } else {
      c = getColor(ly);
    }
    cout << c << endl;
  }

  return 0;
}
int getColor(int i) {
  int c = i % 3;
  if (c == 0)
    return 3;
  return c;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <stdlib.h>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n;
  cin >> k;
  int t = n / 2 + n % 2;
  int *line = new int[t + 1];
  for (int i = 1; i <= t; i++) {
    int c = i % 3;
    if (c == 0)
      c = 3;
    line[i] = c;
  }

  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    int lx = a, ly = b;
    if (a > t)
      lx = n - a + 1;
    if (b > t)
      ly = n - b + 1;

    int c;
    if (lx == t && ly == t)
      c = line[t];
    if (ly >= lx) {
      c = line[lx];
    } else {
      c = line[ly];
    }
    cout << c << endl;
  }

  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int n, k, nn;
  int x[1000];
  int y[1000];
  int z[1000];
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    cin >> x[i];
    cin >> y[i];
  }
  for (int i = 0; i < k; i++) {
    if (n - x[i] < x[i])
      x[i] = n - x[i] + 1;
    if (n - y[i] < y[i])
      y[i] = n - y[i] + 1;

    if (x[i] > y[i])
      z[i] = y[i] % 3;
    if (y[i] > x[i])
      z[i] = x[i] % 3;
    if (x[i] == y[i])
      z[i] = x[i] % 3;
    if (z[i] == 0)
      z[i] = 3;
    cout << z[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int n, k, nn;
  int x[1000];
  int y[1000];
  int z[1000];
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    cin >> x[i];
    cin >> y[i];
    z[i] = 0;
  }
  for (int i = 0; i < k; i++) {
    if (n - x[i] < x[i])
      x[i] = n - x[i] + 1;
    if (n - y[i] < y[i])
      y[i] = n - y[i] + 1;
    nn = 1;
    while (true) {
      if (nn == x[i] || nn == y[i])
        break;
      if (nn - x[i] > 1000000 && nn - y[i] > 1000000) {
        z[i] += 1000000;
        nn += 1000000;
      }
      if (nn - x[i] > 100000 && nn - y[i] > 100000) {
        z[i] += 100000;
        nn += 100000;
      }
      if (nn - x[i] > 100000 && nn - y[i] > 100000) {
        z[i] += 100000;
        nn += 100000;
      }
      if (nn - x[i] > 10000 && nn - y[i] > 10000) {
        z[i] += 10000;
        nn += 10000;
      }
      z[i]++;
      nn++;
    }
    z[i] %= 3;
    cout << z[i] + 1 << endl;
  }
  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define lli long long int

using namespace std;

int main() {
  lli n, m, s, t, u, v;

  while (cin >> n && n) {
    cin >> m;
    if (n % 2 == 0) {
      v = n / 2;
    } else {
      v = (n + 1) / 2;
    }

    while (m--) {
      cin >> s >> t;
      if (s > v) {
        s = n - s + 1;
      }
      if (t > v) {
        t = n - t + 1;
      }

      if (s < t) {
        u = s;
      } else {
        u = t;
      }
      u = (u - 1) % 3;
      cout << (u + 1) << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define lli long long int

using namespace std;

int main() {
  lli n, m, s, t, u, v;

  while (cin >> n && n) {
    cin >> m;
    if (n % 2 == 0) {
      v = n / 2;
    } else {
      v = (n + 1) / 2;
    }

    while (m--) {
      cin >> s >> t;
      if (s > v) {
        s = n - s + 1;
      }
      if (t > v) {
        t = n - t + 1;
      }

      if (s < t) {
        u = s;
      } else {
        u = t;
      }

      for (lli i = 1; i <= v; i += 3) {
        if (i == u) {
          cout << 1 << endl;
          break;
        } else if (i + 1 == u) {
          cout << 2 << endl;
          break;
        } else if (i + 2 == u) {
          cout << 3 << endl;
          break;
        }
      }
    }
  }

  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    int x, y, c;
    cin >> x >> y;
    if (x <= y && x + y <= n + 1) {
      c = (x - 1) % 3;
    } else if (x <= y && x + y > n + 1) {
      c = (n - y) % 3;
    } else if (x > y && x + y <= n + 1) {
      c = (y - 1) % 3;
    } else if (x > y && x + y > n + 1) {
      c = (n - x) % 3;
    }
    cout << c + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

// char f[1000000] = {0};

int main() {
  int n, k, a, b;
  char *f = new char[10000000];

  // while( cin >> n , n ){
  //	char* f = new char[n*n];
  cin >> n;
  for (int i = 0; i <= n / 2; i++) {
    for (int j = 0; j < (n - 2 * i); j++) {
      char c = i % 3 + 1;
      f[(n * i + i) + j] = c;
      f[(n * (n - i - 1) + i) + j] = c;
      f[(n * i + i) + j * n] = c;
      f[n * i + (n - i - 1) + j * n] = c;
    }
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    cout << (int)f[(b - 1) * n + (a - 1)] << endl;
  }
  //}
  delete[] f;
}
Problem p00479 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  while (k--) {
    int x, y;
    cin >> x >> y;
    int w = min(x, n - x + 1);
    int h = min(y, n - y + 1);

    if (h > w) {
      cout << (w - 1) % 3 + 1 << endl;
    } else {
      cout << (h - 1) % 3 + 1 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;

  while (1) {
    int x, y;
    cin >> x >> y;
    k--;
    int w = min(x, n - x + 1);
    int h = min(y, n - y + 1);

    if (h > w) {
      cout << (w - 1) % 3 + 1 << endl;
    } else {
      cout << (h - 1) % 3 + 1 << endl;
    }
  }
  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  REP(i, k) {
    int x, y;
    cin >> x >> y;
    if (x > (n + 1) / 2 && y > (n + 1) / 2) {
      int tx, ty;
      tx = (n + 1) - x;
      ty = (n + 1) - y;
      x = tx;
      y = ty;
    } else if (x > (n + 1) / 2) {
      x = (n + 1) - x;
    } else if (y > (n + 1) / 2) {
      y = (n + 1) - y;
    }
    if (x == 0)
      x = 1;
    if (y == 0)
      y = 1;
    //	printf("x = %d y = %d\\n",x,y);
    int ans = 0;
    int d = x - y;
    ans = y;
    if (d <= 0) {
      ans += d;
    } else {
      if (y == 0 || (y != 0 && n - 2 * (y - 1) >= d)) {
      } else {
        ans += d;
      }
    }
    ans = (ans % 3);
    if (ans == 0)
      ans = 3;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> ti(n, vector<int>(n, 0));
  REP(i, k) {
    int x, y;
    bool flg = false;
    cin >> x >> y;
    if (x > (n + 1) / 2 && y > (n + 1) / 2) {
      flg = true;
      int tx, ty;
      tx = (n + 1) - x;
      ty = (n + 1) - y;
      x = tx;
      y = ty;
    } else if (x > (n + 1) / 2) {
      flg = true;
      x = (n + 1) - x;
    } else if (y > (n + 1) / 2) {
      // flg = true;
      y = (n + 1) - y;
    }
    if (x == 0)
      x = 1;
    if (y == 0)
      y = 1;
    //	printf("x = %d y = %d\\n",x,y);
    int ans = 0;
    int d = x - y;
    // if(flg) d *= -1;
    ans = y;
    if (d <= 0) {
      ans += d;
    } else {
      if (y == 0 || (y != 0 && n - 2 * (y - 1) >= d)) {
      } else {
        ans += d;
      }
    }
    ans = (ans % 3);
    if (ans == 0)
      ans = 3;
    cout << ans << endl;
  }
  return 0;
}
Problem p00479 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int n, k;
  cin >> n >> k;

  while (k--) {
    int x, y, ans;
    cin >> x >> y;
    --x;
    --y;

    if (n & 2) {
      if (x > n / 2)
        x = n - x - 1;
      if (y > n / 2)
        y = n - y - 1;
    } else {
      if (x >= n / 2)
        x = n - x - 1;
      if (y >= n / 2)
        y = n - y - 1;
    }

    if (x <= y)
      ans = x % 3 + 1;
    else
      ans = y % 3 + 1;

    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int tile[500000001];

int main() {
  int n, k;
  cin >> n >> k;

  int color = 0;
  REP(i, n / 2 + 1) tile[i] = (color = color++ % 3) + 1;

  while (k--) {
    int x, y, ans;
    cin >> x >> y;
    --x;
    --y;

    if (n & 2) {
      if (x > n / 2)
        x = n - x - 1;
      if (y > n / 2)
        y = n - y - 1;
    } else {
      if (x >= n / 2)
        x = n - x - 1;
      if (y >= n / 2)
        y = n - y - 1;
    }

    if (x <= y)
      ans = tile[x];
    else
      ans = tile[y];

    cout << ans << endl;
  }
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#define cou cnt
using namespace std;

int N, num[100], ans;
long long memo[100][21];

long long func(int n, int s) {
  long long cou = 0;

  if (s < 0 || s > 20)
    return 0;
  if (memo[n][s] != -1)
    return memo[n][s];
  if (n == N - 1) {
    if (s == ans)
      return 1;
    return 0;
  }
  cou += func(n + 1, s + num[n]);
  if (n != 0) {
    cou += func(n + 1, s - num[n]);
  }
  memo[n][s] = cou;
  return cou;
}
int main() {
  cin >> N;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= 20; j++) {
      memo[i][j] = -1;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    cin >> num[i];
  }
  cin >> ans;
  cout << func(0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int N, num[100], ans, cou = 0;
void func(int n, int s) {
  if (s < 0 || s > 20)
    return;
  if (n == N - 1) {
    if (s == ans)
      cou++;
    return;
  }
  func(n + 1, s + num[n]);
  func(n + 1, s - num[n]);
  return;
}
int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> num[i];
  }
  cin >> ans;
  func(0, 0);
  cout << cou << endl;
}