Problem p01860 (C++) — buggy submission, verdict: Runtime Error

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

const ll MAX = (1 << 18);
const ll INF = (1 << 29);
ll N, M, K, D, S;
ll A[100001];
ll B[100001];
ll C[100001];

struct state {
  ll a, b, c, k;
  bool operator<(const state st) const {
    if (a == st.a)
      return k > st.k;
    return a > st.a;
  }
};
ll compress() {
  vector<ll> x;
  for (ll i = 0; i < M; i++)
    x.push_back(C[i]);
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (ll i = 0; i < M; i++) {
    C[i] = lower_bound(x.begin(), x.end(), C[i]) - x.begin();
  }
  return x.size();
}
typedef pair<ll, ll> P;
struct seg {
  ll n, d[MAX + 1];

  void init(ll _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    memset(d, 0, sizeof(d));
  }

  // k番目の値をaに変更
  void update(ll k, ll a) {
    k += n - 1;
    d[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      d[k] = min(d[k * 2 + 1], d[k * 2 + 2]);
    }
  }

  // [a,b) の最小値を求める
  ll query(ll a, ll b) { return query(a, b, 0, 0, n); }
  ll query(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return d[k];
    else {
      ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};

seg Seg;
int main() {
  // cin >> N >> M >> K >>D >> S;
  scanf("%d %d %d %d %d", &N, &M, &K, &D, &S);
  for (ll i = 0; i < M; i++) {
    // cin >> A[i] >> B[i] >> C[i];
    scanf("%d %d %d", &A[i], &B[i], &C[i]);
  }
  ll n = compress();
  if (n < N)
    N = n + 1;
  // cout << N << endl;

  priority_queue<state> q;
  for (ll i = 0; i < M; i++) {
    q.push((state){A[i], B[i], C[i], 0});
  }
  q.push((state){S + D, 0, 0, 1});

  Seg.init(N);

  ll co = 0;
  ll res = 0;
  while (co < K) {
    state p = q.top();
    q.pop();
    ll now = p.a;
    //   cout << p.a << " " << p.b << " " << p.c <<" "<< p.k << endl;
    if (p.k == 0) {
      ll id = p.c;
      ll time = Seg.query(id, id + 1);
      Seg.update(id, max(time, now) + p.b);
      //  cout << id <<" -> " << max(time,now)+p.b << endl;
    } else {
      ll time = Seg.query(0, N);
      ll nex = max(time, now) + D;
      // cout << time << endl;
      co++;
      res = max(time, now);
      q.push((state){nex, 0, 0, 1});
    }
  }
  // cout << res-S << endl;
  printf("%d\\n", res - S);
}

Error: 0

Fixed submission:

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

const int MAX = (1 << 18);
const int INF = (1 << 29);
int N, M, K, D, S;
int A[100001];
int B[100001];
int C[100001];

struct state {
  int a, b, c, k;
  bool operator<(const state st) const {
    if (a == st.a)
      return k > st.k;
    return a > st.a;
  }
};
int compress() {
  vector<int> x;
  for (int i = 0; i < M; i++)
    x.push_back(C[i]);
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (int i = 0; i < M; i++) {
    C[i] = lower_bound(x.begin(), x.end(), C[i]) - x.begin();
  }
  return x.size();
}
typedef pair<int, int> P;
struct seg {
  int n, d[MAX + 1];

  void init(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    memset(d, 0, sizeof(d));
  }

  // k番目の値をaに変更
  void update(int k, int a) {
    k += n - 1;
    d[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      d[k] = min(d[k * 2 + 1], d[k * 2 + 2]);
    }
  }

  // [a,b) の最小値を求める
  int query(int a, int b) { return query(a, b, 0, 0, n); }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return d[k];
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};

seg Seg;
int main() {
  // cin >> N >> M >> K >>D >> S;
  scanf("%d %d %d %d %d", &N, &M, &K, &D, &S);
  for (int i = 0; i < M; i++) {
    // cin >> A[i] >> B[i] >> C[i];
    scanf("%d %d %d", &A[i], &B[i], &C[i]);
  }
  int n = compress();
  if (n > N)
    N = n + 1;
  // cout << N << endl;

  priority_queue<state> q;
  for (int i = 0; i < M; i++) {
    q.push((state){A[i], B[i], C[i], 0});
  }
  q.push((state){S + D, 0, 0, 1});

  Seg.init(N);

  int co = 0;
  int res = 0;
  while (co < K) {
    state p = q.top();
    q.pop();
    int now = p.a;
    //   cout << p.a << " " << p.b << " " << p.c <<" "<< p.k << endl;
    if (p.k == 0) {
      int id = p.c;
      int time = Seg.query(id, id + 1);
      Seg.update(id, max(time, now) + p.b);
      //  cout << id <<" -> " << max(time,now)+p.b << endl;
    } else {
      int time = Seg.query(0, N);
      int nex = max(time, now) + D;
      // cout << time << endl;
      co++;
      res = max(time, now);
      q.push((state){nex, 0, 0, 1});
    }
  }
  // cout << res-S << endl;
  printf("%d\\n", res - S);
}