Problem p03941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long

void read(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -f;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}

void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (!x)
    return;
  print(x / 10), putchar(x % 10 + 48);
}
void write(int x) {
  if (!x)
    putchar('0');
  else
    print(x);
  putchar('\n');
}

#define lf double
#define ll long long

#define pii pair<int, int>
#define fr first
#define sc second
#define mp make_pair

#define vec vector<pii>
#define pb push_back
#define iter vector<pii>::iterator
#define for_vec(i, x) for (iter i = x.begin(); i != x.end(); i++)

#define _sort(x, y) sort(x.begin(), x.end(), y)
#define I (int)

const int maxn = 1e5 + 10;
const int inf = 1e9;
const lf eps = 1e-8;

int cmpx(pii x, pii y) { return x.fr < y.fr; }
int cmpy(pii x, pii y) { return x.sc < y.sc; }

int son[maxn][2], v[maxn][2], n, d[maxn], mid, mn[maxn];
vec f[maxn];

void solve(vec &ls, vec &rs, vec &s) {
  if (ls.size() > rs.size())
    swap(ls, rs);
  if (!s.empty())
    s.clear();
  _sort(ls, cmpy), _sort(rs, cmpx);
  mn[0] = rs[0].sc;
  for (int i = 1; i < I rs.size(); i++)
    mn[i] = min(mn[i - 1], rs[i].sc);
  int p1 = 0, p2 = rs.size() - 1;
  for (; p1 < I ls.size(); p1++) {
    while (p2 >= 0 && ls[p1].sc + rs[p2].fr > mid)
      p2--;
    if (~p2)
      s.pb(mp(ls[p1].fr, mn[p2]));
  }
  _sort(ls, cmpx), _sort(rs, cmpy);
  mn[0] = rs[0].fr;
  for (int i = 1; i < I rs.size(); i++)
    mn[i] = min(mn[i - 1], rs[i].fr);
  for (p1 = 0, p2 = rs.size() - 1; p1 < I ls.size(); p1++) {
    while (p2 >= 0 && ls[p1].fr + rs[p2].sc > mid)
      p2--;
    if (~p2)
      s.pb(mp(mn[p2], ls[p1].sc));
  }
}

void dfs(int x) {
  if (!son[x][0])
    return f[x].pb(mp(0, 0)), void();
  int l = son[x][0], r = son[x][1];
  dfs(l), dfs(r);
  vec &ls = f[l], &rs = f[r];
  for_vec(i, ls) i->fr += v[x][0], i->sc += v[x][0];
  for_vec(i, rs) i->fr += v[x][1], i->sc += v[x][1];
  solve(ls, rs, f[x]);
  ls.clear(), rs.clear();
}

signed main() {
  read(n);
  for (int i = 2, x, y; i <= n; i++)
    read(x), read(y), son[x][d[x]] = i, v[x][d[x]++] = y;
  int l = 0, r = 1e10, ans = 1e10;
  while (l <= r) {
    mid = (l + r) >> 1,
    dfs(1); // puts("fuckpps");printf("mid :: %lld %lld %lld\n",mid,l,r);
    // for_vec(i,f[1]) printf("%lld %lld\n",i->fr,i->sc);
    if (f[1].empty())
      l = mid + 1;
    else
      r = mid - 1, ans = mid;
    f[1].clear();
  }
  write(ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long

void read(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -f;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}

void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (!x)
    return;
  print(x / 10), putchar(x % 10 + 48);
}
void write(int x) {
  if (!x)
    putchar('0');
  else
    print(x);
  putchar('\n');
}

#define lf double
#define ll long long

#define pii pair<int, int>
#define fr first
#define sc second
#define mp make_pair

#define vec vector<pii>
#define pb push_back
#define iter vector<pii>::iterator
#define for_vec(i, x) for (iter i = x.begin(); i != x.end(); i++)

#define _sort(x, y) sort(x.begin(), x.end(), y)
#define I (int)

const int maxn = 3e5 + 10;
const int inf = 1e9;
const lf eps = 1e-8;

int cmpx(pii x, pii y) { return x.fr < y.fr; }
int cmpy(pii x, pii y) { return x.sc < y.sc; }

int son[maxn][2], v[maxn][2], n, d[maxn], mid, mn[maxn];
vec f[maxn];

void solve(vec &ls, vec &rs, vec &s) {
  if (ls.size() > rs.size())
    swap(ls, rs);
  if (!s.empty())
    s.clear();
  _sort(ls, cmpy), _sort(rs, cmpx);
  mn[0] = rs[0].sc;
  for (int i = 1; i < I rs.size(); i++)
    mn[i] = min(mn[i - 1], rs[i].sc);
  int p1 = 0, p2 = rs.size() - 1;
  for (; p1 < I ls.size(); p1++) {
    while (p2 >= 0 && ls[p1].sc + rs[p2].fr > mid)
      p2--;
    if (~p2)
      s.pb(mp(ls[p1].fr, mn[p2]));
  }
  _sort(ls, cmpx), _sort(rs, cmpy);
  mn[0] = rs[0].fr;
  for (int i = 1; i < I rs.size(); i++)
    mn[i] = min(mn[i - 1], rs[i].fr);
  for (p1 = 0, p2 = rs.size() - 1; p1 < I ls.size(); p1++) {
    while (p2 >= 0 && ls[p1].fr + rs[p2].sc > mid)
      p2--;
    if (~p2)
      s.pb(mp(mn[p2], ls[p1].sc));
  }
}

void dfs(int x) {
  if (!son[x][0])
    return f[x].pb(mp(0, 0)), void();
  int l = son[x][0], r = son[x][1];
  dfs(l), dfs(r);
  vec &ls = f[l], &rs = f[r];
  for_vec(i, ls) i->fr += v[x][0], i->sc += v[x][0];
  for_vec(i, rs) i->fr += v[x][1], i->sc += v[x][1];
  solve(ls, rs, f[x]);
  ls.clear(), rs.clear();
}

signed main() {
  read(n);
  for (int i = 2, x, y; i <= n; i++)
    read(x), read(y), son[x][d[x]] = i, v[x][d[x]++] = y;
  int l = 0, r = 1e10, ans = 1e10;
  while (l <= r) {
    mid = (l + r) >> 1,
    dfs(1); // puts("fuckpps");printf("mid :: %lld %lld %lld\n",mid,l,r);
    // for_vec(i,f[1]) printf("%lld %lld\n",i->fr,i->sc);
    if (f[1].empty())
      l = mid + 1;
    else
      r = mid - 1, ans = mid;
    f[1].clear();
  }
  write(ans);
  return 0;
}