Problem p03941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
inline void read(int &x) {
  int s = 0, w = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    s = (s << 3) + (s << 1) + c - '0', c = getchar();
  }
  x = s * w;
}
const int N = 200010;
typedef long long ll;
#define rg register int
#define pb push_back
struct data {
  ll x, y;
  data(){};
  data(ll _x, ll _y) : x(_x), y(_y) {}
};
inline bool cmpx(const data &A, const data &B) { return A.x < B.x; }
inline bool cmpy(const data &A, const data &B) { return A.y < B.y; }
vector<data> f[N];
ll d[N], mn[N];
inline void solve(vector<data> a, vector<data> b, vector<data> &c, ll e,
                  ll mid) {
  if (!a.size() || !b.size())
    return;
  if (a.size() > b.size())
    swap(a, b);
  sort(a.begin(), a.end(), cmpy), sort(b.begin(), b.end(), cmpx);
  int sb = b.size();
  for (rg i = 0; i < sb; i++)
    if (!i)
      mn[i] = b[i].y;
    else
      mn[i] = min(mn[i - 1], b[i].y);
  for (rg i = 0, j = sb - 1; i < a.size(); i++) {
    while (j >= 0 && a[i].y + b[j].x - 2 * e > mid)
      j--;
    if (~j)
      c.pb(data(a[i].x, mn[j]));
  }
  sort(a.begin(), a.end(), cmpx), sort(b.begin(), b.end(), cmpy);
  for (rg i = 0; i < sb; i++)
    if (!i)
      mn[i] = b[i].x;
    else
      mn[i] = min(mn[i - 1], b[i].x);
  for (rg i = 0, j = sb - 1; i < a.size(); i++) {
    while (j >= 0 && a[i].x + b[j].y - 2 * e > mid)
      j--;
    if (~j)
      c.pb(data(mn[j], a[i].y));
  }
}
int ls[N], rs[N];
bool cao;
inline void dfs(int u, ll mid) {
  f[u].clear();
  if (!ls[u]) {
    f[u].pb(data(d[u], d[u]));
    return;
  }
  if (cao)
    return;
  dfs(ls[u], mid), dfs(rs[u], mid);
  if (cao)
    return;
  solve(f[ls[u]], f[rs[u]], f[u], d[u], mid);
  f[ls[u]].clear(), f[rs[u]].clear();
  if (!f[u].size())
    cao = 1;
}
int n;
ll L, R, ans;
int main() {
  freopen("cc.in", "r", stdin);
  freopen("cc.out", "w", stdout);
  read(n);
  for (rg i = 2, x, y; i <= n; i++) {
    read(x), read(y);
    d[i] = d[x] + y, R = max(R, d[i]);
    if (ls[x])
      rs[x] = i;
    else
      ls[x] = i;
  }
  R <<= 1;
  while (L <= R) {
    ll mid = (L + R) >> 1;
    cao = 0;
    dfs(1, mid);
    if (!cao)
      ans = mid, R = mid - 1;
    else
      L = mid + 1;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
inline void read(int &x) {
  int s = 0, w = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    s = (s << 3) + (s << 1) + c - '0', c = getchar();
  }
  x = s * w;
}
const int N = 200010;
typedef long long ll;
#define rg register int
#define pb push_back
struct data {
  ll x, y;
  data(){};
  data(ll _x, ll _y) : x(_x), y(_y) {}
};
inline bool cmpx(const data &A, const data &B) { return A.x < B.x; }
inline bool cmpy(const data &A, const data &B) { return A.y < B.y; }
vector<data> f[N];
ll d[N], mn[N];
inline void solve(vector<data> a, vector<data> b, vector<data> &c, ll e,
                  ll mid) {
  if (!a.size() || !b.size())
    return;
  if (a.size() > b.size())
    swap(a, b);
  sort(a.begin(), a.end(), cmpy), sort(b.begin(), b.end(), cmpx);
  int sb = b.size();
  for (rg i = 0; i < sb; i++)
    if (!i)
      mn[i] = b[i].y;
    else
      mn[i] = min(mn[i - 1], b[i].y);
  for (rg i = 0, j = sb - 1; i < a.size(); i++) {
    while (j >= 0 && a[i].y + b[j].x - 2 * e > mid)
      j--;
    if (~j)
      c.pb(data(a[i].x, mn[j]));
  }
  sort(a.begin(), a.end(), cmpx), sort(b.begin(), b.end(), cmpy);
  for (rg i = 0; i < sb; i++)
    if (!i)
      mn[i] = b[i].x;
    else
      mn[i] = min(mn[i - 1], b[i].x);
  for (rg i = 0, j = sb - 1; i < a.size(); i++) {
    while (j >= 0 && a[i].x + b[j].y - 2 * e > mid)
      j--;
    if (~j)
      c.pb(data(mn[j], a[i].y));
  }
}
int ls[N], rs[N];
bool cao;
inline void dfs(int u, ll mid) {
  f[u].clear();
  if (!ls[u]) {
    f[u].pb(data(d[u], d[u]));
    return;
  }
  if (cao)
    return;
  dfs(ls[u], mid), dfs(rs[u], mid);
  if (cao)
    return;
  solve(f[ls[u]], f[rs[u]], f[u], d[u], mid);
  f[ls[u]].clear(), f[rs[u]].clear();
  if (!f[u].size())
    cao = 1;
}
int n;
ll L, R, ans;
int main() {
  // freopen("cc.in","r",stdin);
  // freopen("cc.out","w",stdout);
  read(n);
  for (rg i = 2, x, y; i <= n; i++) {
    read(x), read(y);
    d[i] = d[x] + y, R = max(R, d[i]);
    if (ls[x])
      rs[x] = i;
    else
      ls[x] = i;
  }
  R <<= 1;
  while (L <= R) {
    ll mid = (L + R) >> 1;
    cao = 0;
    dfs(1, mid);
    if (!cao)
      ans = mid, R = mid - 1;
    else
      L = mid + 1;
  }
  cout << ans << endl;
}