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

#include <cstdio>
#include <functional>
#include <numeric>
#include <tuple>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)
using namespace std;
template <class T> inline void setmax(T &a, T const &b) { a = max(a, b); }
template <typename X, typename T> auto vectors(X x, T a) {
  return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
  auto cont = vectors(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    vector<int> p(n - 1);
    repeat(i, n - 1) {
      scanf("%d", &p[i]);
      --p[i];
    }
    vector<int> d(n - 1);
    repeat(i, n - 1) scanf("%d", &d[i]);
    vector<vector<pair<int, int>>> g(n);
    repeat(i, n - 1) {
      g[i + 1].emplace_back(p[i], d[i]);
      g[p[i]].emplace_back(i + 1, d[i]);
    }
    auto dist = vectors(n, n, -1);
    repeat(root, n) {
      function<void(int)> dfs = [&](int i) {
        for (auto e : g[i]) {
          int j, d;
          tie(j, d) = e;
          if (dist[root][j] == -1) {
            dist[root][j] = dist[root][i] + d;
            dfs(j);
          }
        }
      };
      dist[root][root] = 0;
      dfs(root);
    }
    int longest = 0;
    repeat(i, n) if (g[i].size() >= 2) {
      repeat(j, n) if (g[j].size() >= 2) { setmax(longest, dist[i][j]); }
    }
    int d_sum = whole(accumulate, d, 0);
    int d_leaf = 0;
    repeat(i, n) if (g[i].size() == 1) { d_leaf += g[i][0].second; }
    int result = 3 * d_sum - 2 * d_leaf - longest;
    printf("%d\\n", result);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < int(n); ++(i))
using namespace std;
template <class T> inline void setmax(T &a, T const &b) { a = max(a, b); }
template <class T> inline void setmin(T &a, T const &b) { a = min(a, b); }

int main() {
  while (true) {
    int r, n;
    scanf("%d%d", &r, &n);
    if (r == 0 and n == 0)
      break;
    vector<int> hl(2 * r);
    vector<int> hr(2 * r);
    repeat(i, n) {
      int xl, xr, hi;
      scanf("%d%d%d", &xl, &xr, &hi);
      repeat_from(x, max(-r, xl), min(+r, xr) - 1 + 1) setmax(hr[r + x], hi);
      repeat_from(x, max(-r, xl) + 1, min(+r, xr) + 1)
          setmax(hl[r - 1 + x], hi);
    }
    double t = INFINITY;
    repeat_from(x, -r, +r + 1) {
      double y = sqrt(r * r - x * x);
      if (x < +r)
        setmin(t, r - y + hr[r + x]);
      if (-r < x)
        setmin(t, r - y + hl[r - 1 + x]);
    }
    printf("%.8lf\\n", t);
  }
  return 0;
}