Problem p03747 (C++)

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

using namespace std;
int N;
long long L, T;
long long Xs[100008];
int Sols[100008];
int Sols_sorted[100008];
vector<int> cw;
vector<int> ccw;
int cmp(const void *ka, const void *kb) {
  int a = *(int *)ka;
  int b = *(int *)kb;
  return a - b;
}
int main() {
  scanf("%d%lld%lld", &N, &L, &T);
  int cwccw = 1;
  for (int i = 0; i < N; i++) {
    int vb;
    scanf("%lld%d", &Xs[i], &vb);
    if (vb == 1) {
      cw.push_back(i);
    } else {
      ccw.push_back(i);
    }
    if (i == 0 && vb == 2)
      cwccw = 2;
    if (vb == 1) {
      Sols[i] = (Xs[i] + T) % L;
    } else {
      Sols[i] = (L * T - T + Xs[i]) % L;
    }
    Sols_sorted[i] = Sols[i];
  }
  qsort(Sols_sorted, N, sizeof(int), cmp);
  long long minv = 0;
  long long maxv = T * 10;
  while (minv < maxv) {
    long long hf = (minv + maxv + 1) / 2;
    long long n_cw = (hf + 1) / 2;
    long long n_ccw = (hf + 1) / 2;
    if (hf % 2 == 0) {
      if (cwccw == 1)
        n_cw++;
      if (cwccw == 2)
        n_ccw++;
    }
    long long cw_dist = 0;
    long long ccw_dist = 0;
    cw_dist += (n_cw - 1) / cw.size() * L;
    int rem = n_cw - (n_cw - 1) / cw.size() * cw.size();
    if (cw[0] == 0) {
      cw_dist += (L - Xs[cw[(cw.size() - rem + 1) % cw.size()]]) % L;
    } else {
      cw_dist += (L - Xs[cw[(cw.size() - rem) % cw.size()]]) % L;
    }
    ccw_dist += (n_ccw - 1) / ccw.size() * L;
    rem = n_ccw - (n_ccw - 1) / ccw.size() * ccw.size();
    ccw_dist += Xs[ccw[rem - 1]];
    // if(hf==1) printf("%lld %lld\n",cw_dist,ccw_dist);
    if (cw_dist + ccw_dist <= T * 2) {
      minv = hf;
    } else {
      maxv = hf - 1;
    }
  }
  // printf("%lld\n",minv);
  int base;
  long long n_cw = (minv + 1) / 2;
  long long n_ccw = (minv + 1) / 2;
  if (minv % 2 == 0) {
    if (cwccw == 1)
      n_cw++;
    if (cwccw == 2)
      n_ccw++;
  }
  if (minv % 2 == 1 & cwccw == 1 || minv % 2 == 0 & cwccw == 2) {
    // ccw
    int rem = n_ccw - (n_ccw - 1) / ccw.size() * ccw.size();
    int id = ccw[rem - 1];
    for (int i = N - 1; i >= 0; i--) {
      if (Sols_sorted[i] == Sols[id])
        base = i;
    }
  } else {
    // cw
    int rem = n_cw - (n_cw - 1) / cw.size() * cw.size();
    int id;
    if (cw[0] == 0) {
      id = cw[(cw.size() - rem + 1) % cw.size()];
    } else {
      id = cw[(cw.size() - rem) % cw.size()];
    }
    for (int i = 0; i < N; i++) {
      if (Sols_sorted[i] == Sols[id])
        base = i;
    }
  }
  for (int i = base; i < N; i++)
    printf("%d\n", Sols_sorted[i]);
  for (int i = 0; i < base; i++)
    printf("%d\n", Sols_sorted[i]);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;
int N;
long long L, T;
long long Xs[100008];
int Sols[100008];
int Sols_sorted[100008];
vector<int> cw;
vector<int> ccw;
int cmp(const void *ka, const void *kb) {
  int a = *(int *)ka;
  int b = *(int *)kb;
  return a - b;
}
int main() {
  scanf("%d%lld%lld", &N, &L, &T);
  int cwccw = 1;
  for (int i = 0; i < N; i++) {
    int vb;
    scanf("%lld%d", &Xs[i], &vb);
    if (vb == 1) {
      cw.push_back(i);
    } else {
      ccw.push_back(i);
    }
    if (i == 0 && vb == 2)
      cwccw = 2;
    if (vb == 1) {
      Sols[i] = (Xs[i] + T) % L;
    } else {
      Sols[i] = (L * T - T + Xs[i]) % L;
    }
    Sols_sorted[i] = Sols[i];
  }
  qsort(Sols_sorted, N, sizeof(int), cmp);
  if (cw.size() == 0 || ccw.size() == 0) {
    // corner
    int base;
    for (int i = 0; i < N; i++) {
      if (Sols_sorted[i] == Sols[0])
        base = i;
    }
    for (int i = base; i < N; i++)
      printf("%d\n", Sols_sorted[i]);
    for (int i = 0; i < base; i++)
      printf("%d\n", Sols_sorted[i]);
    return 0;
  }
  long long minv = 0;
  long long maxv = T * 10;
  while (minv < maxv) {
    long long hf = (minv + maxv + 1) / 2;
    long long n_cw = (hf + 1) / 2;
    long long n_ccw = (hf + 1) / 2;
    if (hf % 2 == 0) {
      if (cwccw == 1)
        n_cw++;
      if (cwccw == 2)
        n_ccw++;
    }
    long long cw_dist = 0;
    long long ccw_dist = 0;
    cw_dist += (n_cw - 1) / cw.size() * L;
    int rem = n_cw - (n_cw - 1) / cw.size() * cw.size();
    if (cw[0] == 0) {
      cw_dist += (L - Xs[cw[(cw.size() - rem + 1) % cw.size()]]) % L;
    } else {
      cw_dist += (L - Xs[cw[(cw.size() - rem) % cw.size()]]) % L;
    }
    ccw_dist += (n_ccw - 1) / ccw.size() * L;
    rem = n_ccw - (n_ccw - 1) / ccw.size() * ccw.size();
    ccw_dist += Xs[ccw[rem - 1]];
    // if(hf==1) printf("%lld %lld\n",cw_dist,ccw_dist);
    if (cw_dist + ccw_dist <= T * 2) {
      minv = hf;
    } else {
      maxv = hf - 1;
    }
  }
  // printf("%lld\n",minv);
  int base;
  long long n_cw = (minv + 1) / 2;
  long long n_ccw = (minv + 1) / 2;
  if (minv % 2 == 0) {
    if (cwccw == 1)
      n_cw++;
    if (cwccw == 2)
      n_ccw++;
  }
  if (minv % 2 == 1 & cwccw == 1 || minv % 2 == 0 & cwccw == 2) {
    // ccw
    int rem = n_ccw - (n_ccw - 1) / ccw.size() * ccw.size();
    int id = ccw[rem - 1];
    for (int i = N - 1; i >= 0; i--) {
      if (Sols_sorted[i] == Sols[id])
        base = i;
    }
  } else {
    // cw
    int rem = n_cw - (n_cw - 1) / cw.size() * cw.size();
    int id;
    if (cw[0] == 0) {
      id = cw[(cw.size() - rem + 1) % cw.size()];
    } else {
      id = cw[(cw.size() - rem) % cw.size()];
    }
    for (int i = 0; i < N; i++) {
      if (Sols_sorted[i] == Sols[id])
        base = i;
    }
  }
  for (int i = base; i < N; i++)
    printf("%d\n", Sols_sorted[i]);
  for (int i = 0; i < base; i++)
    printf("%d\n", Sols_sorted[i]);
  return 0;
}