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

#include <algorithm>
#include <bitset>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
const int N = 1e5 + 10;
const ll INF = 1LL << 60;
ll now = 12394129385729LL;
struct node {
  node *l;
  node *r;
  int size;
  ll val;
  node(ll n) {
    l = r = nullptr;
    size = 1;
    val = n;
  }
  void pull() {
    size = 1;
    if (l)
      size += l->size;
    if (r)
      size += r->size;
    return;
  }
};
bool ran(int a, int b) {
  now ^= 12394817923847LL;
  now += 12937417834292LL;
  return (now %= (a + b)) >= b;
}
node *merge(node *a, node *b) {
  if (!a)
    return b;
  if (!b)
    return a;
  if (ran(a->size, b->size)) {
    a->r = merge(a->r, b);
    a->pull();
    return a;
  } else {
    b->l = merge(a, b->l);
    b->pull();
    return b;
  }
}
void split(node *s, ll x, node *&a, node *&b) {
  if (!s) {
    a = b = nullptr;
    return;
  }
  if (x > s->val) {
    a = s;
    split(s->r, x, a->r, b);
    a->pull();
  } else {
    b = s;
    split(s->l, x, a, b->l);
    b->pull();
  }
  return;
}
int query(node *s, ll x) {
  if (!s)
    return 0;
  int ans = 0;
  if (s->val >= x) {
    if (s->l)
      ans += query(s->l, x);
    ans++;
    if (s->r)
      ans += s->r->size;
  } else if (s->r)
    ans += query(s->r, x);
  return ans;
}
int main() {
  int n, m, q, a, b, c, ans[N];
  node *root = nullptr, *l, *r;
  priority_queue<pll, vector<pll>, greater<pll>> pq;
  pll temp, s[N];
  pair<pll, int> ask[N];
  bitset<N> went;
  vector<pi> graph[N];
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d%d", &a, &b, &c);
    graph[a].PB({b, c});
    graph[b].PB({a, c});
  }
  for (int i = 0; i < n; i++)
    s[i] = {INF, INF};
  s[0].F = 0;
  pq.push({0, 0});
  went.reset();
  while (!pq.empty()) {
    temp = pq.top();
    pq.pop();
    if (went[temp.S])
      continue;
    went[temp.S] = true;
    for (pi i : graph[temp.S])
      if (!went[i.F] && s[temp.S].F + i.S < s[i.F].F) {
        s[i.F].F = s[temp.S].F + i.S;
        pq.push({s[i.F].F, i.F});
      }
  }
  went.reset();
  pq.push({0, n - 1});
  s[n - 1].S = 0;
  while (!pq.empty()) {
    temp = pq.top();
    pq.pop();
    if (went[temp.S])
      continue;
    went[temp.S] = true;
    for (pi i : graph[temp.S])
      if (!went[i.F] && s[temp.S].S + i.S < s[i.F].S) {
        s[i.F].S = s[temp.S].S + i.S;
        pq.push({s[i.F].S, i.F});
      }
  }
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%lld%lld", &ask[i].F.F, &ask[i].F.S);
    ask[i].S = i;
  }
  sort(ask, ask + q);
  a = 0;
  for (int i = 0; i < q; i++) {
    while (a < n) {
      if (s[a].F <= ask[i].F.F) {
        split(root, s[a].S, l, r);
        root = merge(l, new node(s[a].S));
        root = merge(root, r);
        a++;
      } else
        break;
    }
    ans[ask[i].S] = query(root, ask[i].F.S);
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
const int N = 1e5 + 10;
const ll INF = 1LL << 60;
struct node {
  node *l;
  node *r;
  int size;
  ll val;
  node(ll n) {
    l = r = nullptr;
    size = 1;
    val = n;
  }
  void pull() {
    size = 1;
    if (l)
      size += l->size;
    if (r)
      size += r->size;
    return;
  }
};
node *merge(node *a, node *b) {
  if (!a)
    return b;
  if (!b)
    return a;
  if (a->size > b->size) {
    a->r = merge(a->r, b);
    a->pull();
    return a;
  } else {
    b->l = merge(a, b->l);
    b->pull();
    return b;
  }
}
void split(node *s, ll x, node *&a, node *&b) {
  if (!s) {
    a = b = nullptr;
    return;
  }
  if (x > s->val) {
    a = s;
    split(s->r, x, a->r, b);
    a->pull();
  } else {
    b = s;
    split(s->l, x, a, b->l);
    b->pull();
  }
  return;
}
int query(node *s, ll x) {
  if (!s)
    return 0;
  int ans = 0;
  if (s->val >= x) {
    if (s->l)
      ans += query(s->l, x);
    ans++;
    if (s->r)
      ans += s->r->size;
  } else if (s->r)
    ans += query(s->r, x);
  return ans;
}
int main() {
  int n, m, q, a, b, c, ans[N];
  node *root = nullptr, *l, *r;
  priority_queue<pll, vector<pll>, greater<pll>> pq;
  pll temp, s[N];
  pair<pll, int> ask[N];
  bitset<N> went;
  vector<pi> graph[N];
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d%d", &a, &b, &c);
    graph[a].PB({b, c});
    graph[b].PB({a, c});
  }
  for (int i = 0; i < n; i++)
    s[i] = {INF, INF};
  s[0].F = 0;
  pq.push({0, 0});
  went.reset();
  while (!pq.empty()) {
    temp = pq.top();
    pq.pop();
    if (went[temp.S])
      continue;
    went[temp.S] = true;
    for (pi i : graph[temp.S])
      if (!went[i.F] && s[temp.S].F + i.S < s[i.F].F) {
        s[i.F].F = s[temp.S].F + i.S;
        pq.push({s[i.F].F, i.F});
      }
  }
  went.reset();
  pq.push({0, n - 1});
  s[n - 1].S = 0;
  while (!pq.empty()) {
    temp = pq.top();
    pq.pop();
    if (went[temp.S])
      continue;
    went[temp.S] = true;
    for (pi i : graph[temp.S])
      if (!went[i.F] && s[temp.S].S + i.S < s[i.F].S) {
        s[i.F].S = s[temp.S].S + i.S;
        pq.push({s[i.F].S, i.F});
      }
  }
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%lld%lld", &ask[i].F.F, &ask[i].F.S);
    ask[i].S = i;
  }
  sort(ask, ask + q);
  a = 0;
  for (int i = 0; i < q; i++) {
    while (a < n) {
      if (s[a].F <= ask[i].F.F) {
        split(root, s[a].S, l, r);
        root = merge(l, new node(s[a].S));
        root = merge(root, r);
        a++;
      } else
        break;
    }
    ans[ask[i].S] = query(root, ask[i].F.S);
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}