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

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

using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;

int main() {
  llong N, M;
  vector<llong> L, R, rui;

  cin >> N >> M;
  L.resize(N);
  R.resize(N);
  rui.resize(N + 2, 0);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    rui[L[i]]++;
    rui[R[i] + 1]--;
  }

  llong ans = 0;
  for (int i = 1; i <= N; i++) {
    rui[i] += rui[i - 1];
    if (rui[i] == M)
      ans++;
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;

int main() {
  llong N, M;
  vector<llong> L, R, rui;

  cin >> N >> M;
  L.resize(M);
  R.resize(M);
  rui.resize(N + 2, 0);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    rui[L[i]]++;
    rui[R[i] + 1]--;
  }

  llong ans = 0;
  for (int i = 1; i <= N; i++) {
    rui[i] += rui[i - 1];
    if (rui[i] == M)
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int mxsz = 1e5 + 3;
int n, m;
vector<pii> v;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> v[i].fi >> v[i].se;
  }
  sort(v.begin(), v.end());
  int tot = 0;
  int l = 0, r = 0x3f3f3f3f;
  for (int i = 0; i < m; i++) {
    l = max(l, v[i].fi);
    r = min(r, v[i].se);
  }
  cout << max(0, r - l + 1) << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int mxsz = 1e5 + 3;
int n, m;
vector<pii> v;
int main() {
  cin >> n >> m;
  v.resize(m);
  for (int i = 0; i < m; i++) {
    cin >> v[i].fi >> v[i].se;
  }
  sort(v.begin(), v.end());
  int tot = 0;
  int l = 0, r = 0x3f3f3f3f;
  for (int i = 0; i < m; i++) {
    l = max(l, v[i].fi);
    r = min(r, v[i].se);
  }
  cout << max(0, r - l + 1) << '\n';
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

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

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

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

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

  int x = 0;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    x += a[i];
    if (x == 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;
  vector<int> L(N, 0), R(N, 0);
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++)
    scanf("%d%d", &L[i], &R[i]);
  int min = 1, max = N;
  for (int i = 0; i < M; i++) {
    if (L[i] > min)
      min = L[i];
    if (R[i] < max)
      max = R[i];
  }
  if (min <= max)
    printf("%d", max - min + 1);
  else
    printf("0");
  return 0;
}

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

int main() {
  int N, M;
  vector<int> L(N, 0), R(N, 0);
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    L[i] = l;
    R[i] = r;
  }
  int min = 1, max = N;
  for (int i = 0; i < M; i++) {
    if (L[i] > min)
      min = L[i];
    if (R[i] < max)
      max = R[i];
  }
  if (min <= max)
    printf("%d", max - min + 1);
  else
    printf("0");
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define _GLIBCXX_DEBUG
using namespace std;
const long long INF = 1LL << 60;
typedef long long ll;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(M);
  vector<int> R(N);
  int min_R;
  int max_L;
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
  }
  for (int i = 0; i < M; i++) {
    if (i == 0) {
      min_R = R[i];
      max_L = L[i];
    } else if (min_R > R[i]) {
      min_R = R[i];
    } else if (max_L < L[i]) {
      max_L = L[i];
    }
  }
  int ans;
  ans = min_R - max_L + 1;
  if (ans <= 0) {
    ans = 0;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define _GLIBCXX_DEBUG
using namespace std;
const long long INF = 1LL << 60;
typedef long long ll;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(M);
  vector<int> R(M);
  int min_R;
  int max_L;
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
  }
  for (int i = 0; i < M; i++) {
    if (i == 0) {
      min_R = R[i];
      max_L = L[i];
    } else if (min_R > R[i]) {
      min_R = R[i];
    } else if (max_L < L[i]) {
      max_L = L[i];
    }
  }
  int ans;
  ans = min_R - max_L + 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> l(n);
  vector<int> r(n);
  int mal = 0;
  int mar = 10000000;

  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i];
    mal = max(mal, l[i]);
    mar = min(mar, r[i]);
  }
  if (mal <= mar) {
    cout << mar - mal + 1 << endl;
  } else {
    cout << 0 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m);
  vector<int> r(m);
  int mal = 0;
  int mar = 10000000;

  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i];
    mal = max(mal, l[i]);
    mar = min(mar, r[i]);
  }
  if (mal <= mar) {
    cout << mar - mal + 1 << endl;
  } else {
    cout << 0 << endl;
  }
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include "bits/stdc++.h"
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long long ll;

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


Fixed submission:
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include "bits/stdc++.h"
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long long ll;

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
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);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int count = 0;
    for (int j = 0; j < m; j++) {
      if (l.at(j) <= i && i <= r.at(j)) {
        count++;
      }
    }
    if (count == m) {
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
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);
  }
  cout << max(0, *min_element(r.begin(), r.end()) -
                     *max_element(l.begin(), l.end()) + 1);
}
Problem p03037 (C++)

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

using namespace std;
using ll = long long;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(N), R(N);
  int maxl = 0, minr = 100010;
  for (int i = 0; i < M; ++i) {
    cin >> L[i] >> R[i];
    maxl = max(maxl, L[i]);
    minr = min(minr, R[i]);
  }
  cout << max(0, minr - maxl + 1) << endl;
}


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

using namespace std;
using ll = long long;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(M), R(M);
  int maxl = 0, minr = 100010;
  for (int i = 0; i < M; ++i) {
    cin >> L[i] >> R[i];
    maxl = max(maxl, L[i]);
    minr = min(minr, R[i]);
  }
  cout << max(0, minr - maxl + 1) << endl;
}
Problem p03037 (C++)

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

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

  std::vector<bool> can_use_card(n, true);
  for (int i = 0; i < m; i++) {
    int l, r;
    std::cin >> l >> r;

    l--;
    r--;

    for (int j = 0; j < l; j++) {
      can_use_card[j] = false;
    }

    for (int j = r + 1; j < n; j++) {
      can_use_card[j] = false;
    }
  }

  std::cout << std::count(can_use_card.begin(), can_use_card.end(), true)
            << std::endl;
}


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

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

  std::vector<bool> can_use_card(n, true);
  for (int i = 0; i < m; i++) {
    int l, r;
    std::cin >> l >> r;

    l--;
    r--;

    std::fill(can_use_card.begin(), can_use_card.begin() + l, false);
    std::fill(can_use_card.begin() + r + 1, can_use_card.end(), false);
  }

  std::cout << std::count(can_use_card.begin(), can_use_card.end(), true)
            << std::endl;
}
Problem p03037 (C++)

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

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(n);
  vector<int> r(n);

  int r_min = n;
  int l_max = 1;
  for (int i = 0; i < m; ++i) {
    cin >> l[i] >> r[i];
    if (r[i] < r_min) {
      r_min = r[i];
    }
    if (l_max < l[i]) {
      l_max = l[i];
    }
  }

  // cerr << r_min << l_max << endl;

  if (l_max <= r_min) {
    cout << r_min - l_max + 1 << endl;
  } else {
    cout << 0 << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m);
  vector<int> r(m);

  int r_min = n;
  int l_max = 1;
  for (int i = 0; i < m; ++i) {
    cin >> l[i] >> r[i];
    if (r[i] < r_min) {
      r_min = r[i];
    }
    if (l_max < l[i]) {
      l_max = l[i];
    }
  }

  // cerr << r_min << l_max << endl;

  if (l_max <= r_min) {
    cout << r_min - l_max + 1 << endl;
  } else {
    cout << 0 << endl;
  }

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define yes "Yes"
#define no "No"

typedef pair<int, int> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  map<int, int> ma;
  REP(i, m) {
    int l, r;
    cin >> l >> r;
    while (l <= r) {
      ma[l]++;
      l++;
    }
  }
  int ans = 0;
  for (auto e : ma) {
    if (e.second == m)
      ans++;
  }
  print(ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define yes "Yes"
#define no "No"

typedef pair<int, int> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<lint> l(m), r(m);
  REP(i, m) { cin >> l[i] >> r[i]; }
  sort(all(l), greater<lint>());
  sort(all(r));
  if (r[0] - l[0] >= 0)
    print(r[0] - l[0] + 1);
  else
    print(0);
  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> L(M), R(M);
  vector<int> card_list(N + 1, 0);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    card_list[L[i]]++;
    card_list[R[i] + 1]--;
  }

  for (int i = 0; i < N; i++) {
    card_list[i + 1] += card_list[i];
  }
  int count = 0;

  for (int i = 0; i < N + 1; i++) {
    if (card_list[i] == M)
      count++;
  }

  cout << count << 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);
  vector<int> card_list(N + 1, 0);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i];
    card_list[L[i] - 1]++;
    card_list[R[i] + 1 - 1]--;
  }

  for (int i = 0; i < N; i++) {
    card_list[i + 1] += card_list[i];
  }
  int count = 0;

  for (int i = 0; i < N + 1; i++) {
    if (card_list[i] == M)
      count++;
  }

  cout << count << endl;

  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):

// </> : towhid1zaman

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define fill(a, b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define sp(k) cout << setprecision(k) << fixed;
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define _                                                                      \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f; // (int)3e18;
const int maxn = 2000100;

int main() {
  _ ll n, m;
  cin >> n >> m;
  ll L[n + 1], R[m + 1];
  for (int i = 0; i < m; i++) {
    cin >> L[i] >> R[i];
  }
  sort(L, L + m);
  sort(R, R + m);
  ll X = L[m - 1];
  ll Y = R[0];
  ll ans = Y - X + 1;
  cout << (ans <= 0 ? 0 : ans) << endl;

  return 0;
}


Fixed submission:

// </> : towhid1zaman

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define fill(a, b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define sp(k) cout << setprecision(k) << fixed;
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define _                                                                      \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int inf = 0x3f3f3f3f; // (int)3e18;
const int maxn = 2000100;

int main() {
  _ ll n, m;
  cin >> n >> m;
  ll L[m + 1], R[m + 1];
  for (int i = 0; i < m; i++) {
    cin >> L[i] >> R[i];
  }
  sort(L, L + m);
  sort(R, R + m);
  ll X = L[m - 1];
  ll Y = R[0];
  ll ans = Y - X + 1;
  cout << (ans <= 0 ? 0 : ans) << endl;

  return 0;
}
Problem p03037 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> ok;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = a; j <= b; j++) {
      ok[j - 1]++;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (ok[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> a(m);
  vector<int> b(m);
  for (int i = 0; i < m; i++)
    cin >> a.at(i) >> b.at(i);
  sort(a.begin(), a.end(), greater());
  sort(b.begin(), b.end());
  cout << max(0, b.at(0) - a.at(0) + 1) << 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 max_l = INT_MIN;
  int min_r = INT_MAX;
  for (int i = 0; i < M; i++) {
    int l, r;
    cin >> l >> r;
    max_l = max(max_l, l);
    min_r = min(min_r, r);
  }
  if (min_r - max_l < 0)
    return -1;
  cout << min_r - max_l + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int max_l = INT_MIN;
  int min_r = INT_MAX;
  for (int i = 0; i < M; i++) {
    int l, r;
    cin >> l >> r;
    max_l = max(max_l, l);
    min_r = min(min_r, r);
  }
  if (min_r - max_l < 0)
    cout << 0 << endl;
  else
    cout << min_r - max_l + 1 << endl;
}
Problem p03037 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
using ll = long long;
int main() {
  ll N, M;
  cin >> N >> M;
  ll L[N + 10], R[N + 10], l, r;
  for (int k = 1; k <= M; k++)
    cin >> L[k] >> R[k];
  l = L[1];
  for (int i = 1; i <= M; i++) {
    if (L[i] >= l)
      l = L[i];
  }
  r = R[1];
  for (int i = 1; i <= M; i++) {
    if (R[i] <= r)
      r = R[i];
  }
  if (0 <= r - l)
    cout << r - l + 1 << endl;
  else
    cout << 0 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
using ll = long long;
int main() {
  ll N, M;
  cin >> N >> M;
  ll L[M + 10], R[M + 10], l, r;
  for (int k = 1; k <= M; k++)
    cin >> L[k] >> R[k];
  l = L[1];
  for (int i = 1; i <= M; i++) {
    if (L[i] >= l)
      l = L[i];
  }
  r = R[1];
  for (int i = 1; i <= M; i++) {
    if (R[i] <= r)
      r = R[i];
  }
  if (0 <= r - l)
    cout << r - l + 1 << endl;
  else
    cout << 0 << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long INF = 1e18, MOD = 1e9 + 7;
using namespace std;
int main() {
  ll N, M, ans;
  ll maxL = -1;
  cin >> N >> M;
  vector<ll> L(N), R(N);
  rep(i, M) { cin >> L[i] >> R[i]; }
  ll minR = INF;
  rep(i, M) {
    maxL = max(maxL, L[i]);
    minR = min(minR, R[i]);
  }
  ans = minR - maxL + 1;
  ans = max(ans, (ll)0);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long INF = 1e18, MOD = 1e9 + 7;
using namespace std;
int main() {
  ll N, M, ans;
  ll maxL = -1;
  cin >> N >> M;
  vector<ll> L(M), R(M);
  rep(i, M) { cin >> L[i] >> R[i]; }
  ll minR = INF;
  rep(i, M) {
    maxL = max(maxL, L[i]);
    minR = min(minR, R[i]);
  }
  ans = minR - maxL + 1;
  ans = max(ans, (ll)0);
  cout << ans << endl;
}
Problem p03037 (C++)

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

int main() {
  int N, M;
  vector<int> a(M);
  vector<int> b(M);
  for (int i = 0; i < M; i++) {
    cin >> a.at(i) >> b.at(i);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  if (b[0] >= a[M - 1])
    cout << b[0] - a[M - 1] + 1 << endl;
  else
    cout << 0 << endl;
}


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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M);
  vector<int> b(M);
  for (int i = 0; i < M; i++) {
    cin >> a.at(i) >> b.at(i);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  if (b[0] >= a[M - 1])
    cout << b[0] - a[M - 1] + 1 << endl;
  else
    cout << 0 << endl;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
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 l, r;
  l = L[0];
  r = R[0];
  for (int i = 1; i < M; i++) {
    l = max(l, L[i]);
    r = min(r, R[i]);
  }

  cout << max(0, r - l + 1) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

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];

  int l, r;
  l = L[0];
  r = R[0];
  for (int i = 1; i < M; i++) {
    l = max(l, L[i]);
    r = min(r, R[i]);
  }

  cout << max(0, r - l + 1) << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> xx, yy;
const int maxn = 1e5 + 7;
struct q {
  int x, y;
} N[maxn];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    xx.push_back(x);
    yy.push_back(y);
  }
  sort(xx.begin(), xx.end());
  sort(yy.begin(), yy.end());
  cout << xx[n - 1] - yy[0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> xx, yy;
const int maxn = 1e5 + 7;
struct q {
  int x, y;
} N[maxn];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    xx.push_back(x);
    yy.push_back(y);
  }
  sort(xx.begin(), xx.end());
  sort(yy.begin(), yy.end());
  cout << max(-xx[m - 1] + yy[0] + 1, 0) << endl;
}
Problem p03037 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  int r[N];
  int l[N];
  int left = 0;
  int right = 100000;
  for (int i = 0; i < M; i++) {
    cin >> l[i] >> r[i];
    if (left < l[i]) {
      left = l[i];
    }
    if (right > r[i]) {
      right = r[i];
    }
  }
  if ((right - left) < 0) {
    cout << 0 << endl;
    return 0;
  }

  cout << right - left + 1 << endl;
  return 0;
}


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

int main() {
  int N, M;
  cin >> N >> M;
  int r[M];
  int l[M];
  int left = 0;
  int right = 100000;
  for (int i = 0; i < M; i++) {
    cin >> l[i] >> r[i];
    if (left < l[i]) {
      left = l[i];
    }
    if (right > r[i]) {
      right = r[i];
    }
  }
  if ((right - left) < 0) {
    cout << 0 << endl;
    return 0;
  }

  cout << right - left + 1 << endl;
  return 0;
}
Problem p03037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mx 100000
#define md 1000000007ll
#define prMX 10000000
ll prime[prMX + 2];
bool vis[prMX + 2];
#define MX 10000000000000ll

int main() {

  int n, m;
  cin >> n >> m;
  int ar[m + 2];
  memset(ar, 0, sizeof ar);

  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    ar[x]++;
    ar[y + 1]--;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ar[i] += ar[i - 1];
    if (ar[i] == m)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mx 100000
#define md 1000000007ll
#define prMX 10000000
ll prime[prMX + 2];
bool vis[prMX + 2];
#define MX 10000000000000ll

int main() {

  int n, m;
  cin >> n >> m;
  int ar[n + 2];
  memset(ar, 0, sizeof ar);

  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    ar[x]++;
    ar[y + 1]--;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ar[i] += ar[i - 1];
    if (ar[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++)
using ll = unsigned long long;
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> card;
  for (int i = 1; i <= m; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      card[j]++;
    }
  }
  int ans = 0;
  for (int i = 1; i < card.size() + 1; i++)
    if (card[i] == m)
      ans++;
  cout << ans << "\n";
  return 0;
}

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

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

  sort(l.begin(), l.end(), greater<int>());
  sort(r.begin(), r.end());

  int ans = r[0] - l[0] + 1;
  if (ans < 0)
    ans = 0;
  cout << ans << "\n";
  return 0;
}
Problem p03037 (C++)

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

int min(vector<int> a, int m) {
  sort(a.begin(), a.end());
  return a.at(0);
}

int max(vector<int> a, int m) {
  sort(a.begin(), a.end());
  return a.at(m - 1);
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> L(m);
  vector<int> R(m);
  int ans = 0;

  for (int i = 0; i < m; i++) {
    cin >> L.at(i) >> R.at(i);
  }

  int Lmax = max(L, m);
  int Rmin = min(R, m);

  for (int i = Lmax; i <= Rmin && i <= n; i++) {
    int count = 0;
    for (int j = 1; j < m / 2; j++) {
      if (L.at(j) <= i && R.at(j) >= i)
        count++;
      else
        break;
    }
    for (int j = m - 1; j > m / 2 - 1; j--) {
      if (L.at(j) <= i && R.at(j) >= i)
        count++;
      else
        break;
    }

    if (count >= m - 1)
      ans++;
  }
  cout << ans;
}

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

int min(vector<int> a, int m) {
  sort(a.begin(), a.end());
  return a.at(0);
}

int max(vector<int> a, int m) {
  sort(a.begin(), a.end());
  return a.at(m - 1);
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> L(m);
  vector<int> R(m);
  int ans = 0;

  for (int i = 0; i < m; i++) {
    cin >> L.at(i) >> R.at(i);
  }

  int Lmax = max(L, m);
  int Rmin = min(R, m);

  if (Lmax > Rmin)
    cout << 0;
  else
    cout << Rmin - Lmax + 1;
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#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);
  //  vector<bool> A(N,true);
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    bool mighty = true;
    for (int j = 0; j < M; j++) {
      if (i < L.at(j) || R.at(j) < i) {
        mighty = false;
        break;
      }
    }
    if (mighty)
      ans++;
  }
  cout << ans << endl;
}


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);
  //  vector<bool> A(N,true);
  sort(L.begin(), L.end());
  sort(R.begin(), R.end());
  reverse(L.begin(), L.end());
  if (L.at(0) <= R.at(0))
    cout << R.at(0) - L.at(0) + 1 << endl;
  else
    cout << 0 << 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);
  vector<int> r(m);
  for (int i = 0; i < m; ++i) {
    cin >> l[i] >> r[i];
  }
  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  int num = l[n - 1] - r[0] + 1;
  cout << num << endl;
}


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

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

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

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


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

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

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifndef FAST_IO
#define IOS ios_base::sync_with_stdio(false), cin.tie(nullptr);
#endif
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
template <typename T> void print(T t) {
  for (auto &it : t)
    cout << it << " ";
  cout << endl;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(n);
  int max_l = INT_MIN, min_r = INT_MAX;
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i];
    max_l = max(max_l, l[i]);
    min_r = min(min_r, r[i]);
  }
  int ans = min_r - max_l;
  ans++;
  cout << max(0ll, ans) << endl;
}
int32_t main() {
  IOS;
  solve();
  // debug();
#ifdef LOCAL_DEFINE
  cerr << "time elapsed " << (double)clock() / CLOCKS_PER_SEC << endl;
#endif
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifndef FAST_IO
#define IOS ios_base::sync_with_stdio(false), cin.tie(nullptr);
#endif
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
template <typename T> void print(T t) {
  for (auto &it : t)
    cout << it << " ";
  cout << endl;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  int max_l = 1, min_r = 1000001;
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i];
    max_l = max(max_l, l[i]);
    min_r = min(min_r, r[i]);
  }
  int ans = min_r - max_l;
  ans++;
  cout << max(0ll, ans) << endl;
}
int32_t main() {
  IOS;
  solve();
  // debug();
#ifdef LOCAL_DEFINE
  cerr << "time elapsed " << (double)clock() / CLOCKS_PER_SEC << endl;
#endif
}
Problem p03037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const int INF = 1e+9;

int main() {
  int n, m = 0;
  cin >> n >> m;
  vector<int> l_list(m);
  vector<int> r_list(m);
  int r_min = INF;
  int l_max = -99999;
  int l = 0;
  int r = 0;
  for (int i = 0; i < m; i++) {
    cin >> l;
    l_max = max(l_max, l);

    cin >> r;
    r_min = min(r_min, r);
  }

  int ct = 0;
  for (int i = 1; i < n + 1; i++) {
    bool a = true;
    for (int j = 0; j < m; j++) {
      if (l_max <= i && i <= r_min) {
      } else {
        a = false;
      }
    }
    if (a == true) {
      ct++;
    }
  }
  cout << ct << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const int INF = 1e+9;

int main() {
  int n, m = 0;
  cin >> n >> m;
  vector<int> l_list(m);
  vector<int> r_list(m);
  int r_min = INF;
  int l_max = -99999;
  int l = 0;
  int r = 0;
  for (int i = 0; i < m; i++) {
    cin >> l;
    l_max = max(l_max, l);

    cin >> r;
    r_min = min(r_min, r);
  }

  int ct = 0;
  for (int i = 1; i < n + 1; i++) {
    if (l_max <= i && i <= r_min) {
      ct++;
    }
  }
  cout << ct << "\n";

  return 0;
}
Problem p03038 (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;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  map<int, ll> mp;
  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    mp[c] += b;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    auto it = mp.rbegin();
    if (it->first > a[i]) {
      a[i] = it->first;
      it->second--;
      if (it->second == 0)
        mp.erase(it->first);
    }
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  map<int, ll> mp;
  for (int i = 0; i < m; i++) {
    int b, c;
    cin >> b >> c;
    mp[c] += b;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    auto it = mp.rbegin();
    if (mp.size() && it->first > a[i]) {
      a[i] = it->first;
      it->second--;
      if (it->second == 0)
        mp.erase(it->first);
    }
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-Ofast")
#include <bits/stdc++.h>
using namespace std;

typedef int in;
#define int long long
#define pb push_back
#define f first
#define s second
#define double long double
const int MAX = 2e5 + 5;
const int MAX2 = 2e5 + 4;
const int MAAX = 2e18;
const int MOD = 1e9 + 7;
int a[MAX];
pair<int, int> p[MAX];
in main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> p[i].s >> p[i].f;
    p[i].f *= -1;
  }
  sort(a, a + n);
  sort(p, p + m);
  int idx = 0;
  for (int i = 0; i < m; i++) {
    p[i].f *= -1;
    int cur = p[i].s;
    while (p[i].f > a[idx] && cur--) {
      a[idx++] = p[i].f;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-Ofast")
#include <bits/stdc++.h>
using namespace std;

typedef int in;
#define int long long
#define pb push_back
#define f first
#define s second
#define double long double
const int MAX = 2e5 + 5;
const int MAX2 = 2e5 + 4;
const int MAAX = 2e18;
const int MOD = 1e9 + 7;
int a[MAX];
pair<int, int> p[MAX];
in main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> p[i].s >> p[i].f;
    p[i].f *= -1;
  }
  sort(a, a + n);
  sort(p, p + m);
  int idx = 0;
  for (int i = 0; i < m; i++) {
    p[i].f *= -1;
    int cur = p[i].s;
    while (idx < n && p[i].f > a[idx] && cur--) {
      a[idx++] = p[i].f;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int N, M;
long long int A[100005], C[100005];
int B[100005];

vector<long long int> V;
set<long long int> temp;
vector<long long int> W;

map<long long int, int> MAP;

int main(void) {

  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    V.push_back(A[i]);
  }

  for (int i = 1; i <= M; i++) {
    cin >> B[i] >> C[i];
    temp.insert(-C[i]);
    MAP[C[i]] += B[i];
  }

  sort(V.begin(), V.end());

  for (auto it = temp.begin(); it != temp.end(); it++) {

    long long int val;
    val = (*it);
    val = -val;

    for (int j = 1; j <= MAP[val]; j++) {
      W.push_back(val);
    }

    if (W.size() > N) {
      break;
    }
  }

  W.push_back(-100004);

  vector<long long int> ans;

  int p = 0, q = 0;

  for (int i = 1; i <= N; i++) {
    // cout << "comp " << V[p] << " " << W[q] << endl;
    if (V[p] > W[q]) {
      ans.push_back(V[p]);
      p++;
    } else {
      ans.push_back(W[q]);
      q++;
      p++;
    }
  }

  long long int ANS = 0;

  for (int i = 0; i < N; i++) {
    ANS += ans[i];
  }

  cout << ANS << endl;

  /*for (int i = 0; i < N; i++) {
          cout << "V[" << i << "]= " << V[i] << endl;
  }
  for (int i = 0; i < N; i++) {
          cout << "W[" << i << "]= " << W[i] << endl;
  }

  for (int i = 0; i < N; i++) {
          cout << "ans[" << i << "]= " << ans[i] << endl;
  }*/

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int N, M;
long long int A[100005], C[100005];
int B[100005];

vector<long long int> V;
set<long long int> temp;
vector<long long int> W;

map<long long int, int> MAP;

int main(void) {

  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    V.push_back(A[i]);
  }

  for (int i = 1; i <= M; i++) {
    cin >> B[i] >> C[i];
    temp.insert(-C[i]);
    MAP[C[i]] += B[i];
  }

  sort(V.begin(), V.end());

  for (auto it = temp.begin(); it != temp.end(); it++) {

    long long int val;
    val = (*it);
    val = -val;

    for (int j = 1; j <= MAP[val]; j++) {
      W.push_back(val);
      if (W.size() > N) {
        break;
      }
    }

    if (W.size() > N) {
      break;
    }
  }

  W.push_back(-100004);

  vector<long long int> ans;

  int p = 0, q = 0;

  for (int i = 1; i <= N; i++) {
    // cout << "comp " << V[p] << " " << W[q] << endl;
    if (V[p] > W[q]) {
      ans.push_back(V[p]);
      p++;
    } else {
      ans.push_back(W[q]);
      q++;
      p++;
    }
  }

  long long int ANS = 0;

  for (int i = 0; i < N; i++) {
    ANS += ans[i];
  }

  cout << ANS << endl;

  /*for (int i = 0; i < N; i++) {
          cout << "V[" << i << "]= " << V[i] << endl;
  }
  for (int i = 0; i < N; i++) {
          cout << "W[" << i << "]= " << W[i] << endl;
  }

  for (int i = 0; i < N; i++) {
          cout << "ans[" << i << "]= " << ans[i] << endl;
  }*/

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

int main() {
  ll N, M;
  cin >> N >> M;
  priority_queue<ll, vector<ll>, greater<ll>> Q;

  rep(i, N) {
    ll a;
    cin >> a;
    Q.push(a);
  }

  vector<pair<ll, ll>> CB(M);
  rep(i, M) { cin >> CB[i].second >> CB[i].first; }

  sort(CB.rbegin(), CB.rend());

  rep(i, M) {
    rep(j, CB[i].second) {
      if (Q.top() < CB[i].first) {
        ll tmp = Q.top();
        Q.pop();
        Q.push(CB[i].first);
      }
    }
  }

  ll ans = 0;
  while (!Q.empty()) {
    ans += Q.top();
    Q.pop();
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

int main() {
  ll N, M;
  cin >> N >> M;
  priority_queue<ll, vector<ll>, greater<ll>> Q;

  rep(i, N) {
    ll a;
    cin >> a;
    Q.push(a);
  }

  vector<pair<ll, ll>> CB(M);
  rep(i, M) { cin >> CB[i].second >> CB[i].first; }

  sort(CB.rbegin(), CB.rend());

  rep(i, M) {
    rep(j, CB[i].second) {
      if (Q.top() < CB[i].first) {
        ll tmp = Q.top();
        Q.pop();
        Q.push(CB[i].first);
      } else {
        break;
      }
    }
  }

  ll ans = 0;
  while (!Q.empty()) {
    ans += Q.top();
    Q.pop();
  }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, m;
  vector<int> data;
  vector<pair<int, int>> changes;
  long long sum = 0;
  cin >> n;
  cin >> m;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    data.push_back(t);
  }
  sort(data.begin(), data.end());

  for (int i = 0; i < m; i++) {
    int k, t;
    cin >> k;
    cin >> t;
    changes.push_back(make_pair(t, k));
  }
  sort(changes.rbegin(), changes.rend());

  auto ptr = data.begin();
  for (auto it = changes.begin(); it != changes.end();) {
    if (*ptr < (*it).first) {
      sum += (*it).first;
      ++ptr;
      if ((*it).second == 1) {
        ++it;
      } else {
        (*it).second--;
      }
    } else {
      break;
    }
  }
  while (ptr != data.end()) {
    sum += *ptr;
    ++ptr;
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, m;
  vector<int> data;
  vector<pair<int, int>> changes;
  long long sum = 0;
  cin >> n;
  cin >> m;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    data.push_back(t);
  }
  sort(data.begin(), data.end());

  for (int i = 0; i < m; i++) {
    int k, t;
    cin >> k;
    cin >> t;
    changes.push_back(make_pair(t, k));
  }
  sort(changes.rbegin(), changes.rend());

  auto ptr = data.begin();
  for (auto it = changes.begin(); it != changes.end();) {
    if (ptr == data.end()) {
      break;
    }
    if (*ptr < (*it).first) {
      sum += (*it).first;
      ++ptr;
      if ((*it).second == 1) {
        ++it;
      } else {
        (*it).second--;
      }
    } else {
      break;
    }
  }
  while (ptr != data.end()) {
    sum += *ptr;
    ++ptr;
  }
  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef long long ll;
bool debug = false;
string yes = "Yes";
string no = "No";

int main() {
  ll n, m;
  cin >> n >> m;
  priority_queue<ll, vector<ll>, greater<ll>> q;
  rep(i, n) {
    ll a;
    cin >> a;
    q.push(a);
  }
  vector<vector<ll>> v(m, vector<ll>(2, 0));
  rep(j, m) {
    ll b, c;
    cin >> b >> c;
    v[j][1] = b;
    v[j][0] = c;
  }
  sort(v.begin(), v.end(), greater<vector<ll>>());
  rep(k, m) {
    rep(p, v[k][1]) {
      ll t = q.top();
      if (t > v[k][0])
        break;
      q.pop();
      q.push(v[k][0]);
    }
  }
  ll ans = 0;
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef long long ll;
bool debug = false;
string yes = "Yes";
string no = "No";

int main() {
  ll n, m;
  cin >> n >> m;
  priority_queue<ll, vector<ll>, greater<ll>> q;
  rep(i, n) {
    ll a;
    cin >> a;
    q.push(a);
  }
  vector<vector<ll>> v(m, vector<ll>(2, 0));
  rep(j, m) {
    ll b, c;
    cin >> b >> c;
    v[j][1] = b;
    v[j][0] = c;
  }
  sort(v.begin(), v.end(), greater<vector<ll>>());
  rep(k, m) {
    rep(p, v[k][1]) {
      ll t = q.top();
      if (t >= v[k][0])
        break;
      q.pop();
      q.push(v[k][0]);
    }
  }
  ll ans = 0;
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  cout << ans;
  return 0;
}