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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 200001

struct Info {
  bool operator<(const struct Info &arg) const {
    if (row != arg.row) {
      return row < arg.row;
    } else {
      return col < arg.col;
    }
  };
  int row, col, left_first_pos, right_first_pos;
};

int H, W, N;
int top_array[NUM], under_array[NUM], loc[NUM];

int calc_final_loc(int row, int col) {

  int tmp_row = row, tmp_col = col;
  tmp_row += (2 * W) * ((H - row) / (2 * W));

  int to_right, to_left, rest_h;

  while (tmp_row <= H) {

    rest_h = H - tmp_row;

    if (tmp_row % 2 == 1) {

      if (tmp_col % 2 == 1) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row += (to_right - 1) + 2;

        } else {
          tmp_col += (rest_h + 1);
          break;
        }
      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row += (to_left - 1) + 2;

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    } else {

      if (tmp_col % 2 == 0) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row += (to_right - 1) + 2;

        } else {

          tmp_col += (rest_h + 1);
          break;
        }

      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row += (to_left - 1) + 2;

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    }
  }

  return tmp_col;
}

int calc_first_loc(int row, int col) {

  int tmp_row = row, tmp_col = col;
  tmp_row -= (2 * W) * ((row - 1) / (2 * W));

  int to_right, to_left, rest_h;

  while (tmp_row >= 1) {

    rest_h = tmp_row - 1;

    if (tmp_row % 2 == 1) {

      if (tmp_col % 2 == 1) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row -= ((to_right - 1) + 2);

        } else {
          tmp_col += (rest_h + 1);
          break;
        }
      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row -= ((to_left - 1) + 2);

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    } else {

      if (tmp_col % 2 == 0) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row -= ((to_right - 1) + 2);

        } else {

          tmp_col += (rest_h + 1);
          break;
        }

      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row -= ((to_left - 1) + 2);

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    }
  }

  return tmp_col;
}

void calc_first() {

  int final_loc;
  for (int i = 1; i <= W; i++) {
    final_loc = calc_final_loc(1, i);
    top_array[i] = i;
    under_array[final_loc] = i;
    loc[i] = final_loc;
  }
}

int main() {

  scanf("%d %d %d", &H, &W, &N);

  Info *info = (Info *)malloc(sizeof(Info) * N);

  calc_first();

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &info[i].row, &info[i].col);
  }

  sort(info, info + N);

  int left_top_loc, right_top_loc, left_under_loc, right_under_loc;

  for (int i = 0; i < N; i++) {

    left_top_loc = calc_first_loc(info[i].row, info[i].col);
    right_top_loc = calc_first_loc(info[i].row, info[i].col + 1);

    left_under_loc = loc[top_array[left_top_loc]];
    right_under_loc = loc[top_array[right_top_loc]];

    swap(under_array[left_under_loc], under_array[right_under_loc]);
    loc[under_array[left_under_loc]] = left_under_loc;
    loc[under_array[right_under_loc]] = right_under_loc;

    swap(top_array[left_top_loc], top_array[right_top_loc]);
  }

  for (int i = 1; i <= W; i++) {
    loc[under_array[i]] = i;
  }

  for (int i = 1; i <= W; i++) {
    printf("%d\\n", loc[i]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 200001

struct Info {
  bool operator<(const struct Info &arg) const {
    if (row != arg.row) {
      return row < arg.row;
    } else {
      return col < arg.col;
    }
  };
  int row, col, left_first_pos, right_first_pos;
};

int H, W, N;
int top_array[NUM], under_array[NUM], loc[NUM];

int calc_final_loc(int row, int col) {

  int tmp_row = row, tmp_col = col;
  int to_right, to_left, rest_h;

  while (tmp_row <= H) {

    rest_h = H - tmp_row;

    if (tmp_row % 2 == 1) {

      if (tmp_col % 2 == 1) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row += (to_right - 1) + 2;

        } else {
          tmp_col += (rest_h + 1);
          break;
        }
      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row += (to_left - 1) + 2;

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    } else {

      if (tmp_col % 2 == 0) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row += (to_right - 1) + 2;

        } else {

          tmp_col += (rest_h + 1);
          break;
        }

      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row += (to_left - 1) + 2;

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    }
  }
  return tmp_col;
}

int calc_first_loc(int row, int col) {

  int tmp_row = row, tmp_col = col;
  int to_right, to_left, rest_h;

  while (tmp_row >= 1) {

    rest_h = tmp_row - 1;

    if (tmp_row % 2 == 1) {

      if (tmp_col % 2 == 1) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row -= ((to_right - 1) + 2);

        } else {
          tmp_col += (rest_h + 1);
          break;
        }
      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row -= ((to_left - 1) + 2);

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    } else {

      if (tmp_col % 2 == 0) {
        to_right = W - tmp_col;

        if (rest_h >= (to_right - 1)) {

          tmp_col = W;
          tmp_row -= ((to_right - 1) + 2);

        } else {

          tmp_col += (rest_h + 1);
          break;
        }

      } else {
        to_left = tmp_col - 1;

        if (rest_h >= (to_left - 1)) {

          tmp_col = 1;
          tmp_row -= ((to_left - 1) + 2);

        } else {

          tmp_col -= (rest_h + 1);
          break;
        }
      }
    }
  }

  return tmp_col;
}

void calc_first() {

  int final_loc;
  for (int i = 1; i <= W; i++) {
    final_loc = calc_final_loc(1, i);
    top_array[i] = i;
    under_array[final_loc] = i;
    loc[i] = final_loc;
  }
}

int main() {

  scanf("%d %d %d", &H, &W, &N);

  Info *info = (Info *)malloc(sizeof(Info) * N);

  calc_first();

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &info[i].row, &info[i].col);
  }

  sort(info, info + N);

  int left_top_loc, right_top_loc, left_under_loc, right_under_loc;

  for (int i = 0; i < N; i++) {

    left_top_loc = calc_first_loc(info[i].row, info[i].col);
    right_top_loc = calc_first_loc(info[i].row, info[i].col + 1);

    left_under_loc = loc[top_array[left_top_loc]];
    right_under_loc = loc[top_array[right_top_loc]];

    swap(under_array[left_under_loc], under_array[right_under_loc]);
    loc[under_array[left_under_loc]] = left_under_loc;
    loc[under_array[right_under_loc]] = right_under_loc;

    swap(top_array[left_top_loc], top_array[right_top_loc]);
  }

  for (int i = 1; i <= W; i++) {
    loc[under_array[i]] = i;
  }

  for (int i = 1; i <= W; i++) {
    printf("%d\\n", loc[i]);
  }

  return 0;
}