Problem p02761 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ls(s) (s & (-s))
#define ll long long
#define inf 0x3f3f3f3f
const ll N = 500030;
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const ll mod = 1e9 + 7;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  rep(i, 0, n) v[i] = -1;
  bool b = false;
  vector<pair<int, int>> vp;
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    vp.push_back({x, y});
  }
  sort(vp.begin(), vp.end());
  if (n == 1) {
    if (m == 1) {
      cout << vp[0].se;
      return 0;
    } else {

      rep(i, 1, m) {
        if (vp[i].se != vp[i - 1].se) {

          cout << "-1";
          return 0;
        }
      }
      cout << vp[0].se;
      return 0;
    }
  } else {

    rep(i, 0, m) {
      int x = vp[i].fi;
      int y = vp[i].se;
      if (x == 1) {
        if (y <= 0)
          b = true;
        if (v[x - 1] == -1 || v[x - 1] == y)
          v[x - 1] = y;
        else
          b = true;
      } else {
        if (v[x - 1] == -1 || v[x - 1] == y)
          v[x - 1] = y;
        else
          b = true;
      }
    }
    rep(i, 0, n) {
      if (!i) {
        if (v[i] == -1)
          v[i] = 1;
        if (v[i] <= 0)
          b = true;
      } else {
        if (v[i] == -1)
          v[i] = 0;
      }
    }
    if (b)
      cout << "-1";
    else {
      rep(i, 0, n) cout << v[i];
    }
  }
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ls(s) (s & (-s))
#define ll long long
#define inf 0x3f3f3f3f
const ll N = 500030;
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const ll mod = 1e9 + 7;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  rep(i, 0, n) v[i] = -1;
  bool b = false;
  vector<pair<int, int>> vp;
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    vp.push_back({x, y});
  }
  sort(vp.begin(), vp.end());
  if (n == 1) {
    if (!m) {
      cout << 0;
      return 0;
    } else if (m == 1) {
      cout << vp[0].se;
      return 0;
    } else {

      rep(i, 1, m) {
        if (vp[i].se != vp[i - 1].se) {

          cout << "-1";
          return 0;
        }
      }
      cout << vp[0].se;
      return 0;
    }
  } else {

    rep(i, 0, m) {
      int x = vp[i].fi;
      int y = vp[i].se;
      if (x == 1) {
        if (y <= 0)
          b = true;
        if (v[x - 1] == -1 || v[x - 1] == y)
          v[x - 1] = y;
        else
          b = true;
      } else {
        if (v[x - 1] == -1 || v[x - 1] == y)
          v[x - 1] = y;
        else
          b = true;
      }
    }
    rep(i, 0, n) {
      if (!i) {
        if (v[i] == -1)
          v[i] = 1;
        if (v[i] <= 0)
          b = true;
      } else {
        if (v[i] == -1)
          v[i] = 0;
      }
    }
    if (b)
      cout << "-1";
    else {
      rep(i, 0, n) cout << v[i];
    }
  }
}