Problem p03747 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// c++11
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define mp make_pair
#define mt make_tuple
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

const int INF = 1 << 29;
const double EPS = 1e-9;
const ll MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
ll N, L, T;
vector<pair<ll, int>> ants;
vector<ll> res;
int main() {
  cin >> N >> L >> T;
  ants.resize(N);
  for (auto &ant : ants) {
    cin >> ant.first >> ant.second;
    if (ant.second == 1) {
      res.push_back((ant.first + T) % L);
    } else {
      res.push_back(((ant.first - T) % L + L) % L);
    }
  }
  ll cnt = 0;
  for (int i = 0; i < ants.size(); i++) {
    if (ants[i].second == 1) {
      cnt += (ants[i].first + T) / L;
    } else {
      cnt -= (T - ants[i].first + (L - 1)) / L;
    }
  }
  sort(res.begin(), res.end());
  cerr << cnt << endl;
  for (int i = 0; i < N; i++) {
    cout << res[(i + cnt + N) % N] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// c++11
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define mp make_pair
#define mt make_tuple
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

const int INF = 1 << 29;
const double EPS = 1e-9;
const ll MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
ll N, L, T;
vector<pair<ll, int>> ants;
vector<ll> res;
int main() {
  cin >> N >> L >> T;
  ants.resize(N);
  for (auto &ant : ants) {
    cin >> ant.first >> ant.second;
    if (ant.second == 1) {
      res.push_back((ant.first + T) % L);
    } else {
      res.push_back(((ant.first - T) % L + L) % L);
    }
  }
  ll cnt = 0;
  for (int i = 0; i < ants.size(); i++) {
    if (ants[i].second == 1) {
      cnt += (ants[i].first + T) / L;
    } else {
      cnt -= (T - ants[i].first + (L - 1)) / L;
    }
  }
  cnt %= N;
  sort(res.begin(), res.end());
  cerr << cnt << endl;
  for (int i = 0; i < N; i++) {
    cout << res[(i + cnt + N) % N] << endl;
  }
  return 0;
}