Problem p00995 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 10000000000000000
int n, m;
ll start[100005];
ll goal[100005];
ll d[100005];
pair<ll, ll> qwe[100005];
struct edge {
  int to;
  ll cost;
  edge(int a, int b) : to(a), cost(b) {}
};
vector<edge> vec[100005];
void dijkstra1() {
  priority_queue<P, vector<P>, greater<P>> que;
  que.pu(mp(0, 0));
  fill(start, start + n, INF);
  start[0] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int V = p.second;
    if (start[V] < p.first)
      continue;
    for (int i = 0; i < vec[V].size(); i++) {
      edge e = vec[V][i];
      if (start[e.to] > start[V] + e.cost) {
        start[e.to] = start[V] + e.cost;
        que.push(mp(start[e.to], e.to));
      }
    }
  }
}
void dijkstra2() {
  priority_queue<P, vector<P>, greater<P>> que;
  que.pu(mp(0, n - 1));
  fill(goal, goal + n, INF);
  goal[n - 1] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int V = p.second;
    if (goal[V] < p.first)
      continue;
    for (int i = 0; i < vec[V].size(); i++) {
      edge e = vec[V][i];
      if (goal[e.to] > goal[V] + e.cost) {
        goal[e.to] = goal[V] + e.cost;
        que.push(mp(goal[e.to], e.to));
      }
    }
  }
}
vector<ll> seg[1 << 18];
ll limits, limite;
void update1(vector<ll> x, int a) { seg[a + (1 << 17) - 1] = x; }
void update() {
  for (int i = (1 << 17) - 2; i >= 0; i--) {
    seg[i].resize(seg[i * 2 + 1].size() + seg[i * 2 + 2].size());
    merge(seg[i * 2 + 1].begin(), seg[i * 2 + 1].end(), seg[i * 2 + 2].begin(),
          seg[i * 2 + 2].end(), seg[i].begin());
  }
}
int sum(int a, int b, int k, int l, int r) {
  if (l > b || a > r) {
    return 0;
  } else if (a <= l && r <= b) {
    int rp = lower_bound(seg[k].begin(), seg[k].end(), limite) - seg[k].begin();
    return seg[k].size() - rp;
  } else {
    return sum(a, b, k * 2 + 1, l, (l + r) / 2) +
           sum(a, b, k * 2 + 2, (l + r) / 2 + 1, r);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    ll c;
    scanf("%d %d %lld", &a, &b, &c);
    vec[a].pb(edge(b, c));
    vec[b].pb(edge(a, c));
  }
  dijkstra1();
  dijkstra2();
  for (int i = 0; i < n; i++) {
    qwe[i] = mp(start[i], goal[i]);
  }
  sort(qwe, qwe + n);
  for (int i = 0; i < n; i++) {
    vector<ll> er;
    er.pb(qwe[i].second);
    update1(er, i);
    start[i] = qwe[i].first;
  }
  update();
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld %lld", &limits, &limite);
    int eo = upper_bound(start, start + n, limits) - start;
    printf("%d\\n", sum(0, eo - 1, 0, 0, (1 << 17) - 1));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 10000000000000000
int n, m;
ll start[100005];
ll goal[100005];
ll d[100005];
pair<ll, ll> qwe[100005];
struct edge {
  int to;
  ll cost;
  edge(int a, int b) : to(a), cost(b) {}
};
vector<edge> vec[100005];
void dijkstra1() {
  priority_queue<P, vector<P>, greater<P>> que;
  que.pu(mp(0, 0));
  fill(start, start + n, INF);
  start[0] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int V = p.second;
    if (start[V] < p.first)
      continue;
    for (int i = 0; i < vec[V].size(); i++) {
      edge e = vec[V][i];
      if (start[e.to] > start[V] + e.cost) {
        start[e.to] = start[V] + e.cost;
        que.push(mp(start[e.to], e.to));
      }
    }
  }
}
void dijkstra2() {
  priority_queue<P, vector<P>, greater<P>> que;
  que.pu(mp(0, n - 1));
  fill(goal, goal + n, INF);
  goal[n - 1] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int V = p.second;
    if (goal[V] < p.first)
      continue;
    for (int i = 0; i < vec[V].size(); i++) {
      edge e = vec[V][i];
      if (goal[e.to] > goal[V] + e.cost) {
        goal[e.to] = goal[V] + e.cost;
        que.push(mp(goal[e.to], e.to));
      }
    }
  }
}
vector<ll> seg[1 << 18];
ll limits, limite;
void update(vector<ll> x, int a) {
  a += (1 << 17) - 1;
  seg[a] = x;
  while (a > 0) {
    a = (a - 1) / 2;
    seg[a].resize(seg[a * 2 + 1].size() + seg[a * 2 + 2].size());
    merge(seg[a * 2 + 1].begin(), seg[a * 2 + 1].end(), seg[a * 2 + 2].begin(),
          seg[a * 2 + 2].end(), seg[a].begin());
  }
}
int sum(int a, int b, int k, int l, int r) {
  if (l > b || a > r) {
    return 0;
  } else if (a <= l && r <= b) {
    int rp = lower_bound(seg[k].begin(), seg[k].end(), limite) - seg[k].begin();
    return seg[k].size() - rp;
  } else {
    return sum(a, b, k * 2 + 1, l, (l + r) / 2) +
           sum(a, b, k * 2 + 2, (l + r) / 2 + 1, r);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    ll c;
    scanf("%d %d %lld", &a, &b, &c);
    vec[a].pb(edge(b, c));
    vec[b].pb(edge(a, c));
  }
  dijkstra1();
  dijkstra2();
  for (int i = 0; i < n; i++) {
    qwe[i] = mp(start[i], goal[i]);
  }
  sort(qwe, qwe + n);
  for (int i = 0; i < n; i++) {
    vector<ll> er;
    er.pb(qwe[i].second);
    update(er, i);
    start[i] = qwe[i].first;
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld %lld", &limits, &limite);
    int eo = upper_bound(start, start + n, limits) - start;
    printf("%d\\n", sum(0, eo - 1, 0, 0, (1 << 17) - 1));
  }
}