Problem p02868 (C++)

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

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

const int N = 110000;
typedef long long LL;
const LL mod = 1e9 + 7;

int l[N], r[N];
int a[N], b[N], c[N];
int n, m;
vector<int> g;
vector<pair<int, LL>> f[N * 8];
priority_queue<pair<LL, int>, vector<pair<LL, int>>, greater<pair<LL, int>>> q;
const LL INF = 1e15;
LL dis[N];
int pp[N];
int idx[N];
int maxx;
void dijkstra(int x, int n) {
  int i, u;
  for (i = 1; i <= n; ++i)
    dis[i] = INF, pp[i] = 0;
  dis[x] = 0;
  for (i = 1; i <= n; ++i)
    q.push(make_pair(dis[i], i));
  while (!q.empty()) {
    u = q.top().second;
    q.pop();
    if (pp[u])
      continue;
    pp[u] = 1;
    for (i = 0; i < f[u].size(); i++)
      if (dis[f[u][i].fi] > dis[u] + f[u][i].se) {
        dis[f[u][i].fi] = dis[u] + f[u][i].se;
        q.push(make_pair(dis[f[u][i].fi], f[u][i].fi));
      }
  }
}
void build(int k, int l, int r) {
  maxx = max(maxx, k);
  if (l == r) {
    idx[l] = k;
    return;
  }
  int mid = (l + r) / 2;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void build2(int k, int l, int r) {
  f[k + maxx].pb(mp(k, 0));
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  build2(k << 1, l, mid), f[k << 1].pb(mp(k, 0)),
      f[k + maxx].pb(mp((k << 1) + maxx, 0));
  build2(k << 1 | 1, mid + 1, r), f[k << 1 | 1].pb(mp(k, 0)),
      f[k + maxx].pb(mp((k << 1 | 1) + maxx, 0));
}
void add(int k, int l, int r, int a, int b, int c) {
  if (l == a && r == b) {
    g.pb(k);
    return;
  }
  int mid = (l + r) / 2;
  if (b <= mid)
    add(k << 1, l, mid, a, b, c);
  else if (a > mid)
    add(k << 1 | 1, mid + 1, r, a, b, c);
  else
    add(k << 1, l, mid, a, mid, c), add(k << 1 | 1, mid + 1, r, mid + 1, b, c);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    l[i] = i - 1, r[i] = i + 1;
  r[0] = 1, l[n + 1] = n;
  build(1, 1, n);
  build2(1, 1, n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    g.clear();
    add(1, 1, n, a[i], b[i], i);
    for (int j = 0; j < g.size(); j++)
      for (int k = 0; k < g.size(); k++)
        f[g[j]].pb(mp(g[k] + maxx, c[i]));
  }
  dijkstra(idx[1], maxx + maxx);
  if (dis[idx[n]] == INF)
    cout << -1 << endl;
  else
    cout << dis[idx[n]] << endl;
  return 0;
}

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

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

const int N = 110000;
typedef long long LL;
const LL mod = 1e9 + 7;

int l[N], r[N];
int a[N], b[N], c[N];
int n, m;
vector<int> g;
vector<pair<int, LL>> f[N * 8];
priority_queue<pair<LL, int>, vector<pair<LL, int>>, greater<pair<LL, int>>> q;
const LL INF = 1e15;
LL dis[N * 8];
int pp[N * 8];
int idx[N];
int maxx;
void dijkstra(int x, int n) {
  int i, u;
  for (i = 1; i <= n; ++i)
    dis[i] = INF, pp[i] = 0;
  dis[x] = 0;
  for (i = 1; i <= n; ++i)
    q.push(make_pair(dis[i], i));
  while (!q.empty()) {
    u = q.top().second;
    q.pop();
    if (pp[u])
      continue;
    pp[u] = 1;
    for (i = 0; i < f[u].size(); i++)
      if (dis[f[u][i].fi] > dis[u] + f[u][i].se) {
        dis[f[u][i].fi] = dis[u] + f[u][i].se;
        q.push(make_pair(dis[f[u][i].fi], f[u][i].fi));
      }
  }
}
void build(int k, int l, int r) {
  maxx = max(maxx, k);
  if (l == r) {
    idx[l] = k;
    return;
  }
  int mid = (l + r) / 2;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void build2(int k, int l, int r) {
  f[k + maxx].pb(mp(k, 0));
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  build2(k << 1, l, mid), f[k << 1].pb(mp(k, 0)),
      f[k + maxx].pb(mp((k << 1) + maxx, 0));
  build2(k << 1 | 1, mid + 1, r), f[k << 1 | 1].pb(mp(k, 0)),
      f[k + maxx].pb(mp((k << 1 | 1) + maxx, 0));
}
void add(int k, int l, int r, int a, int b, int c) {
  if (l == a && r == b) {
    g.pb(k);
    return;
  }
  int mid = (l + r) / 2;
  if (b <= mid)
    add(k << 1, l, mid, a, b, c);
  else if (a > mid)
    add(k << 1 | 1, mid + 1, r, a, b, c);
  else
    add(k << 1, l, mid, a, mid, c), add(k << 1 | 1, mid + 1, r, mid + 1, b, c);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    l[i] = i - 1, r[i] = i + 1;
  r[0] = 1, l[n + 1] = n;
  build(1, 1, n);
  build2(1, 1, n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    g.clear();
    add(1, 1, n, a[i], b[i], i);
    for (int j = 0; j < g.size(); j++)
      for (int k = 0; k < g.size(); k++)
        f[g[j]].pb(mp(g[k] + maxx, c[i]));
  }
  dijkstra(idx[1], maxx + maxx);
  if (dis[idx[n]] == INF)
    cout << -1 << endl;
  else
    cout << dis[idx[n]] << endl;
  return 0;
}