prithivMLmods/Coder-Stat huggingface dataset:
Problem p03036 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int r, d, x, arr[2010];

int main() {
  cin >> r >> d >> x;
  arr[2000] = x;
  for (int i = 2001; i <= 2010; i++) {
    arr[i] = r * arr[i - 1] - d;
    cout << arr[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int r, d, x, arr[2011];

int main() {
  cin >> r >> d >> x;
  arr[2000] = x;
  for (int i = 2001; i <= 2010; i++) {
    arr[i] = r * arr[i - 1] - d;
    cout << arr[i] << endl;
  }
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define ALL(a) (a).begin(), (a).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int MOD = 1000000007;
const double PI = acos(-1); // 3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g(n + 1, 0);
  rep(i, m) {
    int l, r;
    cin >> l >> r;
    r++; // rを含みたいから開区間にするためのインクリメント
    g[l]++;
    g[r]--;
  }

  int CNT = 0;
  rep(i, n) g[i + 1] += g[i];
  // rep(i, g.size())
  // {
  //     COUT(g[i]);
  // }
  int ans = 0;
  rep(i, n + 1) if (g[i] == m) ans++;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define ALL(a) (a).begin(), (a).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int MOD = 1000000007;
const double PI = acos(-1); // 3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g(n + 2, 0);
  rep(i, m) {
    int l, r;
    cin >> l >> r;
    r++; // rを含みたいから開区間にするためのインクリメント
    g[l]++;
    g[r]--;
  }

  int CNT = 0;
  rep(i, n) g[i + 1] += g[i];
  // rep(i, g.size())
  // {
  //     COUT(g[i]);
  // }
  int ans = 0;
  rep(i, n + 1) if (g[i] == m) ans++;
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  for (int i = 0; i < m; i++)
    cin >> l.at(i) >> r.at(i);

  sort(l.begin(), l.end());
  sort(r.begin(), r.end());

  int c;
  c = l.at(m - 1) - r.at(1) + 1;
  if (c < 0)
    c = 0;

  cout << c << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  for (int i = 0; i < m; i++)
    cin >> l.at(i) >> r.at(i);

  sort(l.begin(), l.end());
  sort(r.begin(), r.end());

  int c;
  c = r.at(0) - l.at(m - 1) + 1;
  if (c < 0)
    c = 0;

  cout << c << endl;

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  int L_max = 1, R_min = N;
  int ANS;
  vector<int> L;
  vector<int> R;

  for (int i = 0; i < M; i++) {
    cin >> L.at(i) >> R.at(i);
    if (L.at(i) > L_max) {
      L_max = L.at(i);
    }
    if (R.at(i) < R_min) {
      R_min = R.at(i);
    }
  }

  ANS = R_min - L_max + 1;

  if (ANS < 0) {
    ANS = 0;
  }

  cout << ANS << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  int L_max = 1, R_min = N;
  int ANS;
  vector<int> L(M);
  vector<int> R(M);

  for (int i = 0; i < M; i++) {
    cin >> L.at(i) >> R.at(i);
    if (L.at(i) > L_max) {
      L_max = L.at(i);
    }
    if (R.at(i) < R_min) {
      R_min = R.at(i);
    }
  }

  ANS = R_min - L_max + 1;

  if (ANS < 0) {
    ANS = 0;
  }

  cout << ANS << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, M;
  cin >> N >> M;

  vector<int> card(N + 1);
  for (int i = 0; i < M; i++) {
    int L, R;
    cin >> L >> R;
    card[L]++;
    card[R + 1]--;
  }

  for (int i = 1; i <= N; i++) {
    card[i] = card[i] + card[i - 1];
  }

  int answer = 0;

  for (int i = 1; i <= N; i++) {
    if (card[i] == M) {
      answer++;
    }
  }

  cout << answer << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, M;
  cin >> N >> M;

  vector<int> card(N + 1);
  for (int i = 0; i < M; i++) {
    int L, R;
    cin >> L >> R;
    card[L]++;
    if (R != N) {
      card[R + 1]--;
    }
  }

  for (int i = 1; i <= N; i++) {
    card[i] = card[i] + card[i - 1];
  }

  int answer = 0;

  for (int i = 1; i <= N; i++) {
    if (card[i] == M) {
      answer++;
    }
  }

  cout << answer << endl;

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>

using namespace std;

#define MAX_INT 10000

int array_max(int *arr, int M) {
  int max = 0;
  for (int i = 0; i < M; i++) {
    // printf("%d ",arr[i]);
    if (max < arr[i]) {
      max = arr[i];
    }
  }
  // printf("max: %d, \r\n", max);
  return max;
}

int array_min(int *arr, int M) {
  int min = MAX_INT;
  for (int i = 0; i < M; i++) {
    // printf("%d ",arr[i]);
    if (min > arr[i]) {
      min = arr[i];
    }
  }
  // printf("min: %d \r\n", min);
  return min;
}

int main() {
  int N, M, L_e, R_e;
  int L[MAX_INT], R[MAX_INT];
  int ret;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &L_e, &R_e);
    L[i] = L_e, R[i] = R_e;
  }
  ret = array_min(R, M) - array_max(L, M) + 1;
  printf("%d\r\n", (ret > 0) ? ret : 0);
  fflush(stdout);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>

using namespace std;

#define MAX_INT 100000

int array_max(int *arr, int M) {
  int max = 0;
  for (int i = 0; i < M; i++) {
    // printf("%d ",arr[i]);
    if (max < arr[i]) {
      max = arr[i];
    }
  }
  // printf("max: %d, \r\n", max);
  return max;
}

int array_min(int *arr, int M) {
  int min = MAX_INT;
  for (int i = 0; i < M; i++) {
    // printf("%d ",arr[i]);
    if (min > arr[i]) {
      min = arr[i];
    }
  }
  // printf("min: %d \r\n", min);
  return min;
}

int main() {
  int N, M, L_e, R_e;
  int L[MAX_INT], R[MAX_INT];
  int ret;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &L_e, &R_e);
    L[i] = L_e, R[i] = R_e;
  }
  ret = array_min(R, M) - array_max(L, M) + 1;
  printf("%d\r\n", (ret > 0) ? ret : 0);
  fflush(stdout);
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int arr[100010];

int main() {
  int N, M;
  cin >> N >> M;
  int l[M], r[M];
  for (int i = 0; i < M; i++)
    cin >> l[i] >> r[i];

  for (int i = 0; i < M; i++) {
    arr[l[i]]++;
    arr[r[i] + 1]--;
  }

  for (int i = 1; i < 100010; i++)
    arr[i] += arr[i - 1];

  int ans = 0;
  for (int i = 0; i < 1000010; i++)
    if (arr[i] == M)
      ans++;

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int arr[100010];

int main() {
  int N, M;
  cin >> N >> M;
  int l[M], r[M];
  for (int i = 0; i < M; i++)
    cin >> l[i] >> r[i];

  for (int i = 0; i < M; i++) {
    arr[l[i]]++;
    arr[r[i] + 1]--;
  }

  for (int i = 1; i < 100010; i++)
    arr[i] += arr[i - 1];

  int ans = 0;
  for (int i = 0; i < 100010; i++)
    if (arr[i] == M)
      ans++;

  cout << ans << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> ls, rs;
  rep(i, m) cin >> ls[i] >> rs[i];
  int l = *max_element(ls.begin(), ls.end());
  int r = *min_element(rs.begin(), rs.end());
  if (r < l) {
    cout << 0 << endl;
  } else {
    cout << r - l + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> ls(m), rs(m);
  rep(i, m) cin >> ls[i] >> rs[i];
  int l = *max_element(ls.begin(), ls.end());
  int r = *min_element(rs.begin(), rs.end());
  if (r < l) {
    cout << 0 << endl;
  } else {
    cout << r - l + 1 << endl;
  }
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m, l, r;
  cin >> n >> m;
  vector<int> imos(n + 1, 0);
  for (int i = 0; i < m; ++i) {
    cin >> l >> r;
    imos[l]++;
    imos[r + 1]--;
  }
  int ans = 0;
  for (int i = 1; i < n + 1; ++i) {
    imos[i] += imos[i - 1];
    ans += (imos[i] == m);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m, l, r;
  cin >> n >> m;
  vector<int> imos(n + 2, 0);
  for (int i = 0; i < m; ++i) {
    cin >> l >> r;
    imos[l]++;
    imos[r + 1]--;
  }
  int ans = 0;
  for (int i = 1; i < n + 1; ++i) {
    imos[i] += imos[i - 1];
    ans += (imos[i] == m);
  }
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(n + 1);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    v[l]++;
    v[r + 1]--;
  }
  for (int i = 0; i < n; i++)
    v[i + 1] += v[i];
  int ans = 0;
  for (int i = 0; i < v.size(); i++)
    ans += (v[i] == m);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(n + 2);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    v[l]++;
    v[r + 1]--;
  }
  for (int i = 0; i < n; i++)
    v[i + 1] += v[i];
  int ans = 0;
  for (int i = 0; i < v.size(); i++)
    ans += (v[i] == m);
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> l(m);
  vector<ll> r(m);
  vector<ll> a(n + 1);
  rep(i, m) cin >> l[i] >> r[i];
  rep(i, n) a[i] = 0;
  rep(i, m) {
    for (int j = l[i]; j <= r[i]; j++) {
      a[j]++;
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == m)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> l(m);
  vector<ll> r(m);
  vector<ll> a(n + 1);
  rep(i, m) cin >> l[i] >> r[i];
  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  if (r[0] < l[m - 1])
    cout << "0" << endl;
  else
    cout << r[0] - l[m - 1] + 1 << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
using P = pair<int, int>;
typedef long long int ll;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {
  int n, m;
  cin >> n >> m;
  int maxL = 1;
  int minR = n;
  vector<int> R, L;
  rep(i, n) cin >> L[i] >> R[i];
  rep(i, n) {
    maxL = max(maxL, L[i]);
    minR = min(minR, R[i]);
  }
  int ans = minR - maxL + 1;
  ans = max(ans, 0);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
using P = pair<int, int>;
typedef long long int ll;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {
  int n, m;
  cin >> n >> m;
  int maxL = 1;
  int minR = n;
  vector<int> R(m), L(m);
  rep(i, m) cin >> L[i] >> R[i];
  rep(i, m) {
    maxL = max(maxL, L[i]);
    minR = min(minR, R[i]);
  }
  int ans = minR - maxL + 1;
  ans = max(ans, 0);
  cout << ans << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(M, 0), R(M, 0);
  int maxL = 0, minR = 10000001;
  for (int i = 0; i < M; i++) {
    cin >> L.at(i);
    cin >> R.at(i);
    maxL = max(maxL, L.at(i));
    minR = min(minR, R.at(i));
  }

  int ans = 0;
  for (int i = maxL; i <= minR; i++) {
    bool path = true;
    for (int j = 0; j < M; j++) {
      if (!(L.at(j) <= i && i <= R.at(j)))
        path = false;
    }
    if (path)
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(M, 0), R(M, 0);
  int maxL = 0, minR = 10000001;
  for (int i = 0; i < M; i++) {
    cin >> L.at(i);
    cin >> R.at(i);
    maxL = max(maxL, L.at(i));
    minR = min(minR, R.at(i));
  }

  int ans = minR - maxL + 1;
  if (ans < 0)
    ans = 0;
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

int N, M;

void dump(int max_, int min_) {
  cout << "max:" << max_ << " min:" << min_ << endl;
}

int main(int argc, char **argv) {
  int i;

  cin >> N >> M;
  int L[N], R[N];
  int min_ = 0, max_ = N;

  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    if (min_ < L[i])
      min_ = L[i];
    if (max_ > R[i])
      max_ = R[i];
  }

  int ans;
  if (max_ - min_ < 0)
    ans = 0;
  else
    ans = max_ - min_ + 1;

  // dump(max_, min_);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int N, M;

void dump(int max_, int min_) {
  cout << "max:" << max_ << " min:" << min_ << endl;
}

int main(int argc, char **argv) {
  int i;

  cin >> N >> M;
  int L[M], R[M];
  int min_ = 0, max_ = N;

  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    if (min_ < L[i])
      min_ = L[i];
    if (max_ > R[i])
      max_ = R[i];
  }

  int ans;
  if (max_ - min_ < 0)
    ans = 0;
  else
    ans = max_ - min_ + 1;

  // dump(max_, min_);
  cout << ans << endl;

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main() {
  int a, b, c[10000], d[10000], e;
  e = 0;
  cin >> a >> b;
  for (int i = 0; i < b; i++) {
    cin >> c[i] >> d[i];
  }
  sort(c, c + b, greater<int>());
  sort(d, d + b);
  for (int i = 0; i < 1000000; i++) {
    if (c[0] <= i && i <= d[0]) {
      e++;
    }
  }
  if (a >= e) {
    cout << e;
  } else
    cout << a;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main() {
  int a, b, c[100000], d[100000], e;
  e = 0;
  cin >> a >> b;
  for (int i = 0; i < b; i++) {
    cin >> c[i] >> d[i];
  }
  sort(c, c + b, greater<int>());
  sort(d, d + b);
  for (int i = 0; i < 1000000; i++) {
    if (c[0] <= i && i <= d[0]) {
      e++;
    }
  }
  if (a >= e) {
    cout << e;
  } else
    cout << a;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
using namespace std;
int N;
int M;
int L[10020];
int R[10020];
int lmax, rmin;
int ans;

int main() {
  cin >> N >> M;
  lmax = 0;
  rmin = N;
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    if (lmax < L[i])
      lmax = L[i];
    if (rmin > R[i])
      rmin = R[i];
  }
  ans = rmin - lmax + 1;
  if (ans < 0)
    ans = 0;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
int N;
int M;
int L[100020];
int R[100020];
int lmax, rmin;
int ans;

int main() {
  cin >> N >> M;
  lmax = 0;
  rmin = N;
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    if (lmax < L[i])
      lmax = L[i];
    if (rmin > R[i])
      rmin = R[i];
  }
  ans = rmin - lmax + 1;
  if (ans < 0)
    ans = 0;
  cout << ans << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl "\n"
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define REPR(i, a, n) for (int i = a; i > n; --i)
#define RUP(a, b) ((a + b - 1) / (b))
#define SORTG(v) sort(v.begin(), v.end(), greater<>())
#define SORT(v) sort(v.begin(), v.end())
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define INF LLONG_MAX / 3

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<string> Vs;

template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
void YesNo(int a) { cout << ((a) ? "Yes" : "No") << endl; }
void YESNO(int a) { cout << ((a) ? "YES" : "NO") << endl; }
void AorB(int a, string b, string c) { cout << ((a) ? b : c) << endl; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void start() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
}

signed main() {
  start();
  int n, m, ans = 0;
  cin >> n >> m;
  Vi l(m), r(m), a(n);
  REP(i, 0, m) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
    a[l[i]]++;
    a[r[i] + 1]--;
  }
  REP(i, 1, n) a[i] += a[i - 1];
  REP(i, 0, n) if (a[i] == m) ans++;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl "\n"
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define REPR(i, a, n) for (int i = a; i > n; --i)
#define RUP(a, b) ((a + b - 1) / (b))
#define SORTG(v) sort(v.begin(), v.end(), greater<>())
#define SORT(v) sort(v.begin(), v.end())
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define INF LLONG_MAX / 3

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<string> Vs;

template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
void YesNo(int a) { cout << ((a) ? "Yes" : "No") << endl; }
void YESNO(int a) { cout << ((a) ? "YES" : "NO") << endl; }
void AorB(int a, string b, string c) { cout << ((a) ? b : c) << endl; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void start() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
}

signed main() {
  start();
  int n, m, ans = 0;
  cin >> n >> m;
  Vi l(m), r(m), a(n);
  REP(i, 0, m) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
    a[l[i]]++;
    if (r[i] + 1 < n)
      a[r[i] + 1]--;
  }
  REP(i, 1, n) a[i] += a[i - 1];
  REP(i, 0, n) if (a[i] == m) ans++;
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define MAX 50005

int tree[4 * MAX], arr[MAX], print[MAX];

map<int, int> cnt;

void bld(int n, int b, int e) {
  if (b == e) {
    // cout << arr[b] << " = " << tree[n] << endl;
    cnt[arr[b]] += tree[n];
    return;
  }

  int lft = 2 * n;
  int rit = lft + 1;
  int mid = (b + e) / 2;

  tree[lft] += tree[n];
  tree[rit] += tree[n];

  bld(lft, b, mid);
  bld(rit, mid + 1, e);
}

void up(int n, int b, int e, int l, int r) {
  if (r < arr[b] or arr[e] < l)
    return;

  if (l <= arr[b] and arr[e] <= r) {
    // cout << l << " to " << r << " val " << arr[b] << " " << arr[e] << endl;
    tree[n]++;
    return;
  }

  int left = n * 2;
  int right = left + 1;
  int mid = (b + e) / 2;

  up(left, b, mid, l, r);
  up(right, mid + 1, e, l, r);
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);

  for (int i = 1; i <= n; i++)
    arr[i] = i;

  for (int i = 0; i < q; i++) {
    int u, v;
    scanf("%d %d", &u, &v);

    if (q == 1) {
      cout << v - u + 1 << endl;
      return 0;
    }

    up(1, 1, n, u, v);
  }

  bld(1, 1, n);

  int cn = 0;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] == q)
      cn++;
  }

  cout << cn << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define MAX 100005

int tree[4 * MAX], arr[MAX], print[MAX];

map<int, int> cnt;

void bld(int n, int b, int e) {
  if (b == e) {
    // cout << arr[b] << " = " << tree[n] << endl;
    cnt[arr[b]] += tree[n];
    return;
  }

  int lft = 2 * n;
  int rit = lft + 1;
  int mid = (b + e) / 2;

  tree[lft] += tree[n];
  tree[rit] += tree[n];

  bld(lft, b, mid);
  bld(rit, mid + 1, e);
}

void up(int n, int b, int e, int l, int r) {
  if (r < arr[b] or arr[e] < l)
    return;

  if (l <= arr[b] and arr[e] <= r) {
    // cout << l << " to " << r << " val " << arr[b] << " " << arr[e] << endl;
    tree[n]++;
    return;
  }

  int left = n * 2;
  int right = left + 1;
  int mid = (b + e) / 2;

  up(left, b, mid, l, r);
  up(right, mid + 1, e, l, r);
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);

  for (int i = 1; i <= n; i++)
    arr[i] = i;

  for (int i = 0; i < q; i++) {
    int u, v;
    scanf("%d %d", &u, &v);

    if (q == 1) {
      cout << v - u + 1 << endl;
      return 0;
    }

    up(1, 1, n, u, v);
  }

  bld(1, 1, n);

  int cn = 0;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] == q)
      cn++;
  }

  cout << cn << endl;

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c = 0, N = 0;
  cin >> a >> b;
  vector<int> L(b), R(b);
  for (int i = 0; i < b; i++)
    cin >> L[i] >> R[i];
  for (int i = L[b - 1]; i < R[0] + 1; i++) {
    for (int j = 0; j < b; j++) {
      if (L[j] <= i && i <= R[j])
        c++;
    }
    if (b == c)
      N++;
    c = 0;
  }
  cout << N << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c = 0, N = 0;
  cin >> a >> b;
  vector<int> L(b), R(b);
  for (int i = 0; i < b; i++)
    cin >> L[i] >> R[i];
  sort(L.begin(), L.end());
  sort(R.begin(), R.end());
  N = R[0] - L[b - 1] + 1;
  if (0 > N)
    N = 0;
  cout << N << endl;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
const long long INF = 1e9 + 7;
using namespace std;
map<int, int> mp;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  for (int i = 0; i < m; i++)
    cin >> l[i] >> r[i];
  int counter = 0;
  bool flag = true;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if (!(l[j] <= i && i <= r[j])) {
        flag = false;
        break;
      }
    }
    if (flag)
      counter++;
    flag = true;
  }
  cout << counter << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
const long long INF = 1e9 + 7;
using namespace std;
map<int, int> mp;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  for (int i = 0; i < m; i++)
    cin >> l[i] >> r[i];
  sort(l.rbegin(), l.rend());
  sort(r.begin(), r.end());
  int x = r[0] - l[0] + 1;
  if (x > 0) {
    cout << x << endl;
  } else
    cout << 0 << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define print(x) cout << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() {
  int n, m;
  cin >> n >> m;
  vi s(n + 1, 0);
  rep(i, m) {
    int l, r;
    cin >> l >> r;
    s[l]++;
    s[r + 1]--;
  }
  rep(i, n) { s[i + 1] += s[i]; }
  int cnt = 0;
  rep(i, n + 1) {
    if (s[i] == m)
      cnt++;
  }
  print(cnt);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define print(x) cout << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() {
  int n, m;
  cin >> n >> m;
  vi s(n + 2, 0);
  rep(i, m) {
    int l, r;
    cin >> l >> r;
    s[l]++;
    s[r + 1]--;
  }
  rep(i, n) { s[i + 1] += s[i]; }
  int cnt = 0;
  rep(i, n + 1) {
    if (s[i] == m)
      cnt++;
  }
  print(cnt);
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define ll long long
#define INF INT_MAX
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int L[N], R[N];
  for (int i = 0; i < M; i++)
    cin >> L[i] >> R[i];
  vector<int> count(N, 0);
  for (int i = 0; i < M; i++) {
    count[L[i] - 1]++;
    count[R[i]]--;
  }
  for (int i = 1; i < N; i++) {
    count[i] = count[i] + count[i - 1];
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (count[i] == M)
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define ll long long
#define INF INT_MAX
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int L[N], R[N];
  for (int i = 0; i < M; i++)
    cin >> L[i] >> R[i];
  int count[N];
  for (int i = 0; i < N; i++)
    count[i] = 0;
  for (int i = 0; i < M; i++) {
    count[L[i] - 1]++;
    count[R[i]]--;
  }
  for (int i = 1; i < N; i++) {
    count[i] = count[i] + count[i - 1];
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (count[i] == M)
      ans++;
  }
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long n, m;
  cin >> n >> m;
  long l[m];
  long r[m];
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i];
  }
  long sei = 0;
  for (long i = 1; i <= n; i++) {
    int aho = 1;
    for (long j = 0; j < m; j++) {
      if (i < l[j] || i > r[j]) {
        aho = 0;
        break;
      }
    }
    if (aho == 1) {
      sei += 1;
      // cout<<i<<endl;
    }
  }
  cout << sei;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long n, m;
  cin >> n >> m;
  long l[m];
  long r[m];
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i];
  }
  sort(l, l + m);
  sort(r, r + m);
  int hidari = l[m - 1];
  int migi = r[0];
  int k = migi - hidari + 1;
  if (k <= 0)
    cout << "0";
  else
    cout << k;
  return 0;
}
Problem p03037 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
import sys

N, M = map(int, input().rstrip().split())

L = []
R = []
for _ in range(M):
    l, r = map(int, input().rstrip().split())
    L.append(l)
    R.append(r)

Ans = set(range(L[0], R[0] + 1))
for i in range(1, len(L)):
    Ans = set(range(L[i], R[i] + 1)) & Ans
    if not Ans:
        print(0)
        sys.exit(0)

print(len(Ans))


Fixed submission:
#!/usr/bin/env python3
import sys

N, M = map(int, input().rstrip().split())

L = []
R = []
for _ in range(M):
    l, r = map(int, input().rstrip().split())
    L.append(l)
    R.append(r)

Ans = min(R) - max(L) + 1
if Ans < 0:
    Ans = 0
print(Ans)
Problem p03037 (Python)

Buggy submission (status: Time Limit Exceeded):
n, m = map(int, input().split())
ans = set(range(1, n + 1))
for _ in range(m):
    l, r = map(int, input().split())
    st = set(range(l, r + 1))
    ans &= st
print(len(ans))


Fixed submission:
def solve2():
    n, m = map(int, input().split())
    L = 1
    R = n
    for _ in range(m):
        l, r = map(int, input().split())
        L = max(L, l)
        R = min(R, r)
    if L <= R:
        print(R - L + 1)
    else:
        print(0)


solve2()
Problem p03037 (Python)

Buggy submission (status: Time Limit Exceeded):
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
count = 0
for i in range(M):
    L[i], R[i] = map(int, input().split())
for i in range(N):
    clear = 0
    for j in range(M):
        if L[j] <= i + 1 <= R[j]:
            clear += 1
        else:
            break
    if clear == M:
        count += 1
print(count)


Fixed submission:
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
count = 0
for i in range(M):
    L[i], R[i] = map(int, input().split())
Lmax = max(L)
Rmin = min(R)
if Lmax <= Rmin:
    print(Rmin - Lmax + 1)
else:
    print(0)
Problem p03037 (Python)

Buggy submission (status: Time Limit Exceeded):
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
ans = []
for i in range(m):
    res = []
    for j in range(lr[i][0], lr[i][1] + 1):
        res.append(j)
    if i == 0:
        ans = set(res)
    else:
        ans = set(ans) & set(res)
print(len(ans))


Fixed submission:
n, m = map(int, input().split())
mi, ma = 1, n
for _ in range(m):
    l, r = map(int, input().split())
    mi = max(l, mi)
    ma = min(r, ma)
print(max(0, ma - mi + 1))
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<int> l(n), r(n);
  int maxl = 0, minr = n + 1;
  rep(i, m) cin >> l.at(i) >> r.at(i);
  rep(i, m) {
    maxl = max(maxl, l.at(i));
    minr = min(minr, r.at(i));
  }
  if (minr < maxl)
    cout << 0 << endl;
  else
    cout << minr - maxl + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  int maxl = 1, minr = n + 1;
  rep(i, m) cin >> l.at(i) >> r.at(i);
  rep(i, m) {
    maxl = max(maxl, l.at(i));
    minr = min(minr, r.at(i));
  }
  if (minr < maxl)
    cout << 0 << endl;
  else
    cout << minr - maxl + 1 << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(obj) (obj).begin, (obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
  int n, m;
  cin >> n >> m;
  int l, r;
  int L[n], R[n];
  REP(i, m) { cin >> L[i] >> R[i]; }
  l = L[0];
  r = R[0];
  FOR(i, 1, m) {
    if (l > R[i] || r < L[i]) {
      cout << 0 << endl;
      return 0;
    }
    l = max(l, L[i]);
    r = min(r, R[i]);
  }
  cout << r - l + 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(obj) (obj).begin, (obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
  int n, m;
  cin >> n >> m;
  int l, r;
  int L[m], R[m];
  REP(i, m) { cin >> L[i] >> R[i]; }
  l = L[0];
  r = R[0];
  FOR(i, 1, m) {
    if (l > R[i] || r < L[i]) {
      cout << 0 << endl;
      return 0;
    }
    l = max(l, L[i]);
    r = min(r, R[i]);
  }
  cout << r - l + 1 << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}

int main() {
  int N, M;
  cin >> N >> M;
  vint L(N);
  vint R(N);
  REP(i, M) { cin >> L[i] >> R[i]; }
  int l = L[0];
  int r = R[0];

  REP(i, M - 1) {
    l = max(l, L[i + 1]);
    r = min(r, R[i + 1]);
  }

  int ans = 0;
  if (l <= r) {
    REP(i, N) {
      if (l <= (i + 1) && (i + 1) <= r) {
        ans++;
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}

int main() {
  int N, M;
  cin >> N >> M;
  vint L(M);
  vint R(M);
  REP(i, M) { cin >> L[i] >> R[i]; }
  int l = L[0];
  int r = R[0];

  REP(i, M - 1) {
    l = max(l, L[i + 1]);
    r = min(r, R[i + 1]);
  }

  int ans = 0;
  if (l <= r) {
    REP(i, N) {
      if (l <= (i + 1) && (i + 1) <= r) {
        ans++;
      }
    }
  }

  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = s; i < (int)n; i++)
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  rep(i, m) cin >> a[i] >> b[i];
  map<int, int> MAP;
  rep(i, m) {
    rep2(j, a[i] - 1, b[i]) { MAP[j]++; }
  }
  int ans = 0;
  rep(i, n) if (MAP[i] == m) ans++;
  cout << ans << endl;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = s; i < (int)n; i++)
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  rep(i, m) cin >> a[i] >> b[i];
  sort(all(a));
  sort(all(b));
  if (b[0] < a[m - 1])
    cout << 0 << endl;
  else
    cout << b[0] - a[m - 1] + 1 << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(n), r(n);
  rep(i, m) cin >> l[i] >> r[i];

  int mnr = n, mxl = 1;
  rep(i, m) {
    mxl = max(mxl, l[i]);
    mnr = min(mnr, r[i]);
  }
  if (mnr < mxl)
    cout << 0 << endl;
  else
    cout << mnr - mxl + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  rep(i, m) cin >> l[i] >> r[i];

  int mnr = n, mxl = 1;
  rep(i, m) {
    mxl = max(mxl, l[i]);
    mnr = min(mnr, r[i]);
  }
  if (mnr < mxl)
    cout << 0 << endl;
  else
    cout << mnr - mxl + 1 << endl;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m);
  vector<int> r(m);
  rep(i, m) cin >> l[i] >> r[i];
  int cnt = 0;
  rep(i, n + 1) {
    bool ok = true;
    rep(j, m) {
      if (!(i >= l[j] && i <= r[j]))
        ok = false;
    }
    if (ok)
      cnt++;
  }
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m);
  vector<int> r(m);
  rep(i, m) cin >> l[i] >> r[i];
  int L = *max_element(l.begin(), l.end());
  int R = *min_element(r.begin(), r.end());

  int ans = R - L + 1;
  if (ans >= 0)
    cout << ans;
  else
    cout << "0";
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>

int main() {
  int n, m;

  std::cin >> n >> m;

  std::vector<int> v(n + 1, 0);

  for (size_t i = 0; i < m; i++) {
    int a, b;

    std::cin >> a >> b;

    v[a]++;
    v[b + 1]--;
  }

  for (size_t i = 1; i < n + 1; i++) {
    v[i] += v[i - 1];
  }

  int ans = 0;
  for (size_t i = 0; i < n + 1; i++) {
    if (v[i] == m) {
      ans++;
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>

int main() {
  int n, m;

  std::cin >> n >> m;

  std::vector<int> v(n + 1, 0);

  for (size_t i = 0; i < m; i++) {
    int a, b;

    std::cin >> a >> b;

    a--;
    b--;

    v[a]++;
    v[b + 1]--;
  }

  for (size_t i = 1; i < n + 1; i++) {
    v[i] += v[i - 1];
  }

  int ans = 0;
  for (size_t i = 0; i < n + 1; i++) {
    if (v[i] == m) {
      ans++;
    }
  }

  std::cout << ans << std::endl;

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> l(n + 1);
  vector<ll> r(n + 1);
  for (int i = 1; i <= m; i++) {
    cin >> l[i] >> r[i];
  }

  ll lmax = 1;
  ll rmin = 1e5;
  for (ll i = 1; i <= m; i++) {
    if (l[i] > lmax)
      lmax = l[i];
    if (r[i] < rmin)
      rmin = r[i];
    // cerr << lmax << " " << rmin << endl;
  }

  // cerr << lmax << " " << rmin << endl;
  rmin = min(rmin, n);
  ll ans = max((ll)0, rmin - lmax + 1);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> l(m + 1);
  vector<ll> r(m + 1);
  for (int i = 1; i <= m; i++) {
    cin >> l[i] >> r[i];
  }

  ll lmax = 1;
  ll rmin = 1e5;
  for (ll i = 1; i <= m; i++) {
    if (l[i] > lmax)
      lmax = l[i];
    if (r[i] < rmin)
      rmin = r[i];
    // cerr << lmax << " " << rmin << endl;
  }

  // cerr << lmax << " " << rmin << endl;
  rmin = min(rmin, n);
  ll ans = max((ll)0, rmin - lmax + 1);
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

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

#define INF INT_MAX
#define MOD 100000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N, M;
  cin >> N >> M;
  vector<int> L(M), R(M);
  for (int i = 0; i < M; i++)
    cin >> L[i] >> R[i];
  vector<int> sum(N + 1, 0);
  for (int i = 0; i < M; i++)
    sum[L[i]]++, sum[R[i] + 1]--;
  for (int i = 1; i < N + 1; i++)
    sum[i] += sum[i - 1];
  int ans = 0;
  for (auto it : sum)
    if (it == M)
      ans++;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

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

#define INF INT_MAX
#define MOD 100000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N, M;
  cin >> N >> M;
  vector<int> L(M), R(M);
  for (int i = 0; i < M; i++)
    cin >> L[i] >> R[i];
  vector<int> sum(N + 2, 0);
  for (int i = 0; i < M; i++)
    sum[L[i]]++, sum[R[i] + 1]--;
  for (int i = 1; i < N + 1; i++)
    sum[i] += sum[i - 1];
  int ans = 0;
  for (auto it : sum)
    if (it == M)
      ans++;
  cout << ans << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> lr(n + 1, 0);

  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    lr[l]++, lr[r + 1]--;
  }

  for (int i = 0; i < n; i++) {
    lr[i + 1] += lr[i];
  }

  cout << count(lr.begin(), lr.end(), m) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  vector<int64_t> lr(n + 1, 0);

  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    lr[l]++, lr[r + 1]--;
  }

  for (int i = 0; i < n; i++) {
    lr[i + 1] += lr[i];
  }

  cout << count(lr.begin(), lr.end(), m) << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> imo(n + 1, 0);
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    ++imo[l];
    --imo[r + 1];
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    imo[i] += imo[i - 1];
    if (imo[i] == m)
      ++ans;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> imo(n + 2, 0);
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    ++imo[l];
    --imo[r + 1];
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    imo[i] += imo[i - 1];
    if (imo[i] == m)
      ++ans;
  }
  cout << ans << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m);
  vector<int> r(m);
  rep(i, m) {
    cin >> l[i];
    cin >> r[i];
  }
  int ans = 0;
  rep(i, n) {
    bool ja = true;
    rep(j, m) {
      if (i + 1 < l[j] || r[j] < i + 1) {
        ja = false;
      }
    }
    if (ja) {
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m);
  vector<int> r(m);
  rep(i, m) {
    cin >> l[i];
    cin >> r[i];
  }
  sort(all(l));
  reverse(all(l));
  sort(all(r));
  int ans = r[0] - l[0] + 1;
  if (ans < 0) {
    ans = 0;
  }
  cout << ans << endl;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define num 100000

int main() {
  int n, m;
  cin >> n >> m;
  bitset<num> a;
  REP(i, m) {
    int L, R;
    cin >> L >> R;
    bitset<num> b;
    for (int j = L - 1; j < R; j++)
      b.set(j, 1);
    if (i == 0)
      a = b;
    else
      a = a & b;
  }
  cout << a.count() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define num 100000

int main() {
  int n, m;
  cin >> n >> m;
  bitset<num> a;
  REP(i, m) {
    int L, R;
    cin >> L >> R;
    bitset<num> b;
    b.flip();
    b = (b >> L - 1) ^ (b >> R);
    if (i == 0)
      a = b;
    else
      a = a & b;
  }
  cout << a.count() << endl;
}