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

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

using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rep0(i, n) for (ll i = 0; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef _DEBUG
string _format(string args) {
  string r = "", t;
  istringstream s(args);
  while (getline(s, t, ','))
    r += t + ":%d ";
  return r + "\\n";
}
#define debug(...)                                                             \\
  do {                                                                         \\
    printf(_format(#__VA_ARGS__).c_str(), __VA_ARGS__);                        \\
  } while (0)
#define FAST_IOSTREAM()                                                        \\
  do {                                                                         \\
  } while (0);
#else
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (0)
#define FAST_IOSTREAM()                                                        \\
  do {                                                                         \\
    std::ios::sync_with_stdio(false);                                          \\
    std::cin.tie(nullptr);                                                     \\
  } while (0)
#endif

int main() {
  FAST_IOSTREAM();

  ll n;
  cin >> n;
  ll ans = 0;

  // rep1(l,n) {
  //     for(int r=l;r<=n;r++) {
  //         ans += (r-l)+1;
  //     }
  // }

  rep1(l, n) {
    ans += n - l + 1;
    ans -= (n - l + 1) * l;
    ans += (n * (n + 1) / 2) - (l * (l - 1) / 2);
  }

  rep0(i, n - 1) {
    int u, v;
    cin >> u >> v;

    // for (int l=1; l<=n; l++) {
    //     if (u >= l && v >= l) {
    //         for (int r=l; r<=n; r++) {
    //             if (u <= r && v <= r) ans--;
    //         }
    //     }
    // }
    ll mx = max(u, v);
    ll mn = min(u, v);
    ans -= mn;
    ans -= n * mn;
    ans += mx * mn;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rep0(i, n) for (int i = 0; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef _DEBUG
string _format(string args) {
  string r = "", t;
  istringstream s(args);
  while (getline(s, t, ','))
    r += t + ":%d ";
  return r + "\\n";
}
#define debug(...)                                                             \\
  do {                                                                         \\
    printf(_format(#__VA_ARGS__).c_str(), __VA_ARGS__);                        \\
  } while (0)
#define FAST_IOSTREAM()                                                        \\
  do {                                                                         \\
  } while (0);
#else
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (0)
#define FAST_IOSTREAM()                                                        \\
  do {                                                                         \\
    std::ios::sync_with_stdio(false);                                          \\
    std::cin.tie(nullptr);                                                     \\
  } while (0)
#endif

int main() {
  FAST_IOSTREAM();

  int n;
  cin >> n;
  ll ans = 0;

  rep1(l, n) {
    for (int r = l; r <= n; r++) {
      ans += (r - l) + 1;
    }
  }

  rep0(i, n - 1) {
    int u, v;
    cin >> u >> v;

    // for (int l=1; l<=n; l++) {
    //     if (u >= l && v >= l) {
    //         for (int r=l; r<=n; r++) {
    //             if (u <= r && v <= r) ans--;
    //         }
    //     }
    // }

    for (int l = 1; l <= min(u, v); l++) {
      ans -= n - max(max(u, v), l) + 1;
    }
  }

  cout << ans << endl;
}