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

#include <bits/stdc++.h>
#define sp ' '
#define nyan "(=^・ω・^=)"
#define mkp make_pair
#define intmax 2147483647
#define llmax 9223372036854775807
#define lP pair<ll, ll>
#define iP pair<int, int>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;

ll N, A[100000], B[100000], MIN[100000], aa[100000], bb[100000];
lP a[100000], b[100000];
priority_queue<lP, vector<lP>, greater<lP>> q;

int main() {
  cin >> N;
  for (int i = 0; i != N; ++i) {
    cin >> A[i];
    a[i] = mkp(A[i], i);
    MIN[i] = llmax;
  }
  for (int i = 0; i != N; ++i) {
    cin >> B[i];
    b[i] = mkp(B[i], i);
  }
  sort(a, a + N);
  sort(b, b + N);
  for (int i = 0; i != N; ++i) {
    aa[a[i].second] = i;
    bb[b[i].second] = i;
  }
  MIN[0] = 0;
  q.push(mkp(0, 0));
  while (!q.empty()) {
    int qf = q.top().first, qs = q.top().second;
    q.pop();
    if (qf == MIN[qs]) {
      for (int i = aa[qs] - 1; i != -1; --i) {
        if (qf + a[aa[qs]].first - a[i].first < MIN[a[i].second]) {
          MIN[a[i].second] = qf + a[aa[qs]].first - a[i].first;
          q.push(mkp(qf + a[aa[qs]].first - a[i].first, a[i].second));
        } else
          break;
      }
      for (int i = aa[qs] + 1; i != N; ++i) {
        if (qf + a[i].first - a[aa[qs]].first < MIN[a[i].second]) {
          MIN[a[i].second] = qf + a[i].first - a[aa[qs]].first;
          q.push(mkp(qf + a[i].first - a[aa[qs]].first, a[i].second));
        } else
          break;
      }
      for (int i = bb[qs] - 1; i != -1; --i) {
        if (qf + b[bb[qs]].first - b[i].first < MIN[b[i].second]) {
          MIN[b[i].second] = qf + b[bb[qs]].first - b[i].first;
          q.push(mkp(qf + b[bb[qs]].first - b[i].first, b[i].second));
        } else
          break;
      }
      for (int i = bb[qs] + 1; i != N; ++i) {
        if (qf + b[i].first - b[bb[qs]].first < MIN[b[i].second]) {
          MIN[b[i].second] = qf + b[i].first - b[bb[qs]].first;
          q.push(mkp(qf + b[i].first - b[bb[qs]].first, b[i].second));
        } else
          break;
      }
    }
  }
  for (int i = 0; i != N; ++i) {
    cout << MIN[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define sp ' '
#define nyan "(=^・ω・^=)"
#define mkp make_pair
#define intmax 2147483647
#define llmax 9223372036854775807
#define lP pair<ll, ll>
#define iP pair<int, int>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;

ll N, A[100000], B[100000], MIN[100000], aa[100000], bb[100000];
lP a[100000], b[100000];
priority_queue<lP, vector<lP>, greater<lP>> q;

int main() {
  cin >> N;
  for (int i = 0; i != N; ++i) {
    cin >> A[i];
    a[i] = mkp(A[i], i);
    MIN[i] = llmax;
  }
  for (int i = 0; i != N; ++i) {
    cin >> B[i];
    b[i] = mkp(B[i], i);
  }
  sort(a, a + N);
  sort(b, b + N);
  for (int i = 0; i != N; ++i) {
    aa[a[i].second] = i;
    bb[b[i].second] = i;
  }
  MIN[0] = 0;
  q.push(mkp(0, 0));
  while (!q.empty()) {
    int qf = q.top().first, qs = q.top().second;
    q.pop();
    if (qf == MIN[qs]) {
      for (int i = aa[qs] - 1; i != -1; --i) {
        if (qf + a[aa[qs]].first - a[i].first < MIN[a[i].second]) {
          MIN[a[i].second] = qf + a[aa[qs]].first - a[i].first;
          q.push(mkp(qf + a[aa[qs]].first - a[i].first, a[i].second));
        } else
          break;
      }
      for (int i = aa[qs] + 1; i != N; ++i) {
        if (qf + a[i].first - a[aa[qs]].first < MIN[a[i].second]) {
          MIN[a[i].second] = qf + a[i].first - a[aa[qs]].first;
          q.push(mkp(qf + a[i].first - a[aa[qs]].first, a[i].second));
        } else
          break;
      }
      for (int i = bb[qs] - 1; i != -1; --i) {
        if (qf + b[bb[qs]].first - b[i].first < MIN[b[i].second]) {
          MIN[b[i].second] = qf + b[bb[qs]].first - b[i].first;
          q.push(mkp(qf + b[bb[qs]].first - b[i].first, b[i].second));
        } else
          break;
      }
      for (int i = bb[qs] + 1; i != N; ++i) {
        if (qf + b[i].first - b[bb[qs]].first < MIN[b[i].second]) {
          MIN[b[i].second] = qf + b[i].first - b[bb[qs]].first;
          q.push(mkp(qf + b[i].first - b[bb[qs]].first, b[i].second));
        }
      }
    }
  }
  for (int i = 0; i != N; ++i) {
    cout << MIN[i] << endl;
  }
  return 0;
}