Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
d = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if u > v:
        d[u].append(v)
    else:
        d[v].append(u)
ans = 0
for l in range(n):
    ans += (l + 1) * (l + 2) // 2
    for r in range(l, n):
        for t in d[r]:
            if l <= t:
                ans -= (n - r)
print(ans)


Fixed submission:
n = int(input())
d = [0] * n
present = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if u > v:
        u, v = v, u
    d[v] += 1
    present[u].append(v)
ans = 0
cur = 0
for v in range(n):
    cur += (n - v) * d[v]
for l in range(n):
    ans += (l + 1) * (l + 2) // 2
    ans -= cur
    for v in present[l]:
        cur -= (n - v)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
d = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    d[u].append(v)
    d[v].append(u)
ans = 0
for l in range(n):
    ans += (l + 1) * (l + 2) // 2
    for r in range(l, n):
        for t in d[r]:
            if l <= t < r:
                ans -= (n - r)
print(ans)


Fixed submission:
n = int(input())
d = [0] * n
present = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if u > v:
        u, v = v, u
    d[v] += 1
    present[u].append(v)
ans = 0
cur = 0
for v in range(n):
    cur += (n - v) * d[v]
for l in range(n):
    ans += (l + 1) * (l + 2) // 2
    ans -= cur
    for v in present[l]:
        cur -= (n - v)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
d = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    d[u].append(v)
    d[v].append(u)
ans = 0
for l in range(n):
    cur = 0
    ans += (l + 1) * (l + 2) // 2
    for r in range(l, n):
        for t in d[r]:
            if l <= t < r:
                cur -= 1
        ans += cur
print(ans)


Fixed submission:
n = int(input())
d = [0] * n
present = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if u > v:
        u, v = v, u
    d[v] += 1
    present[u].append(v)
ans = 0
cur = 0
for v in range(n):
    cur += (n - v) * d[v]
for l in range(n):
    ans += (l + 1) * (l + 2) // 2
    ans -= cur
    for v in present[l]:
        cur -= (n - v)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
d = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    d[u].append(v)
    d[v].append(u)
ans = 0
for l in range(n):
    cur = 0
    for r in range(l, n):
        cur += 1
        for t in d[r]:
            if l <= t < r:
                cur -= 1
        ans += cur
print(ans)

Fixed submission:
n = int(input())
d = [0] * n
present = [[] for i in range(n)]
for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if u > v:
        u, v = v, u
    d[v] += 1
    present[u].append(v)
ans = 0
cur = 0
for v in range(n):
    cur += (n - v) * d[v]
for l in range(n):
    ans += (l + 1) * (l + 2) // 2
    ans -= cur
    for v in present[l]:
        cur -= (n - v)
print(ans)