Problem p01744 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int leftAns[SIZE], rightAns[SIZE];

void init(int w) {
  int idL = 0;
  int idR = 0;

  if (w % 2 == 0) {
    for (int i = 1; i < w; i += 2) {
      leftAns[idL++] = i;
    }
    for (int i = w - 2; i >= 0; i -= 2) {
      leftAns[idL++] = i;
    }

    for (int i = w - 2; i >= 0; i -= 2) {
      rightAns[idR++] = i;
    }
    for (int i = 1; i < w; i += 2) {
      rightAns[idR++] = i;
    }
  } else {
    for (int i = 1; i < w; i += 2) {
      leftAns[idL++] = i;
    }
    for (int i = w - 1; i >= 0; i -= 2) {
      leftAns[idL++] = i;
    }

    for (int i = w - 1; i >= 0; i -= 2) {
      rightAns[idR++] = i;
    }
    for (int i = 1; i < w; i += 2) {
      rightAns[idR++] = i;
    }
  }
}

int getID(int h, int w, int y, int x) {

  if ((y + x) % 2 == 0) {
    // left
    y -= x;

    y = (y / 2) + (w - 1);

    return leftAns[y % w];
  } else {
    // right
    y -= (w - x - 1);

    if (w % 2 == 0)
      y = (y / 2) + (w - 1);
    else
      y = (y / 2);

    return rightAns[y % w];
  }
}

int main() {
  int s[SIZE], ans[SIZE];
  int h, w, n;
  pair<int, int> query[SIZE];

  scanf("%d%d%d", &h, &w, &n);

  init(w);

  for (int i = 0; i < w; i++)
    s[i] = i;

  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;

    query[i] = {a, b};
  }

  sort(query, query + n);

  for (int i = 0; i < n; i++) {
    swap(s[getID(h, w, query[i].first, query[i].second)],
         s[getID(h, w, query[i].first, query[i].second + 1)]);
  }

  for (int i = 0; i < w; i++) {
    debug(s[i]);
    ans[s[getID(h, w, h, i)]] = i + 1;
  }

  for (int i = 0; i < w; i++) {
    printf("%d\n", ans[i]);
  }

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int leftAns[SIZE], rightAns[SIZE];

void init(int w) {
  int idL = 0;
  int idR = 0;

  if (w % 2 == 0) {
    for (int i = 1; i < w; i += 2) {
      leftAns[idL++] = i;
    }
    for (int i = w - 2; i >= 0; i -= 2) {
      leftAns[idL++] = i;
    }

    for (int i = w - 2; i >= 0; i -= 2) {
      rightAns[idR++] = i;
    }
    for (int i = 1; i < w; i += 2) {
      rightAns[idR++] = i;
    }
  } else {
    for (int i = 1; i < w; i += 2) {
      leftAns[idL++] = i;
    }
    for (int i = w - 1; i >= 0; i -= 2) {
      leftAns[idL++] = i;
    }

    for (int i = w - 1; i >= 0; i -= 2) {
      rightAns[idR++] = i;
    }
    for (int i = 1; i < w; i += 2) {
      rightAns[idR++] = i;
    }
  }
}

int getID(int h, int w, int y, int x) {

  if ((y + x) % 2 == 0) {
    // left
    y -= x;

    y = (y / 2) + (w - 1);

    return leftAns[y % w];
  } else {
    // right
    y -= (w - x - 1);

    if (w % 2 == 0)
      y = (y / 2) + (w - 1);
    else
      y = (y / 2);

    return rightAns[y % w];
  }
}

int main() {
  int s[SIZE], ans[SIZE];
  int h, w, n;
  pair<int, int> query[SIZE];

  scanf("%d%d%d", &h, &w, &n);

  init(w);

  for (int i = 0; i < w; i++)
    s[i] = i;

  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;

    query[i] = {a, b};
  }

  sort(query, query + n);

  for (int i = 0; i < n; i++) {
    swap(s[getID(h, w, query[i].first, query[i].second)],
         s[getID(h, w, query[i].first, query[i].second + 1)]);
  }

  for (int i = 0; i < w; i++) {
    ans[s[getID(h, w, h, i)]] = i + 1;
  }

  for (int i = 0; i < w; i++) {
    printf("%d\n", ans[i]);
  }

  return 0;
}