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

#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int64_t n, m;
  int64_t a, b, c;
  int64_t q;
  int64_t fs, fg;
  int64_t inf = 10000000000000;
  int64_t chunk_size = 512;
  cin >> n >> m;
  vector<unordered_map<int64_t, int64_t>> edges(n);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    if (edges[a].find(b) != edges[a].end()) {
      if (c < edges[a][b]) {
        edges[a].erase(b);
        edges[b].erase(a);
      }
    }
    edges[a].emplace(b, c);
    edges[b].emplace(a, c);
  }
  vector<pair<int64_t, int64_t>> costs(n);
  for (int i = 0; i < n; ++i) {
    costs[i].first = inf;
    costs[i].second = -1;
  }
  priority_queue<pair<int64_t, int64_t>> pq;
  pq.emplace(0, 0);
  while (!pq.empty()) {
    int64_t cost = -pq.top().first;
    int64_t id = pq.top().second;
    pq.pop();
    if (costs[id].first != inf) {
      continue;
    }
    costs[id].first = cost;
    for (auto &edge : edges[id]) {
      if (costs[edge.first].first != inf) {
        continue;
      }
      pq.emplace(-(cost + edge.second), edge.first);
    }
  }
  pq.emplace(0, n - 1);
  while (!pq.empty()) {
    int64_t cost = -pq.top().first;
    int64_t id = pq.top().second;
    pq.pop();
    if (costs[id].second != -1) {
      continue;
    }
    costs[id].second = cost;
    for (auto &edge : edges[id]) {
      if (costs[edge.first].second != -1) {
        continue;
      }
      pq.emplace(-(cost + edge.second), edge.first);
    }
  }
  sort(costs.begin(), costs.end());
  vector<vector<int64_t>> chunked_cost;
  for (int i = 0; i < n; i += chunk_size) {
    vector<int64_t> chunk(chunk_size, -1);
    for (int j = i; j < min(i + chunk_size, n); ++j) {
      chunk[j - i] = costs[j].second;
    }
    sort(chunk.begin(), chunk.end());
    chunked_cost.push_back(chunk);
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> fs >> fg;
    int cnt = 0;
    int nmin_fs = upper_bound(costs.begin(), costs.end(),
                              pair<int64_t, int64_t>{fs, inf}) -
                  costs.begin();
    for (int j = 0; j < nmin_fs / chunk_size; j++) {
      cnt += chunked_cost[j].end() -
             lower_bound(chunked_cost[j].begin(), chunked_cost[j].end(), fg);
    }
    for (int j = nmin_fs / chunk_size * chunk_size; j < nmin_fs; ++j) {
      if (costs[j].second >= fg) {
        ++cnt;
      }
    }
    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

int main() {
  std::vector<std::vector<std::pair<int, int>>> edges;
  std::priority_queue<std::pair<int, int>> queue;

  int n, m;
  int a, b, c;
  int q;
  long long fs, fg;

  std::cin >> n >> m;
  edges.resize(n);
  for (int i = 0; i < m; ++i) {
    std::cin >> a >> b >> c;
    edges[a].emplace_back(b, c);
    edges[b].emplace_back(a, c);
  }

  std::vector<long long> distance_s(n);
  std::vector<std::pair<long long, int>> distance_g(n);

  queue.emplace(0, 0);
  while (!queue.empty()) {
    auto p = queue.top();
    queue.pop();
    if (!distance_s[p.second]) {
      distance_s[p.second] = -p.first;
      for (auto &target : edges[p.second]) {
        if (target.first != 0) {
          if (!distance_s[target.first]) {
            queue.emplace(p.first - target.second, target.first);
          }
        }
      }
    }
  }

  queue.emplace(0, n - 1);
  while (!queue.empty()) {
    auto p = queue.top();
    queue.pop();
    if (!distance_g[p.second].first) {
      distance_g[p.second].first = -p.first;
      distance_g[p.second].second = p.second;
      for (auto &target : edges[p.second]) {
        if (target.first != n - 1) {
          if (!distance_g[target.first].first) {
            queue.emplace(p.first - target.second, target.first);
          }
        }
      }
    }
  }

  std::sort(distance_g.begin(), distance_g.end());

  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    int cnt = 0;
    std::cin >> fs >> fg;
    for (auto it = std::lower_bound(distance_g.begin(), distance_g.end(),
                                    std::make_pair(fg, 0),
                                    [](const std::pair<long long, int> &a,
                                       const std::pair<long long, int> &b) {
                                      return a.first < b.first;
                                    });
         it != distance_g.end(); ++it) {
      if (distance_s[it->second] <= fs) {
        cnt += 1;
      }
    }
    std::cout << cnt << std::endl;
  }
  //   for (int i = 0; i < q; ++i) {
  //     int cnt = 0;
  //     std::cin >> fs >> fg;
  //     for (int j = 0; j < n; ++j) {
  //       if (distance_s[j] <= fs && distance_g[j].first >= fg) {
  //         cnt += 1;
  //       }
  //     }
  //     std::cout << cnt << std::endl;
  //   }
  return 0;
}