alexjercan/bugnet huggingface dataset:
Problem p02383 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
string str, ln;
int die[6] = {0};
int i, n, v;
int DICE() {
  for (i = 0; i < str.size(); i++) {
    if (str[i] == 'E') {
      v = die[0];
      die[0] = die[3];
      die[3] = die[5];
      die[5] = die[2];
      die[2] = v;
    } else if (str[i] == 'W') {
      v = die[0];
      die[0] = die[2];
      die[2] = die[5];
      die[5] = die[3];
      die[3] = v;
    } else if (str[i] == 'N') {
      v = die[0];
      die[0] = die[1];
      die[1] = die[5];
      die[5] = die[4];
      die[4] = v;
    } else if (str[i] == 'S') {
      v = die[0];
      die[0] = die[4];
      die[4] = die[5];
      die[5] = die[1];
      die[1] = v;
    }
  }
  return die[0];
}
int main() {
  for (i = 0; i < 6; i++) {
    cin >> die[i];
  }
  getline(cin, ln);
  getline(cin, str);
  DICE();
  cout << die[0] << endl;
  return 0;
}

Error: WA

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
string str;
int die[6] = {0};
int i, n, v;
int DICE() {
  for (i = 0; i < str.size(); i++) {
    if (str[i] == 'E') {
      v = die[0];
      die[0] = die[2];
      die[2] = die[5];
      die[5] = die[3];
      die[3] = v;
    } else if (str[i] == 'W') {
      v = die[0];
      die[0] = die[3];
      die[3] = die[5];
      die[5] = die[2];
      die[2] = v;
    } else if (str[i] == 'N') {
      v = die[0];
      die[0] = die[1];
      die[1] = die[5];
      die[5] = die[4];
      die[4] = v;
    } else if (str[i] == 'S') {
      v = die[0];
      die[0] = die[4];
      die[4] = die[5];
      die[5] = die[1];
      die[1] = v;
    }
  }
  return die[0];
}
int main() {
  for (i = 0; i < 6; i++) {
    cin >> die[6];
  }
  getline(cin, str);
  DICE();
  cout << die[0] << endl;
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct dice {
  int num[6];
};

int main() {
  int n;
  dice one;
  for (n = 0; n < 6; n++) {
    scanf("%d", &one.num[n]);
  }
  int turn, tmp, i = 2;
  scanf("%d", &turn);
  int top, front;
  for (n = 0; n < turn; n++) {
    scanf("%d %d", &top, &front);
    while (1) {
      if (one.num[0] == top)
        break;
      tmp = one.num[0];
      one.num[0] = one.num[1];
      one.num[1] = one.num[5];
      one.num[5] = one.num[4];
      one.num[4] = tmp;
      if (one.num[0] == top)
        break;
      tmp = one.num[0];
      one.num[0] = one.num[1];
      one.num[1] = one.num[5];
      one.num[5] = one.num[4];
      one.num[4] = tmp;
      if (one.num[0] == top)
        break;
      tmp = one.num[0];
      one.num[0] = one.num[3];
      one.num[3] = one.num[5];
      one.num[5] = one.num[2];
      one.num[2] = tmp;
      if (one.num[0] == top)
        break;
      tmp = one.num[0];
      one.num[0] = one.num[3];
      one.num[3] = one.num[5];
      one.num[5] = one.num[2];
      one.num[2] = tmp;
    }
    while (one.num[1] != front) {
      tmp = one.num[1];
      one.num[1] = one.num[2];
      one.num[2] = one.num[4];
      one.num[4] = one.num[3];
      one.num[3] = tmp;
    }
    printf("%d\\n", one.num[2]);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct dice {
  int num[6];
};

int main() {
  int n;
  dice one;
  for (n = 0; n < 6; n++) {
    scanf("%d", &one.num[n]);
  }
  int turn, tmp, i = 2;
  scanf("%d", &turn);
  int top, front;
  for (n = 0; n < turn; n++) {
    scanf("%d %d", &top, &front);
    while (one.num[0] != top) {
      tmp = one.num[0];
      one.num[0] = one.num[1];
      one.num[1] = one.num[5];
      one.num[5] = one.num[4];
      one.num[4] = tmp;
      tmp = one.num[0];
      one.num[0] = one.num[1];
      one.num[1] = one.num[5];
      one.num[5] = one.num[4];
      one.num[4] = tmp;
      tmp = one.num[0];
      one.num[0] = one.num[3];
      one.num[3] = one.num[5];
      one.num[5] = one.num[2];
      one.num[2] = tmp;
      tmp = one.num[0];
      one.num[0] = one.num[3];
      one.num[3] = one.num[5];
      one.num[5] = one.num[2];
      one.num[2] = tmp;
    }
    while (one.num[1] != front) {
      tmp = one.num[1];
      one.num[1] = one.num[2];
      one.num[2] = one.num[4];
      one.num[4] = one.num[3];
      one.num[3] = tmp;
    }
    printf("%d\\n", one.num[2]);
  }
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
using namespace std;

void moveE(int dice[]);
void moveN(int dice[]);
void moveS(int dice[]);
void moveW(int dice[]);
void moveR(int dice[]);

int main(void) {
  int dice[6];
  for (int i = 0; i < 6; i++)
    cin >> dice[i];

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int num0, num1;
    cin >> num0 >> num1;

    int cnt = 0;
    while (dice[0] != num0) {
      if (cnt < 3)
        moveE(dice);
      else
        moveN(dice);
      cnt++;
    }
    while (dice[1] != num1)
      moveR(dice);

    cout << dice[2] << endl;
  }

  return 0;
}

void moveE(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[3];
  dice[3] = dice[5];
  dice[5] = dice[2];
  dice[2] = temp;
}

void moveN(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[1];
  dice[1] = dice[5];
  dice[5] = dice[4];
  dice[4] = temp;
}

void moveS(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[4];
  dice[4] = dice[5];
  dice[5] = dice[1];
  dice[1] = temp;
}

void moveW(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[2];
  dice[2] = dice[5];
  dice[5] = dice[3];
  dice[3] = temp;
}

void moveR(int dice[]) {
  int temp = dice[1];
  dice[1] = dice[2];
  dice[2] = dice[4];
  dice[4] = dice[3];
  dice[3] = temp;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

void moveE(int dice[]);
void moveN(int dice[]);
void moveS(int dice[]);
void moveW(int dice[]);
void moveR(int dice[]);

int main(void) {
  int dice[6];
  for (int i = 0; i < 6; i++)
    cin >> dice[i];

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int num0, num1;
    cin >> num0 >> num1;

    int cnt = 0;
    while (dice[0] != num0) {
      if (cnt <= 3)
        moveE(dice);
      else
        moveN(dice);
      cnt++;
    }
    while (dice[1] != num1)
      moveR;

    cout << dice[2] << endl;
  }

  return 0;
}

void moveE(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[3];
  dice[3] = dice[5];
  dice[5] = dice[2];
  dice[2] = temp;
}

void moveN(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[1];
  dice[1] = dice[5];
  dice[5] = dice[4];
  dice[4] = temp;
}

void moveS(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[4];
  dice[4] = dice[5];
  dice[5] = dice[1];
  dice[1] = temp;
}

void moveW(int dice[]) {
  int temp = dice[0];
  dice[0] = dice[2];
  dice[2] = dice[5];
  dice[5] = dice[3];
  dice[3] = temp;
}

void moveR(int dice[]) {
  int temp = dice[1];
  dice[1] = dice[2];
  dice[2] = dice[4];
  dice[4] = dice[3];
  dice[3] = temp;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

class Dice {
  int _num[6];

public:
  void set(int num[6]) {
    for (int i = 0; i < 6; i++)
      _num[i] = num[i];
  }
  void N() {
    swap(_num[0], _num[4]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[1]);
  }
  void S() {
    swap(_num[0], _num[1]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[4]);
  }
  void W() {
    swap(_num[0], _num[3]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[2]);
  }
  void E() {
    swap(_num[0], _num[2]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[3]);
  }
  void printTop() { cout << _num[0] << endl; }
  void printRight() { cout << _num[2] << endl; }
  void SetTop(int m) {
    if (_num[2] == m)
      W();
    else if (_num[3] == m)
      E();
    else {
      while (1) {
        if (_num[0] == m)
          break;
        S();
      }
    }
  }
  void SetFront(int m) {
    while (1) {
      if (_num[1] == m)
        break;
      S();
      W();
      N();
    }
  }
};

int main() {
  int num[6];
  for (int i = 0; i < 6; i++)
    cin >> num[i];

  Dice d;
  d.set(num);

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int top, front;
    cin >> top >> front;

    d.SetTop(top);
    d.SetFront(front);

    d.printRight();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class Dice {
  int _num[6];

public:
  void set(int num[6]) {
    for (int i = 0; i < 6; i++)
      _num[i] = num[i];
  }
  void N() {
    swap(_num[0], _num[4]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[1]);
  }
  void S() {
    swap(_num[0], _num[1]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[4]);
  }
  void W() {
    swap(_num[0], _num[3]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[2]);
  }
  void E() {
    swap(_num[0], _num[2]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[3]);
  }
  void printTop() { cout << _num[0] << endl; }
  void printRight() { cout << _num[2] << endl; }
  void SetTop(int n) {
    if (_num[2] == n)
      W();
    else if (_num[3] == n)
      E();
    else {
      while (1) {
        if (_num[0] == 0)
          break;
        S();
      }
    }
  }
  void SetFront(int n) {
    while (1) {
      if (_num[1] == n)
        break;
      S();
      W();
      N();
    }
  }
};

int main() {
  int num[6];
  for (int i = 0; i < 6; i++)
    cin >> num[i];

  Dice d;
  d.set(num);

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int top, front;
    cin >> top >> front;

    d.SetTop(top);
    d.SetFront(front);

    d.printRight();
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;

class Dice_c {

private:
  int me[4];
  int topbot[2]; // top,bottom

public:
  void Input();
  void Roll(int top, int front);
  void Output();
};

int main() {

  Dice_c dice;
  int m;
  int top, front;

  dice.Input();
  cin >> m;

  for (int i = 0; i < m; i++) {
    cin >> top >> front;
    dice.Roll(top, front);
    dice.Output();
  }

  return 0;
}

///////////////////////////////////////

void Dice_c::Input() {

  cin >> me[1] >> topbot[1] >> me[2] >> me[0] >> topbot[0] >> me[3];
  getchar();
}

void Dice_c::Roll(int top, int front) {

  int tmp;

  if (me[1] != top) {

    if (me[0] == top) {
      tmp = me[3];
      me[3] = me[2], me[2] = me[1], me[1] = me[0], me[0] = tmp;
    } else if (me[2] == top) {
      tmp = me[0];
      me[0] = me[1], me[1] = me[2], me[2] = me[3], me[3] = tmp;
    } else {
      while (me[1] != top) {
        tmp = topbot[0];
        topbot[0] = me[1], me[1] = topbot[1], topbot[1] = me[3], me[3] = tmp;

        /*
        cout << endl << "top:" <<top << endl;

        cout <<endl <<  "  " << topbot[0] << endl;
        for(int i=0; i<4; i++)
          cout << me[i] << ' ';
        cout << endl << "  " << topbot[1] << endl;
        */
      }
    }
  }
  ///////fix top

  while (topbot[1] != front) {
    tmp = topbot[1];
    topbot[1] = me[2], me[2] = topbot[0], topbot[0] = me[0], me[0] = tmp;
  }
  ///////fix front
}

void Dice_c::Output() { cout << me[2] << endl; }

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

class Dice_c {

private:
  int me[4];
  int topbot[2]; // top,bottom

public:
  void Input();
  void Roll(int top, int front);
  void Output();
};

int main() {

  Dice_c dice;
  int m;
  int top, front;

  dice.Input();
  cin >> m;

  for (int i = 0; i < m; i++) {
    cin >> top >> front;
    dice.Roll(top, front);
    dice.Output();
  }

  return 0;
}

///////////////////////////////////////

void Dice_c::Input() {

  cin >> me[1] >> topbot[1] >> me[2] >> me[0] >> topbot[0] >> me[3];
  getchar();
}

void Dice_c::Roll(int top, int front) {

  int tmp;

  if (me[1] == top) {
    return;
  } else {

    if (me[1] == top) {
      tmp = me[3];
      me[3] = me[2], me[2] = me[1], me[1] = me[0], me[0] = tmp;
    } else if (me[2] == top) {
      tmp = me[0];
      me[0] = me[1], me[1] = me[2], me[2] = me[3], me[3] = tmp;
    } else {
      while (me[1] != top) {
        tmp = topbot[0];
        topbot[0] = me[1], me[1] = topbot[1], topbot[1] = me[3], me[3] = tmp;
      }
    }
  }
  ///////fix top

  while (topbot[1] != front) {
    tmp = topbot[1];
    topbot[1] = me[2], me[2] = topbot[0], topbot[0] = me[0], me[0] = tmp;
  }
  ///////fix front
}

void Dice_c::Output() { cout << me[2] << endl; }
Problem p02384 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define cpy(a, b) tront[a - 1] = temp[b - 1]

class dice {
public:
  int tront[6];
  dice() {
    for (int i = 0; i < 6; i++) {
      cin >> tront[i];
    }
  }

  void rotation(char c) {
    int temp[6], i;
    for (i = 0; i < 6; i++)
      temp[i] = tront[i];
    switch (c) {
    case 'E':
      cpy(1, 4);
      cpy(4, 6);
      cpy(6, 3);
      cpy(3, 1);
      break;
    case 'N':
      cpy(5, 1);
      cpy(1, 2);
      cpy(2, 6);
      cpy(6, 5);
      break;
    case 'S':
      cpy(2, 1);
      cpy(1, 5);
      cpy(5, 6);
      cpy(6, 2);
      break;
    case 'W':
      cpy(1, 3);
      cpy(3, 6);
      cpy(6, 4);
      cpy(4, 1);
      break;
    }
  }

  void print(int x) { cout << tront[x - 1] << endl; }
};

int main() {
  dice d;
  int i, j, q, a, b, m, n;
  int ans[7][7] = {0};
  ans[5][4] = 1;
  ans[4][2] = 1;
  ans[2][3] = 1;
  ans[3][5] = 1;
  ans[1][4] = 2;
  ans[4][6] = 2;
  ans[6][3] = 2;
  ans[3][1] = 2;
  ans[1][2] = 3;
  ans[2][6] = 3;
  ans[6][5] = 3;
  ans[5][1] = 3;
  ans[1][5] = 4;
  ans[5][6] = 4;
  ans[6][2] = 4;
  ans[2][1] = 4;
  ans[1][3] = 5;
  ans[3][6] = 5;
  ans[6][4] = 5;
  ans[4][1] = 5;
  ans[3][2] = 6;
  ans[2][4] = 6;
  ans[4][5] = 6;
  ans[5][3] = 6;
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> a >> b;
    for (j = 0; j < 6; j++) {
      if (a == d.tront[j])
        m = j + 1;
      if (b == d.tront[j])
        n = j + 1;
    }
    d.print(ans[m][n]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define cpy(a, b) tront[a - 1] = temp[b - 1]

class dice {
public:
  int tront[6];
  dice() {
    for (int i = 0; i < 6; i++) {
      cin >> tront[i];
    }
  }

  void rotation(char c) {
    int temp[6], i;
    for (i = 0; i < 6; i++)
      temp[i] = tront[i];
    switch (c) {
    case 'E':
      cpy(1, 4);
      cpy(4, 6);
      cpy(6, 3);
      cpy(3, 1);
      break;
    case 'N':
      cpy(5, 1);
      cpy(1, 2);
      cpy(2, 6);
      cpy(6, 5);
      break;
    case 'S':
      cpy(2, 1);
      cpy(1, 5);
      cpy(5, 6);
      cpy(6, 2);
      break;
    case 'W':
      cpy(1, 3);
      cpy(3, 6);
      cpy(6, 4);
      cpy(4, 1);
      break;
    }
  }

  void print(int x) { cout << tront[x - 1] << endl; }
};

int main() {
  dice d;
  int i, n, a, b;
  int ans[7][7] = {0};
  ans[5][4] = 1;
  ans[4][2] = 1;
  ans[2][3] = 1;
  ans[3][5] = 1;
  ans[1][4] = 2;
  ans[4][6] = 2;
  ans[6][3] = 2;
  ans[3][1] = 2;
  ans[1][2] = 3;
  ans[2][6] = 3;
  ans[6][5] = 3;
  ans[5][1] = 3;
  ans[1][5] = 4;
  ans[5][6] = 4;
  ans[6][2] = 4;
  ans[2][1] = 4;
  ans[1][3] = 5;
  ans[3][6] = 5;
  ans[6][4] = 5;
  ans[4][1] = 5;
  ans[3][2] = 6;
  ans[2][4] = 6;
  ans[4][5] = 6;
  ans[5][3] = 6;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    d.print(ans[a][b]);
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int Dice[6];

void shake(char c) {
  if (c == 'E') {
    swap(Dice[0], Dice[3]);
    swap(Dice[3], Dice[5]);
    swap(Dice[5], Dice[2]);
  }
  if (c == 'N') {
    swap(Dice[0], Dice[1]);
    swap(Dice[1], Dice[5]);
    swap(Dice[5], Dice[4]);
  }
  if (c == 'S') {
    swap(Dice[1], Dice[0]);
    swap(Dice[0], Dice[4]);
    swap(Dice[4], Dice[5]);
  }
  if (c == 'W') {
    swap(Dice[3], Dice[0]);
    swap(Dice[0], Dice[2]);
    swap(Dice[2], Dice[5]);
  }
}
int main() {
  int q, i, j, k, a, b;
  char direction[10] = {"NNNNWNNNN"};
  for (i = 0; i < 6; i++)
    cin >> Dice[i];
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> a >> b;
    for (j = 0; j < 10; j++) {
      if (Dice[1] == b)
        break;
      shake(direction[j]);
    }
    for (k = 0; k < 4; k++) {
      if (Dice[0] == a) {
        cout << Dice[2] << endl;
        break;
      }
      shake('W');
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int Dice[6];

void shake(char c) {
  if (c == 'E') {
    swap(Dice[0], Dice[3]);
    swap(Dice[3], Dice[5]);
    swap(Dice[5], Dice[2]);
  }
  if (c == 'N') {
    swap(Dice[0], Dice[1]);
    swap(Dice[1], Dice[5]);
    swap(Dice[5], Dice[4]);
  }
  if (c == 'S') {
    swap(Dice[1], Dice[0]);
    swap(Dice[0], Dice[4]);
    swap(Dice[4], Dice[5]);
  }
  if (c == 'W') {
    swap(Dice[3], Dice[0]);
    swap(Dice[0], Dice[2]);
    swap(Dice[2], Dice[5]);
  }
}
int main() {
  int q, i, a, b;
  char direction[10] = {"NNNNWNNNN"};
  for (i = 0; i < 6; i++)
    cin >> Dice[i];
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> a >> b;
    for (i = 0; i < 10; i++) {
      if (Dice[1] == b)
        break;
      shake(direction[i]);
    }
    for (i = 0; i < 4; i++) {
      if (Dice[0] == a) {
        cout << Dice[2] << endl;
        break;
      }
      shake('W');
    }
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int d[6], q, a, b, tmp, j;

  for (int i = 0; i < 6; i++) {
    cin >> d[i];
  }

  cin >> q;

  for (int i = 0; i < q; i++) {
    j = 0;
    cin >> a >> b;
    while (1) {
      if (d[1] == b)
        break;
      tmp = d[0];
      d[0] = d[1];
      d[1] = d[5];
      d[5] = d[4];
      d[4] = tmp;
      if (j == 4) {
        tmp = d[0];
        d[0] = d[3];
        d[3] = d[5];
        d[5] = d[2];
        d[2] = tmp;
      }
      j++;
    }
    while (1) {
      if (d[0] == a)
        break;
      tmp = d[0];
      d[0] = d[2];
      d[2] = d[5];
      d[5] = d[3];
      d[3] = tmp;
    }

    cout << d[2] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int d[6], q, a, b, tmp, j;

  for (int i = 0; i < 6; i++) {
    cin >> d[i];
  }

  cin >> q;

  for (int i = 0; i < q; i++) {
    j = 0;
    cin >> a >> b;
    while (1) {
      if (d[1] == b)
        break;
      tmp = d[0];
      d[0] = d[3];
      d[3] = d[5];
      d[5] = d[2];
      d[2] = tmp;
      if (j == 4) {
        tmp = d[0];
        d[0] = d[1];
        d[1] = d[5];
        d[5] = d[4];
        d[4] = tmp;
      }
      j++;
    }
    while (1) {
      if (d[0] == a)
        break;
      tmp = d[0];
      d[0] = d[2];
      d[2] = d[5];
      d[5] = d[3];
      d[3] = tmp;
    }

    cout << d[2] << endl;
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define rep2(x, from, to) for (int x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
int main() {
  int a[6], head, down;
  rep(i, 6) { cin >> a[i]; }
  int s[6][6];
  s[0][0] = -1, s[0][1] = 2, s[0][2] = 4, s[0][3] = 1, s[0][4] = 3,
  s[0][5] = -1;
  s[1][0] = 3, s[1][1] = -1, s[1][2] = 0, s[1][3] = 5, s[1][4] = -1,
  s[1][5] = 2;
  s[2][0] = 1, s[2][1] = 5, s[2][2] = -1, s[2][3] = -1, s[2][4] = 0,
  s[2][5] = 4;
  rep2(i, 3, 6) {
    rep(j, 6) { s[i][j] = s[5 - i][5 - j]; }
  }
  int q;
  cin >> q;
  rep(i, q) {
    cin >> head >> down;
    int b, c;
    rep(i, 6) {
      if (a[i] == head)
        b = i;
      else if (a[i] == down)
        c = i;
    }
    cout << a[s[b][c]] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define rep2(x, from, to) for (int x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
int main() {
  int a[6], head, down;
  rep(i, 6) { cin >> a[i]; }
  int s[6][6];
  s[0][0] = -1, s[0][1] = 2, s[0][2] = 4, s[0][3] = 1, s[0][4] = 3,
  s[0][5] = -1;
  s[1][0] = 3, s[1][1] = -1, s[1][2] = 0, s[1][3] = 5, s[1][4] = -1,
  s[1][5] = 2;
  s[2][0] = 1, s[2][1] = 5, s[2][2] = -1, s[2][3] = -1, s[2][4] = 0,
  s[2][5] = 4;
  rep2(i, 3, 6) {
    rep(j, 6) { s[i][j] = s[5 - i][5 - j]; }
  }
  int q;
  cin >> q;
  rep(i, q) {
    cin >> head >> down;
    cout << a[s[head - 1][down - 1]] << endl;
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

class Dice {
public:
  int n1, n2, n3, n4, n5, n6;

  void East() {
    int t = n1;
    n1 = n4;
    n4 = n6;
    n6 = n3;
    n3 = t;
  }
  void West() {
    int t = n1;
    n1 = n3;
    n3 = n6;
    n6 = n4;
    n4 = t;
  }
  void South() {
    int t = n1;
    n1 = n5;
    n5 = n6;
    n6 = n2;
    n2 = t;
  }
  void North() {
    int t = n1;
    n1 = n2;
    n2 = n6;
    n6 = n5;
    n5 = t;
  }
};

int main() {
  Dice dice;
  int n1, n2, n3, n4, n5, n6;
  int q;
  cin >> n1 >> n2 >> n3 >> n4 >> n5 >> n6;
  dice.n1 = n1;
  dice.n2 = n2;
  dice.n3 = n3;
  dice.n4 = n4;
  dice.n5 = n5;
  dice.n6 = n6;

  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    if (b == dice.n1) {
      dice.South();
    } else if (b == dice.n3) {
      dice.West();
      dice.South();
    } else if (b == dice.n4) {
      dice.East();
      dice.South();
    } else if (b == dice.n5) {
      dice.South();
      dice.South();
    } else if (b == dice.n6) {
      dice.North();
    }
    if (a == dice.n3) {
      dice.West();
    } else if (a == dice.n4) {
      dice.East();
    } else if (a == dice.n6) {
      dice.East();
      dice.East();
    }
    cout << dice.n3 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class Dice {
public:
  int n1, n2, n3, n4, n5, n6;

  void East() {
    int t = n1;
    n1 = n4;
    n4 = n6;
    n6 = n3;
    n3 = t;
  }
  void West() {
    int t = n1;
    n1 = n3;
    n3 = n6;
    n6 = n4;
    n4 = t;
  }
  void South() {
    int t = n1;
    n1 = n5;
    n5 = n6;
    n6 = n2;
    n2 = t;
  }
  void North() {
    int t = n1;
    n1 = n2;
    n2 = n6;
    n6 = n5;
    n5 = t;
  }
  void Rotation() {
    int t = n2;
    n2 = n3;
    n3 = n5;
    n5 = n4;
    n4 = t;
  }
};

int main() {
  Dice dice;
  int n1, n2, n3, n4, n5, n6;
  int q;
  cin >> n1 >> n2 >> n3 >> n4 >> n5 >> n6;
  dice.n1 = n1;
  dice.n2 = n2;
  dice.n3 = n3;
  dice.n4 = n4;
  dice.n5 = n5;
  dice.n6 = n6;

  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    if (dice.n3 == a | dice.n4 == b) {
      dice.East();
    }
    while (dice.n1 != a) {
      dice.North();
    }
    while (dice.n2 != b) {
      dice.Rotation();
    }
    cout << dice.n3 << endl;
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int a[6], q, s, t, u, v;
const int r[6][6] = {{7, 2, 4, 1, 3, 7}, {3, 7, 0, 5, 7, 2},
                     {1, 5, 7, 7, 0, 4}, {4, 0, 7, 7, 5, 1},
                     {2, 7, 5, 0, 7, 3}, {7, 3, 1, 4, 2, 7}};
int main() {
  for (int i = 0; i < 6; i++)
    scanf("%d", &a[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &s, &t);
    for (int j = 0; j < 6; j++) {
      if (a[j] == s)
        u = j;
      if (a[j] == t)
        v = j;
    }
    printf("%d\\n", a[r[u][v]]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int a[6], q, s, t, u, v;
const int r[6][6] = {{7, 2, 4, 1, 3, 7}, {3, 7, 0, 5, 7, 2},
                     {1, 5, 7, 7, 0, 4}, {4, 0, 7, 7, 5, 1},
                     {2, 7, 5, 0, 7, 3}, {7, 3, 1, 4, 2, 7}};
int main() {
  for (int i = 0; i < 6; i++)
    scanf("%d", &a[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &s, &t);
    for (int j = 0; j, 6; j++) {
      if (a[j] == s)
        u = j;
      if (a[j] == t)
        v = j;
    }
    printf("%d\\n", a[r[u - 1][v - 1]]);
  }
  return 0;
}