alexjercan/bugnet huggingface dataset:
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<int> vis(1000001, 0);

int flag;

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int findGCD(vector<int> arr, int n) {
  int result = arr[0];
  for (int i = 1; i < n; i++) {
    result = gcd(arr[i], result);

    if (result == 1) {
      return 1;
    }
  }
  return result;
}

void primeFactors(int n) {
  if (n % 2 == 0) {
    if (vis[2] == 0) {
      vis[2] = 1;

    } else {
      flag = 1;
      return;
    }
  }
  while (n % 2 == 0) {
    n = n / 2;
  }

  for (int i = 3; i <= sqrt(n); i = i + 2) {
    if (n % i == 0) {
      if (vis[i] == 0) {
        vis[i] = 1;
      } else {
        flag = 1;
        return;
      }
    }

    while (n % i == 0) {
      n = n / i;
    }
  }

  if (n > 2) {
    if (vis[n] == 0) {
      vis[n] = 1;
    } else {
      flag = 1;
      return;
    }
  }
}
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  flag = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < n; i++) {
    if (flag == 1) {
      break;
    }
    primeFactors(v[i]);
  }
  if (flag == 0) {
    cout << "pairwise coprime";
    return;
  }
  if (findGCD(v, v.size()) == 1) {
    cout << "setwise coprime";
    return;
  }
  cout << "not coprime";
  return;
}

int main() {

  int t;
  // cin>>t;
  t = 1;
  while (t--) {

    solve();
    cout << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> vis(100001, 0);

int flag;

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int findGCD(vector<int> arr, int n) {
  int result = arr[0];
  for (int i = 1; i < n; i++) {
    result = gcd(arr[i], result);

    if (result == 1) {
      return 1;
    }
  }
  return result;
}

void primeFactors(int n) {
  if (n % 2 == 0) {
    if (vis[2] == 0) {
      vis[2] = 1;

    } else {
      flag = 1;
      return;
    }
  }
  while (n % 2 == 0) {
    n = n / 2;
  }

  for (int i = 3; i <= sqrt(n); i = i + 2) {
    if (n % i == 0) {
      if (vis[i] == 0) {
        vis[i] = 1;
      } else {
        flag = 1;
        return;
      }
    }

    while (n % i == 0) {
      n = n / i;
    }
  }

  if (n > 2) {
    if (vis[n] == 0) {
      vis[n] = 1;
    } else {
      flag = 1;
      return;
    }
  }
}
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  flag = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < n; i++) {
    if (flag == 1) {
      break;
    }
    primeFactors(v[i]);
  }
  if (flag == 0) {
    cout << "pairwise coprime";
    return;
  }
  if (findGCD(v, v.size()) == 1) {
    cout << "setwise coprime";
    return;
  }
  cout << "not coprime";
  return;
}

int main() {

  int t;
  // cin>>t;
  t = 1;
  while (t--) {

    solve();
    cout << endl;
  }
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define PII pair<int, int>
#define INF LONG_MAX
#define ull unsigned long long
#define long long long
#define PIL pair<long, int>
#define M 1000000007
#define endl "\\n"

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

void findFactor(int el, unordered_set<int> &f, bool *flag) {
  for (int i = 2; i * i <= el; i++) {
    if (el % i == 0) {
      if (el / i == i) {
        if (f.find(i) == f.end()) {
          f.insert(i);
        } else {
          *flag = true;
          return;
        }
      } else {
        if (f.find(i) != f.end() || f.find(el / i) != f.end()) {
          *flag = true;
          return;
        } else {
          f.insert(i);

          f.insert(el / i);
        }
      }
    }
  }
}
bool pairWise(int ar[], int n) {
  unordered_set<int> factors;
  bool flag = false;
  for (int i = 0; i < n; i++) {
    if (ar[i] == 1)
      continue;
    if (factors.find(ar[i]) != factors.end())
      return false;
    findFactor(ar[i], factors, &flag);
    factors.insert(ar[i]);
    if (flag == true) {
      return false;
    }
  }
  return true;
}

int main() {
  fastIO();
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  int allgcd = ar[0];
  for (int i = 1; i < n; i++) {
    allgcd = __gcd(allgcd, ar[i]);
  }

  if (allgcd != 1) {
    cout << "not coprime";

  } else {
    if (pairWise(ar, n)) {
      cout << "pairwise coprime";

    } else {
      cout << "setwise coprime";
    }
  }
}


Error: TLE

Fixed submission:

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

#define rep(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define PII pair<int, int>
#define INF LONG_MAX
#define ull unsigned long long
#define long long long
#define PIL pair<long, int>
#define M 1000000007
#define endl "\\n"

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

bool pairWise(int arr[], int n) {
  int count = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {

      if (__gcd(arr[i], arr[j]) != 1)
        return false;
    }
  }
  return true;
}

int main() {
  fastIO();
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  if (pairWise(ar, n)) {
    cout << "pairwise coprime";
    return 0;
  }
  int allgcd = ar[0];
  for (int i = 1; i < n; i++) {
    allgcd = __gcd(allgcd, ar[i]);
  }

  if (allgcd == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
int a[1000005];
unordered_map<int, int> mp;
int num[1000010];
int main() {
#ifdef LOCAL
  freopen("in1.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif

  int n;
  cin >> n;

  // bool cjs = false;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // if(a[i] == 1) cjs = true;
    mp[a[i]] = 1;
    num[a[i]]++;
  }
  //    if(cjs) {
  //        puts("not coprime");
  //        exit(0);
  //    }
  bool dyh = false;
  for (int i = 2; i < 1000005; i++) {
    bool flag = false;
    for (int j = 1; i * j < 1000010; j++) {
      if (mp[i * j] == 1 and num[i * j] > 1) {
        dyh = true;
        break;
      }
      if (mp[i * j] == 1 and flag) {
        dyh = true;
        break;
      }
      if (mp[i * j] == 1 and !flag)
        flag = true;
    }
  }
  if (dyh) {
    int tmp = a[0];
    for (int i = 1; i < n; i++) {
      tmp = __gcd(tmp, a[i]);
    }
    if (tmp == 1) {
      puts("setwise coprime");
    } else
      puts("not coprime");
  } else {
    puts("pairwise coprime");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
int a[1000005];
unordered_map<int, int> mp;
int num[1000005];
int main() {
#ifdef LOCAL
  freopen("in1.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif

  int n;
  cin >> n;

  // bool cjs = false;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // if(a[i] == 1) cjs = true;
    mp[a[i]] = 1;
    num[a[i]]++;
  }
  //    if(cjs) {
  //        puts("not coprime");
  //        exit(0);
  //    }
  bool dyh = false;
  for (int i = 2; i < 1000005; i++) {
    bool flag = false;
    for (int j = 1; i * j < 1000010; j++) {
      if (mp[i * j] == 1 and num[i * j] > 1) {
        dyh = true;
        break;
      }
      if (mp[i * j] == 1 and flag) {
        dyh = true;
        break;
      }
      if (mp[i * j] == 1 and !flag)
        flag = true;
    }
  }
  if (dyh) {
    int tmp = a[0];
    for (int i = 1; i < n; i++) {
      tmp = __gcd(tmp, a[i]);
    }
    if (tmp == 1) {
      puts("setwise coprime");
    } else
      puts("not coprime");
  } else {
    puts("pairwise coprime");
  }
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
#define pll pair<ll, ll>
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll a[1100000];
map<ll, ll> x;
int main() {
  ll n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  bool pc = true;
  bool sc = false;
  rep(i, 0, n) {
    ll p = a[i];
    ll cnt = 2;
    while (cnt * cnt <= p) {
      if (p % cnt == 0) {
        while (p % cnt == 0) {
          p /= cnt;
        }
        x[cnt]++;
        if (x[cnt] > 1)
          pc = false;
      } else
        cnt++;
    }
    if (p > 1)
      x[p]++;
    if (x[p] > 1)
      pc = false;
    if (!pc)
      break;
  }
  if (!pc) {
    ll q = a[0];
    rep(i, 1, n) q = gcd(q, a[i]);
    if (q == 1)
      sc = true;
  }
  if (pc)
    cout << "pairwise coprime";
  else if (sc)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
#define pll pair<ll, ll>
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll a[1100000];
map<ll, ll> x;
map<ll, ll> y[1100000];
int main() {
  ll n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) {
    ll p = a[i];
    ll cnt = 2;
    while (cnt * cnt <= p) {
      if (p % cnt == 0) {
        x[cnt]++;
        p /= cnt;
        y[i][cnt]++;
      } else
        cnt++;
    }
    if (p > 1) {
      x[p]++;
      y[i][p]++;
    }
  }
  bool pc = true;
  bool sc = false;
  rep(i, 0, n) {
    for (auto k : y[i]) {
      ll pos = k.first;
      ll pos2 = k.second;
      if (x[pos] > pos2)
        pc = false;
    }
  }
  if (!pc) {
    ll q = a[0];
    rep(i, 1, n) q = gcd(q, a[i]);
    if (q == 1)
      sc = true;
  }
  if (pc)
    cout << "pairwise coprime";
  else if (sc)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  int H, W;
  cin >> H >> W;

  map<int, int> mp;
  multiset<int> ms;

  rep(i, W) mp[i] = i;
  rep(_, W) ms.insert(0);

  rep(i, H) {
    int l, r;
    cin >> l >> r;
    --l;
    auto it = mp.lower_bound(l);
    int R = -1;

    while (it != mp.end() && it->first <= r) {
      R = max(R, it->second);
      int x = it->first - it->second;
      ms.erase(ms.find(x));
      it = mp.erase(it);
    }

    if (R != -1 && r < W) {
      ms.insert(r - R);
      mp[r] = R;
    }

    int ans = -1;
    if (ms.size() > 0)
      ans = *ms.begin() + i + 1;

    cout << ans << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  int H, W;
  cin >> H >> W;

  vec<ll> ans(W, 0);
  vec<ll> A(H), B(H);
  rep(i, H) {
    ll A, B;
    cin >> A >> B;
    --A, --B;
    ll mn = 1e18;
    rep(i, W) {
      if (i < A || B < i) {
        ans[i] = min(ans[i] + 1, ans[(ll)max(i - 1, 0LL)] + 1);
      } else {
        if (i == 0)
          ans[i] = 1e18;
        else
          ans[i] = ans[i - 1] + 1;
      }
      mn = min(mn, ans[i]);
    }
    cout << ((mn == 1e18) ? -1 : mn) << '\\n';
  }
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define int long long
#define db(a, ...) cerr << #a << " = " << a << '\\n'
#define db2(a, b) cerr << #a << " = " << a << ", ", db(b)
#define db3(a, b, c) cerr << #a << " = " << a << ", ", db2(b, c)
#define db4(a, b, c, d) cerr << #a << " = " << a << ", ", db3(b, c, d)

using namespace std;

typedef pair<int, int> ii;

const int INF = 1e9, MAX = 2e5 + 5;

int n, k;
int d[MAX];

int32_t main() {
#ifdef ACM
  freopen("D.in", "r", stdin);
#endif
  cin >> n >> k;
  multiset<int> S1;
  multiset<ii> S2;
  for (int i = 1; i <= k; i++) {
    S1.insert(0);
    S2.insert({i, 0});
  }
  int l, r;
  for (int i = 1; i <= n; i++) {
    cin >> l >> r;
    int minD = INF;
    vector<ii> V;
    for (auto it = S2.lower_bound({l, 0}); it != S2.end() && it->first <= r;
         it++) {
      int newD = it->second + r - it->first + 1;
      minD = min(minD, newD);
      V.emplace_back(it->second, it->first);
    }
    for (ii it : V) {
      S1.erase(S1.find(it.first));
      S2.erase(S2.find({it.second, it.first}));
    }
    if (r < k && minD < INF) {
      d[r + 1] = minD;
      S1.insert(minD);
      S2.insert({r + 1, d[r + 1]});
    }
    if (S1.size() == 0)
      cout << -1 << '\\n';
    else
      cout << *S1.begin() + i << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define db(a, ...) cerr << #a << " = " << a << '\\n'
#define db2(a, b) cerr << #a << " = " << a << ", ", db(b)
#define db3(a, b, c) cerr << #a << " = " << a << ", ", db2(b, c)
#define db4(a, b, c, d) cerr << #a << " = " << a << ", ", db3(b, c, d)

using namespace std;

typedef pair<int, int> ii;

const int INF = 1e9, MAX = 2e5 + 5;

int n, k;
int d[MAX];

int32_t main() {
#ifdef ACM
  freopen("D.in", "r", stdin);
#endif
  cin >> n >> k;
  multiset<int> S1, S2;
  for (int i = 1; i <= k; i++) {
    S1.insert(0);
    S2.insert(i);
  }
  int l, r;
  for (int i = 1; i <= n; i++) {
    cin >> l >> r;
    int minD = INF;
    vector<ii> V;
    for (auto it = S2.lower_bound(l); it != S2.end() && *it <= r; it++) {
      int newD = d[*it] + r - *it + 1;
      minD = min(minD, newD);
      V.emplace_back(d[*it], *it);
    }
    for (ii it : V) {
      S1.erase(S1.find(it.first));
      S2.erase(S2.find(it.second));
    }
    if (r < k && minD < INF) {
      d[r + 1] = minD;
      S1.insert(minD);
      S2.insert(r + 1);
    }
    if (S1.size() == 0)
      cout << -1 << '\\n';
    else
      cout << *S1.begin() + i << '\\n';
  }
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  size_t H, W;
  cin >> H >> W;

  vector<size_t> A(H);
  vector<size_t> B(H);
  for (size_t i = 0; i < H; i++) {
    cin >> A[i] >> B[i];
  }

  set<pair<size_t, size_t>> goal_start;
  multiset<int> ans;
  for (size_t i = 1; i <= W; i++) {
    goal_start.insert(make_pair(i, i));
    ans.insert(0);
  }

  for (size_t i = 0; i < H; i++) {
    size_t start = 0;
    size_t right = 0;
    for (set<pair<size_t, size_t>>::iterator itr =
             goal_start.lower_bound(make_pair(A[i], 0));
         itr != goal_start.end() && itr->first <= B[i];) {
      size_t goal = itr->first;
      start = itr->second;
      itr = goal_start.erase(itr);
      ans.erase(ans.find(goal - start));
      right = max(right, start);
    }
    if (start > 0 && B[i] < W) {
      goal_start.insert(make_pair(B[i] + 1, right));
      ans.insert(B[i] + 1 - right);
    }
    if (ans.empty()) {
      cout << -1 << endl;
    } else {
      cout << i + 1 + *ans.begin() << endl;
    }
  }
}


Error: 0

Fixed submission:

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

int main() {
  size_t H, W;
  cin >> H >> W;

  vector<size_t> A(W);
  vector<size_t> B(W);
  for (size_t i = 0; i < W; i++) {
    cin >> A[i] >> B[i];
  }

  set<pair<size_t, size_t>> goal_start;
  multiset<int> ans;
  for (size_t i = 1; i <= W; i++) {
    goal_start.insert(make_pair(i, i));
    ans.insert(0);
  }

  for (size_t i = 0; i < H; i++) {
    size_t start = 0;
    size_t right = 0;
    for (set<pair<size_t, size_t>>::iterator itr =
             goal_start.lower_bound(make_pair(A[i], 0));
         itr != goal_start.end() && itr->first <= B[i];) {
      size_t goal = itr->first;
      start = itr->second;
      itr = goal_start.erase(itr);
      ans.erase(ans.find(goal - start));
      right = max(right, start);
    }
    if (start > 0 && B[i] < H) {
      goal_start.insert(make_pair(B[i] + 1, right));
      ans.insert(B[i] + 1 - right);
    }
    if (ans.empty()) {
      cout << -1 << endl;
    } else {
      cout << i + 1 + *ans.begin() << endl;
    }
  }
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

int main() {
  int h, w;
  cin >> h >> w;

  map<int, int> ns;   // （現在地、スタート地点の最大値）
  multiset<int> dist; // 現在地ースタート地点の最大値

  rep(i, w) ns[i] = i;      // キーiにiを代入
  rep(i, w) dist.insert(0); // はじめは距離0

  rep(i, h) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;

    auto it = ns.lower_bound(a);
    int ms = -1; // 最も右にある開始点を保持
    while (1) {
      // 壁を1だけ超えるスタート地点を取得（b+1）
      if (it == ns.end() || it->first > b + 1)
        break;
      ms = max(ms, it->second);
      dist.erase(dist.find(it->first - it->second));
      ns.erase(it++);
    }
    // 要素が見つからない、wを超えてしまう時を除く。
    if (ms != -1 && b + 1 != w) {
      dist.insert(b + 1 - ms);
      ns[b + 1] = ms;
    }

    int ans = -1;
    if (dist.size() > 0) {
      ans = *dist.begin();
      ans += i + 1;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

int main() {
  int h, w;
  cin >> h >> w;
  vector<P> wall;

  rep(i, h) rep(i, w) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    wall.emplace_back(a, b);
  }

  map<int, int> ns;   // （現在地、スタート地点の最大値）
  multiset<int> dist; // 現在地ースタート地点の最大値

  rep(i, w) ns[i] = i;      // キーiにiを代入
  rep(i, w) dist.insert(0); // はじめは距離0

  rep(i, h) {
    int a, b;
    a = wall[i].first;
    b = wall[i].second;

    auto it = ns.lower_bound(a);
    int ms = -1; // 最も右にある開始点を保持
    while (1) {
      // 壁を1だけ超えるスタート地点を取得（b+1）
      if (it == ns.end() || it->first > b + 1)
        break;
      ms = max(ms, it->second);
      dist.erase(dist.find(it->first - it->second));
      ns.erase(it++);
    }
    // 要素が見つからない、wを超えてしまう時を除く。
    if (ms != -1 && b + 1 != w) {
      dist.insert(b + 1 - ms);
      ns[b + 1] = ms;
    }

    int ans = -1;
    if (dist.size() > 0) {
      ans = *dist.begin();
      ans += i + 1;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

const int MAX_H = 200000;
int H, W;
vector<int> A(MAX_H), B(MAX_H);

int main() {
  cin >> H >> W;
  rep(i, H) { cin >> A[i] >> B[i]; }

  // map[現在位置] = 開始位置
  map<int, int> mp;
  rep(i, W) mp[i] = i;
  // (現在位置-開始位置）を保持
  multiset<int> st;
  rep(i, W) st.insert(0);

  rep(i, H) {
    int l = A[i];
    l--;
    int r = B[i];
    auto it = mp.lower_bound(l);
    int mr = -1;
    while (it != mp.end() && it->first <= r) {
      mr = max(mr, it->second);
      // 下にいけないルートは消す
      int x = it->first - it->second;
      st.erase(st.find(x));
      mp.erase(it++);
    }
    if (mr != -1 && r < W) {
      st.insert(r - mr);
      mp[r] = mr;
    }

    // 最短
    int ans = -1;
    if (st.size() > 0) {
      ans = *st.begin() + (i + 1);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int H, W;
int A[200001], B[200001];
long long dp[200001]; // 最小移動回数を保存
const long long INF = 10000000;
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> A[i] >> B[i];
    --A[i];
    --B[i];
  }

  for (int i = 0; i < W; i++)
    dp[i] = 0;
  // 一番上の行から探索していく
  for (int i = 0; i < H; i++) {
    long long ans = INF;
    // i + 1 行目の調査
    for (int j = 0; j < W; j++) {
      // 上から移動できるか　
      if (j < A[i] || B[i] < j) {
        dp[j] += 1;
      } else {
        dp[j] = INF;
      }

      // 左から移動する
      if (0 < j) {
        dp[j] = min(dp[j - 1] + 1, dp[j]);
      }

      // 答え更新
      ans = min(ans, dp[j]);
    }

    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
  int h, w;
  cin >> h >> w;
  VI a(h), b(h);
  rep(i, h) {
    cin >> a[i] >> b[i];
    a[i]--;
  }
  multiset<int> num;
  rep(i, w) num.insert(0);
  VI dp(w);
  set<int> alive;
  rep(i, w) alive.insert(i);
  rep(i, h) {
    while (1) {
      auto it = alive.lower_bound(a[i]);
      if (it == alive.end() || *it >= b[i])
        break;
      int k = *it;
      assert(num.find(dp[k]) != num.end());
      alive.erase(k);
      num.erase(num.find(dp[k]));
      if (b[i] == w) {
        ;
      } else if (alive.count(b[i]) == 0) {
        alive.insert(b[i]);
        dp[b[i]] = dp[k] + b[i] - k;
        num.insert(dp[k] + b[i] - k);
      } else {
        if (dp[k] + b[i] - k < dp[b[i]]) {
          num.erase(num.find(dp[b[i]]));
          num.insert(dp[k] + b[i] - k);
          dp[b[i]] = dp[k] + b[i] - k;
        }
      }
      a[i] = k + 1;
    }
    if (num.empty())
      cout << -1 << endl;
    else
      cout << *num.begin() + i + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
  int h, w;
  cin >> h >> w;
  VI a(h), b(h);
  rep(i, h) {
    cin >> a[i] >> b[i];
    a[i]--;
  }
  multiset<int> num;
  rep(i, w) num.insert(0);
  VI dp(w);
  set<int> alive;
  rep(i, w) alive.insert(i);
  rep(i, h) {
    while (1) {
      auto it = alive.lower_bound(a[i]);
      if (it == alive.end() || *it >= b[i])
        break;
      int k = *it;
      assert(num.find(dp[k]) != num.end());
      alive.erase(k);
      num.erase(num.find(dp[k]));
      if (b[i] == w) {
        ;
      } else if (alive.count(b[i]) == 0) {
        alive.insert(b[i]);
        dp[b[i]] = dp[k] + b[i] - k;
        num.insert(dp[k] + b[i] - k);
      } else {
        if (dp[k] + b[i] - k < dp[b[i]]) {
          num.erase(dp[b[i]]);
          num.insert(dp[k] + b[i] - k);
          dp[b[i]] = dp[k] + b[i] - k;
        }
      }
      a[i] = k + 1;
    }
    if (num.empty())
      cout << -1 << endl;
    else
      cout << *num.begin() + i + 1 << endl;
  }
}