Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds?

Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property.

The triangle is called non-degenerate if and only if its vertices are not collinear.


-----Input-----

The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers.


-----Output-----

Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds.


-----Examples-----
Input
1 2 3 4

Output
4

Input
1 2 2 5

Output
3

Input
500000 500000 500000 500000

Output
1



-----Note-----

In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$.

In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$.

In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.

Difficulty: MEDIUM_HARD
Tags: Mathematics

Sorting

Amortized analysis

Implementation
Skill types: Sorting

Amortized analysis

Starter code:

Solutions:
[a, b, c, d] = input().split(' ')
a = int(a)
b = int(b)
c = int(c)
d = int(d)

def f(x):
	if x < 1:
		return 0
	else:
		return x * (x + 1) * (x + 2) // 6
print((a + 2 * b - c) * (b + 1 - a) * (c + 1 - b) // 2 + f(c - a - b) - f(c - 2 * b - 1) - f(b + c - d - 1) + f(a + c - d - 2) + f(2 * b - d - 2) - f(a + b - d - 3))


(a, b, c, d) = map(int, input().split())
s = 0
for z in range(c, d + 1):
	if b + c <= z:
		break
	u = min(b + b, a + c)
	v = max(b + b, a + c)
	if z >= v:
		s += (b + c - z) * (b + c - z + 1) // 2
	elif u <= z:
		s += (b + c - v) * (b + c - v + 1) // 2 + (min(c - b, b - a) + 1) * (v - z)
	elif z >= a + b:
		s += (c - b + 1) * (b - a + 1) - (z - a - b + 2) * (z - b - a + 1) // 2
	else:
		s += (c - b + 1) * (b - a + 1)
print(s)


(A, B, C, D) = map(int, input().split())
ans = 0
howmany = [0 for i in range(10 ** 6 + 1)]
cnt = 0
for i in range(A + B + 1):
	if A <= i <= B and B <= A + B - i <= C:
		cnt += 1
howmany[A + B] = cnt
for z in range(A + B + 1, B + C + 1):
	if z <= min(A + C, 2 * B):
		cnt += 1
	elif min(A + C, 2 * B) < z <= max(A + C, 2 * B):
		pass
	elif max(A + C, 2 * B) < z:
		cnt -= 1
	howmany[z] = cnt
for i in range(1, 10 ** 6 + 1):
	howmany[i] += howmany[i - 1]
for i in range(C, D + 1):
	ans += howmany[10 ** 6] - howmany[i]
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
for x in range(a, b + 1):
	(min_, max_) = (max(b, c - x + 1), min(c, d - x + 1))
	(ta, tb) = (min_ + x - c, max_ + x - c)
	ans += (ta + tb) * max(tb - ta + 1, 0) // 2 + (d - c + 1) * min(c - max_, c - b + 1)
print(ans)


import sys

def read_input(input_path=None):
	if input_path is None:
		f = sys.stdin
	else:
		f = open(input_path, 'r')
	(a, b, c, d) = map(int, f.readline().split())
	return (a, b, c, d)

def f(x):
	if x < 1:
		return 0
	else:
		return x * (x + 1) * (x + 2) // 6

def sol(a, b, c, d):
	ans = (a + 2 * b - c) * (b + 1 - a) * (c + 1 - b) // 2 + f(c - a - b) - f(c - 2 * b - 1) - f(b + c - d - 1) + f(a + c - d - 2) + f(2 * b - d - 2) - f(a + b - d - 3)
	return [f'{ans}']

def solve(input_path=None):
	return sol(*read_input(input_path))

def main():
	for line in sol(*read_input()):
		print(f'{line}')
main()


(a, b, c, d) = map(int, input().split())
s = 0
for z in range(c, min(d + 1, b + c)):
	if z - a <= c:
		s += (b - a + 1) * (c - max(b - 1, z - a))
		if z - a >= b:
			x = min(z - a - b + 1, b - a)
			s += (b - a) * x - (x - 1) * x / 2
	elif z - a > c:
		x = min(b + c - z, c - b + 1)
		s += (b + c - z) * x - (x - 1) * x / 2
print(int(s))


(A, B, C, D) = map(int, input().split())
li = [0] * (B + C + 2)
for i in range(A, B + 1):
	twosidemin = i + B
	twosidemax = i + C
	li[twosidemin] += 1
	li[twosidemax + 1] -= 1
for i in range(1, len(li)):
	li[i] = li[i - 1] + li[i]
answer = 0
summ = li[0]
for i in range(1, len(li)):
	summ += li[i]
	li[i] = li[i - 1] + li[i]
for i in range(C, min(B + C + 1, D + 1)):
	answer += summ - li[i]
print(answer)


(A, B, C, D) = map(int, input().split())
Ans = 0
for i in range(C, D + 1):
	k = i + 1 - B
	j = B
	if k > B:
		j += k - B
	if B <= j <= C:
		k = max(i - j + 1, A)
		t = k - A + 1
		p = C - j + 1
		q = B - k
		if p >= t:
			Ans += t * (t + 1) // 2 + (p - t) * t + p * q
		else:
			Ans += p * (p + 1) // 2 + p * q
print(Ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(A, B, C, D) = list(map(int, input().split()))
ans = 0
for x in range(A, B + 1):
	smallest_z = C
	smallest_y = max(smallest_z - x + 1, B)
	biggest_y = C
	max_z = min(x + smallest_y - 1, D)
	min_z = C
	z0 = max(0, max_z - min_z + 1)
	last_y = max(min(C, D - x + 1), smallest_y)
	z1 = z0 + (last_y - smallest_y)
	ans += (z0 + z1) * (last_y - smallest_y + 1) // 2
	if C > last_y:
		ans += z1 * (biggest_y - (last_y + 1) + 1)
print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(a, b + 1):
	if i + b <= d:
		x = max(0, i + b - c)
		y = min(i, d - c)
		p = max(0, i + c - d) * (d - c + 1)
		ans += (x + y) * (y - x + 1) // 2 + p
	else:
		ans += (c - b + 1) * (d - c + 1)
print(ans)


def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(a, b, c, d) = map(int, input().split())
s = dict()
l = []
for i in range(c, d + 1):
	l.append(i)
for i in range(a + b, b + c + 1):
	if i - a > c:
		st = i - c
	else:
		st = a
	if i - b < b:
		end = i - b
	else:
		end = b
	s.update({i: end - st + 1})
ans = 0
for i in s:
	ans += (len(l) - countGreater(l, len(l), i)) * s[i]
print(ans)


(a, b, c, d) = [int(x) for x in input().split()]
if a == b == c == d:
	print('1')
else:
	p = b + c - (a + b)
	arr = [0 for x in range(0, p + 1)]
	for i in range(a, b + 1):
		arr[i + b - (a + b)] = arr[i + b - (a + b)] + 1
		if i + c + 1 - (a + b) < len(arr):
			arr[i + c + 1 - (a + b)] = arr[i + c + 1 - (a + b)] - 1
	for j in range(1, len(arr)):
		arr[j] = arr[j] + arr[j - 1]
	for k in range(len(arr) - 2, -1, -1):
		arr[k] = arr[k] + arr[k + 1]
	count = 0
	for m in range(c, d + 1):
		if m - (a + b) >= len(arr) - 1:
			count += 0
		elif m - (a + b) < 0:
			count += arr[0]
		else:
			count += arr[m + 1 - (a + b)]
	print(count)


(a, b, c, d) = map(int, input().split())
xy = dict()
for i in range(a + b, b + c + 1):
	f = min(i - a - b + 1, b + c - i + 1)
	f = min(f, b - a + 1, c - b + 1)
	xy[i] = f
ans = 0
for i in range(a + b, b + c + 1):
	if i > d:
		ans += xy[i] * (d - c + 1)
	elif i <= c:
		continue
	else:
		ans += xy[i] * (i - c)
print(ans)


def solve(a, b, c, d):
	ans = 0
	for x in range(a, b + 1):
		y_s = max(b, c - x + 1)
		y_e = max(min(d + 1 - x, c), y_s)
		(t_s, t_e) = (min(y_s + x - 1, d) - c + 1, min(y_e + x - 1, d) - c + 1)
		ans += (t_s + t_e) * (y_e - y_s + 1) // 2
		if y_e < c:
			ans += (c - y_e) * (d - c + 1)
	return ans
(a, b, c, d) = list(map(int, input().split()))
print(solve(a, b, c, d))


(a, b, c, d) = map(int, input().split())
ans = 0
for z in range(c, d + 1):
	if b + c > z:
		ans += (b - a + 1) * (c - b + 1)
		if z >= a + b:
			ans -= (z - a - b + 1) * (z - a - b + 2) // 2
			if a + c <= z:
				ans += (z - a - c) * (z - a - c + 1) // 2
			if b + b <= z:
				ans += (z - b - b) * (z - b - b + 1) // 2
print(ans)


import sys
sum = 0
(a, b, c, d) = map(int, input().split())
for i in range(max(a + b, c + 1), c + b + 1):
	sum += (min(d + 1, i) - c) * (min(i - b, b) - max(i - c, a) + 1)
print(sum)


(a, b, c, d) = map(int, input().split())
arr = [0] * max(b + c + 2, d + 1)
for i in range(a, b + 1):
	arr[i + b] += 1
	arr[i + c + 1] += -1
prefix = [0] * max(b + c + 2, d + 1)
curr = 0
for i in range(len(arr)):
	curr += arr[i]
	prefix[i] = curr
prefix2 = [0] * max(b + c + 2, d + 1)
curr = 0
for i in range(len(arr) - 1, -1, -1):
	curr += prefix[i]
	prefix2[i] = curr
print(sum(prefix2[c + 1:d + 2]))


(A, B, C, D) = [int(i) for i in input().split()]
count_ymz = [None for i in range(D - B + 1)]
for i in range(len(count_ymz)):
	count_ymz[i] = min(C, D - i) - max(C - i, B) + 1
pre_count_ymz = [None for i in range(len(count_ymz))]
pre_count_ymz[0] = count_ymz[0]
for i in range(1, len(count_ymz)):
	pre_count_ymz[i] = pre_count_ymz[i - 1] + count_ymz[i]
ans = 0
for x in range(A, B + 1):
	if x > D - B + 1:
		ans += pre_count_ymz[-1]
	else:
		ans += pre_count_ymz[x - 1]
print(ans)


(a, b, c, d) = [int(i) for i in input().split()]
result = 0
for x in range(a, b + 1):
	k = max(c + 1 - x, b)
	p = min(d - c + 1, x)
	q = min(d - c + 1, x + k - c)
	result += p * (p + 1) // 2 - q * (q - 1) // 2 + (c - k - p + q) * p
print(result)


x = input().split(' ')
A = int(x[0])
B = int(x[1])
C = int(x[2])
D = int(x[3])

def gauss_count(n):
	if n <= 0:
		return 0
	return n * (n + 1) // 2
count = 0
for z in range(C, D + 1):
	if A + B > z:
		count += (B - A + 1) * (C - B + 1)
		continue
	x0 = z - C + 1
	y0 = z - B + 1
	incl1 = gauss_count(B - x0 + 1)
	excl1 = gauss_count(A - x0)
	excl2 = gauss_count(B - y0)
	count += max(0, incl1 - excl1 - excl2)
print(count)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())
(a, b, c, d) = rint()
ans = 0
for z in range(c, d + 1):
	if a + b > z:
		ans += (b - a + 1) * (c - b + 1)
	else:
		xmin = z - c + 1
		if xmin > b:
			continue
		bx = b - xmin + 1
		cb = c - b + 1
		if bx <= cb:
			n = bx
			ans += n * (n + 1) // 2
		else:
			n = cb
			ans += n * (n + 1) // 2
			ans += (bx - cb) * cb
		if xmin < a:
			n = a - xmin
			if bx <= cb:
				ans -= n * (n + 1) // 2
			elif cb >= n:
				ans -= n * (n + 1) // 2
			else:
				ans -= cb * (cb + 1) // 2
				ans -= (n - cb) * cb
print(ans)


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
(a, b, c, d) = MAP()
MAX = 10 ** 6 + 7
imos = [0] * MAX
for ai in range(a, b + 1):
	imos[ai + b] += 1
	imos[ai + c + 1] -= 1
imos = list(accumulate(imos))
acc = list(accumulate(imos[::-1]))[::-1]
ans = 0
for z in range(c, d + 1):
	ans += acc[z + 1]
print(ans)


(A, B, C, D) = map(int, input().split())
ans = 0
for z in range(C, D + 1):
	y_STEPS = {0: B, 1: max(B, z - B), 2: max(B, min(C, z - A + 1)), 3: C}
	y_MIN = min(max(0, 2 * B - z), B - A + 1)
	y_MAX = max(0, min(B - A + 1, C + B - z))
	ans += (y_STEPS[3] - y_STEPS[2]) * y_MAX
	ans += (y_MIN + y_MAX) * (y_STEPS[2] - y_STEPS[1] + 1) // 2
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(a, b + 1):
	if i + b <= d:
		x = max(0, i + b - c)
		y = min(i, d - c)
		p = max(0, i + c - d) * (d - c + 1)
		ans += (x + y) * (y - x + 1) // 2 + p
	else:
		ans += (c - b + 1) * (d - c + 1)
print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
(a, b, c, d) = sp()
answer = 0
for i in range(max(a + b, c), c + d + 1):
	answer += min(i - c, d - c + 1) * (i - a - b + 1 - max(0, i - b - b) - max(0, i - a - c) + max(0, i - b - c - 1))
out(answer)


(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(max(a + b, c), b + c + 1):
	if i == a + b or i == b + c:
		ans += min(i - c, d - c + 1)
	elif i - b < b:
		ans += min(max(0, i - b - a + 1), c - b + 1) * min(i - c, d - c + 1)
	elif i - b > b:
		ans += min(max(0, c + 1 - i + b), b + 1 - a) * min(i - c, d - c + 1)
	else:
		ans += min(c + 1 - b, b + 1 - a) * min(i - c, d - c + 1)
print(ans)


(a, b, c, d) = [int(x) for x in input().split()]
ans = 0
for y in range(b, c + 1):
	u = min(d - y, b - 1)
	l = c - y
	if l <= u:
		left = (u - l + 1) * (b + 1)
		L = c - y
		U = min(d - y, b - 1, a - 1)
		s = 0
		if L <= U:
			p1 = (U - L + 1) * a
			s += p1
		L = max(a, c - y)
		U = min(d - y, b - 1)
		if L <= U:
			p2 = (U + 1 - (L + 1) + 1) * (U + 1 + L + 1) // 2
			s += p2
		ans += left - s
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
n = d + c + 3
ar = [0] * n
for x in range(a, b + 1):
	ar[x + b] += 1
	ar[x + c + 1] -= 1
for i in range(n - 1):
	ar[i + 1] += ar[i]
for i in range(n - 1):
	ar[i + 1] += ar[i]
for i in range(c, d + 1):
	ans += ar[-1] - ar[i]
print(ans)


(a, b, c, d) = map(int, input().split())
prefix = [0] * max(b + c + 1, d + 2)
for i in range(a, b + 1):
	mins = i + b
	maxs = i + c
	prefix[maxs] += 1
	prefix[mins - 1] -= 1
for i in range(len(prefix) - 2, -1, -1):
	prefix[i] += prefix[i + 1]
ans = 0
for i in range(len(prefix) - 2, -1, -1):
	prefix[i] += prefix[i + 1]
for i in range(c, d + 1):
	ans += prefix[i + 1]
print(ans)


import sys
input = sys.stdin.buffer.readline

def solution():
	(a, b, c, d) = map(int, input().split())
	ans = [0] * 1000002
	for i in range(a, b + 1):
		ans[i + b] += 1
		ans[i + c + 1] -= 1
	for i in range(1, 1000002):
		ans[i] += ans[i - 1]
	for i in range(1000000, -1, -1):
		ans[i] += ans[i + 1]
	sol = 0
	for i in range(c, d + 1):
		sol += ans[i + 1]
	print(sol)
solution()


from itertools import accumulate
(a, b, c, d) = map(int, input().split())
ans = 0
cnt = 0
ls = [0] * (b + c + 1)
for i in range(a + b, b + c + 1):
	if not (i - a <= c and i - b <= b):
		ls[i] = min(c - b + 1, b - a + 1)
	elif i - a <= c and i - b > b:
		ls[i] = min(i - a - b + 1, b - a + 1)
	elif i - b <= b and i - a > c:
		ls[i] = min(c - b + 1, i - b - a + 1)
	else:
		ls[i] = min(i - a - b + 1, i - b - a + 1)
	if ls[a + 2 * b + c - i]:
		ls[i] = ls[a + 2 * b + c - i]
acc = list(accumulate(ls))
for z in range(c, d + 1):
	if z >= b + c:
		continue
	ans += acc[-1] - acc[z]
print(ans)


st = input()
getting = ''
num = []
for i in st:
	if i == ' ':
		num.append(int(getting))
		getting = ''
	else:
		getting += i
num.append(int(getting))
A = num[0]
B = num[1]
C = num[2]
D = num[3]
ans = 0
if C - B > B - A:
	min = B - A
else:
	min = C - B

def large(add):
	if add <= C:
		return 0
	elif add > C and add <= D:
		return add - C
	else:
		return D - C + 1

def combin(sum):
	if sum < min + 1:
		return sum + 1
	elif sum > C - A - min - 1:
		return C - A - sum + 1
	else:
		return min + 1
for i in range(C - A + 1):
	ans = ans + large(i + A + B) * combin(i)
print(f'{ans}')


(a, b, c, d) = list(map(int, input().split()))
y = c
otvet = 0
for x in range(a, b + 1):
	z = x + y - 1
	tr_xy = min(z, d) - c + 1
	y_kolvo = min(z - c, c - b)
	y_v_z = max(0, min(z - d, y_kolvo))
	an = tr_xy - y_kolvo + y_v_z
	otvet += y_v_z * tr_xy + (tr_xy * tr_xy + tr_xy - an * an + an) // 2
print(otvet)


(A, B, C, D) = map(int, input().split())
out = 0
for s in range(A + B, B + C + 1):
	xSmall = max(A, s - C)
	xBig = min(B, s - B)
	count = xBig - xSmall + 1
	if count < 0:
		continue
	zCount = max(0, min(s - 1, D) - C + 1)
	out += count * zCount
print(out)


import math
arr = [0] * 1000001
t = 1
for _ in range(t):
	x = input().split()
	list1 = [int(i) for i in x]
	a = list1[0]
	b = list1[1]
	c = list1[2]
	d = list1[3]
	j = 1
	for i in range(c + 1, b + c + 1):
		if j < d - c + 1:
			arr[i] = j
			j += 1
		else:
			arr[i] = d - c + 1
	ans = 0
	j = 1
	for i in range(a + b, min(2 * b, a + c)):
		ans += arr[i] * j
		j += 1
	for i in range(min(2 * b, a + c), max(2 * b, a + c) + 1):
		ans += arr[i] * min(b - a + 1, c - b + 1)
	j = 1
	for i in range(b + c, max(2 * b, a + c), -1):
		ans += arr[i] * j
		j += 1
	print(ans)


import sys

def read_integer():
	return int(sys.stdin.readline())

def read_integers():
	return [int(x) for x in sys.stdin.readline().split(' ')]

def print_integers(nums):
	print(' '.join([str(x) for x in nums]))

def f1(i, a, b, c, d):
	ret = 0
	for j in range(b, c + 1):
		ret += max(0, min(d, i + j - 1) - max(c, j - i + 1) + 1)
	return ret

def f2(i, a, b, c, d):
	if c + i - 1 <= d:
		top = c + i - 1 - c + 1
		bottom = max(1, b + i - 1 - c + 1)
		num = top - bottom + 1
		return (top + bottom) * num // 2
	else:
		ret = (c + i - 1 - d) * (d - c + 1)
		if b + i - 2 > d:
			ret -= (b + i - 2 - d) * (d - c + 1)
		else:
			ret += (d - c + 1 + 1) * (d - c + 1) // 2
			if b + i - 1 > c:
				ret -= (b + i - 2 - c + 1 + 1) * (b + i - 2 - c + 1) // 2
		return ret

def func(a, b, c, d):
	ans = 0
	for i in range(a, b + 1):
		r = f2(i, a, b, c, d)
		ans += r
	print(ans)
func(*read_integers())


[a, b, c, d] = input().split(' ')
a = int(a)
b = int(b)
c = int(c)
d = int(d)
ans = (a + 2 * b - c) * (b + 1 - a) * (c + 1 - b) // 2
if a + b - c < 0:
	ans += (c - a - b) * (c - a - b + 1) * (c - a - b + 2) // 6
if 2 * b - c < 0:
	ans -= (c - 2 * b - 1) * (c - 2 * b) * (c - 2 * b + 1) // 6
if b > d + 1 - c:
	ans -= (b + c - d - 1) * (b + c - d) * (b + c - d + 1) // 6
if a > d + 1 - c:
	ans += (a + c - d) * (a + c - d - 1) * (a + c - d - 2) // 6
if 2 * b - c > d + 1 - c:
	ans += (2 * b - d) * (2 * b - d - 1) * (2 * b - d - 2) // 6
if a + b - c > d + 1 - c:
	ans = (d + 1 - c) * (b + 1 - a) * (c + 1 - b)
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(a + b, b + c + 1):
	counter = min(min(i - a, c) - max(i - b, b) + 1, b - a + 1)
	ans += counter * max(min(i, d + 1) - c, 0)
print(ans)


import io
import os
from collections import Counter
DEBUG = False

def sumUpTo(n):
	return n * (n + 1) // 2

def sumRange(i, j):
	return sumUpTo(j) - sumUpTo(i - 1)

def solveFormula(A, B, C, D):
	total = 0
	for z in range(C, D + 1):
		if DEBUG:
			brute = 0
			for x in range(A, B + 1):
				for y in range(B, C + 1):
					assert x + z > y
					assert y + z > x
					if x + y > z:
						brute += 1

		def getNumY(x):
			yMin = max(B, z - x + 1)
			yMax = C
			if yMin <= yMax:
				return yMax - yMin + 1
			else:
				return 0
		if DEBUG:
			brute2 = 0
			for x in range(A, B + 1):
				numY = getNumY(x)
				brute2 += numY
			assert brute == brute2
			brute3 = 0
			xMin = max(A, z - C + 1)
			xMax = B
			for x in range(xMin, xMax + 1):
				numY = getNumY(x)
				assert 0 < numY
				brute3 += numY
			assert brute == brute3
			brute4 = 0
			xMin = max(A, z - C + 1)
			xMax = B
			xMid = min(max(xMin, z - B + 1), xMax + 1)
			for x in range(xMin, xMid):
				numY = getNumY(x)
				assert 0 < numY < C - B + 1
				brute4 += numY
			for x in range(xMid, xMax + 1):
				numY = getNumY(x)
				assert numY == C - B + 1
				brute4 += numY
			assert brute == brute4
		brute5 = 0
		xMin = max(A, z - C + 1)
		xMax = B
		if xMin <= xMax:
			xMid = min(max(xMin, z - B + 1), xMax + 1)
			if xMin < xMid:
				numYStart = getNumY(xMin)
				numYEnd = getNumY(xMid - 1)
				assert 0 < numYStart <= numYEnd < C - B + 1
				brute5 += sumRange(numYStart, numYEnd)
			brute5 += (xMax - xMid + 1) * (C - B + 1)
		if DEBUG:
			assert brute == brute5
		total += brute5
	return total

def solve(A, B, C, D):
	M = max(B + C + 1, D + 1) + 1
	intervalDelta = [0 for i in range(M)]
	for x in range(A, B + 1):
		minY = B
		maxY = C
		intervalDelta[x + minY] += 1
		intervalDelta[x + maxY + 1] += -1
	countXPlusY = [0] * M
	countXPlusY[0] = intervalDelta[0]
	for i in range(1, M):
		countXPlusY[i] = countXPlusY[i - 1] + intervalDelta[i]
	if DEBUG:
		counts = Counter()
		for x in range(A, B + 1):
			for y in range(B, C + 1):
				counts[x + y] += 1
		for (k, v) in counts.items():
			assert countXPlusY[k] == v
		assert sum(countXPlusY) == sum(counts.values())
	countXPlusYGreaterThanEqualTo = [0] * M
	countXPlusYGreaterThanEqualTo[M - 1] = countXPlusY[M - 1]
	for i in range(M - 2, -1, -1):
		countXPlusYGreaterThanEqualTo[i] = countXPlusYGreaterThanEqualTo[i + 1] + countXPlusY[i]
	ans = 0
	for z in range(C, D + 1):
		ans += countXPlusYGreaterThanEqualTo[z + 1]
	assert ans == solveFormula(A, B, C, D)
	return ans
if DEBUG:
	M = 20
	for A in range(1, M + 1):
		for B in range(A, M + 1):
			for C in range(B, M + 1):
				for D in range(C, M + 1):
					ans1 = solve(A, B, C, D)
					ans2 = solveFormula(A, B, C, D)
					assert ans1 == ans2
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(A, B, C, D) = [int(x) for x in input().split()]
ans = solve(A, B, C, D)
print(ans)


import sys
input = sys.stdin.readline

def sum1(x):
	k = min(d, c + x - 1, b + x - 1)
	if k < c:
		return 0
	else:
		return (c - b + 1) * (k - c + 1)

def sum2(x):
	k = min(d, c + x - 1)
	l = max(b + x, c)
	if k < l:
		return 0
	else:
		return (c + x) * (k - l + 1) - (k + l) * (k - l + 1) // 2
(a, b, c, d) = map(int, input().split())
res = 0
for x in range(a, b + 1):
	res += sum1(x) + sum2(x)
print(res)


import sys
import heapq

def main():
	(a, b, c, d) = map(int, input().split())
	dpList = [min(c - b + 1, b - a + 1)] * (c + b + 1)
	for i in range(min(c - b + 1, b - a + 1)):
		dpList[a + b + i] = i + 1
		dpList[-i - 1] = i + 1
	for i in range(a + b):
		dpList[i] = 0
	sumList = [0] * len(dpList)
	sumList[-1] = dpList[-1]
	for i in reversed(range(len(dpList) - 1)):
		sumList[i] = sumList[i + 1] + dpList[i]
	ans = 0
	for i in range(c, d + 1):
		if i > len(sumList) - 2:
			break
		ans += sumList[i + 1]
	print(ans)
	return
main()


def prog():
	(a, b, c, d) = map(int, input().split())
	psum = [0 for i in range(d + 2)]
	left = [False for i in range(d + 2)]
	right = [False for i in range(d + 2)]
	for i in range(a + b, min(2 * b + 1, d + 2)):
		left[i] = True
	for i in range(a + c, min(b + c + 1, d + 2)):
		right[i] = True
	maximum = (b - a + 1) * (c - b + 1)
	for i in range(min(a + b + 1, d + 2)):
		psum[i] = maximum
	bars = 0
	for i in range(a + b + 1, d + 2):
		if left[i - 1] == True:
			bars += 1
		if right[i - 2] == True:
			bars -= 1
		psum[i] = psum[i - 1] - bars
	total = 0
	for i in range(c + 1, d + 2):
		total += psum[i]
	print(total)
prog()


(a, b, c, d) = map(int, input().split())
num = [0] * (b + c + 1)
for i in range(a, b + 1):
	num[i + b - 1] += 1
	num[i + c] -= 1
from itertools import accumulate
num = list(accumulate(num))
ans = 0
for i in range(len(num)):
	ans += num[i] * min(max(i + 1 - c, 0), d - c + 1)
print(ans)


import sys
ints = (int(x) for x in sys.stdin.read().split())
sys.setrecursionlimit(3000)

def main():
	(a, b, c, d) = (next(ints) for i in range(4))
	ans = 0
	A = (b - a + 1) * (c - b + 1)
	h = min(c - b, b - a)
	(lo, hi) = (min(a + c, b + b), max(a + c, b + b))
	for z in range(min(a + b, c), d + 1):
		if z < a + b:
			pass
		elif z < lo:
			A -= 1 + z - (a + b)
		elif z < hi:
			A -= 1 + h
		elif z <= b + c:
			A -= 1 + (b + c) - z
		else:
			assert A == 0
		if c <= z <= d:
			ans += A
	print(ans)
	return
main()


import io
import os
DEBUG = False

def sumUpToN(n):
	return n * (n + 1) // 2

def solve(A, B, C, D):
	total = 0
	for z in range(C, D + 1):
		if DEBUG:
			brute = 0
			for x in range(A, B + 1):
				for y in range(B, C + 1):
					assert x + z > y
					assert y + z > x
					if x + y > z:
						brute += 1
			brute2 = 0
			for x in range(A, B + 1):
				yMin = max(B, z - x + 1)
				yMax = C
				if yMin <= yMax:
					brute2 += yMax - yMin + 1
			assert brute == brute2
			brute3 = 0
			for x in range(max(A, z - C + 1), B + 1):
				yMin = max(B, z - x + 1)
				yMax = C
				assert yMin <= yMax
				numY = yMax - yMin + 1
				brute3 += numY
			assert brute == brute3
		brute4 = 0
		xMin = max(A, z - C + 1)
		xMax = B
		if xMin <= xMax:
			yMin = max(B, z - xMin + 1)
			yMax = C
			assert yMin <= yMax
			numYStart = yMax - yMin + 1
			yMin = max(B, z - xMax + 1)
			yMax = C
			assert yMin <= yMax
			numYEnd = yMax - yMin + 1
			if numYEnd == yMax - B + 1:
				numYEnd -= 1
			if numYStart <= numYEnd:
				brute4 += sumUpToN(numYEnd) - sumUpToN(numYStart - 1)
			xMid = max(xMin, z - B + 1)
			if xMin <= xMid <= xMax:
				brute4 += (xMax - xMid + 1) * (C - B + 1)
		if DEBUG:
			assert brute == brute4
		total += brute4
	return total
if DEBUG:
	solve(5, 10, 15, 20)
	M = 10
	for A in range(1, M + 1):
		for B in range(A, M + 1):
			for C in range(B, M + 1):
				for D in range(C, M + 1):
					solve(A, B, C, D)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(A, B, C, D) = [int(x) for x in input().split()]
ans = solve(A, B, C, D)
print(ans)


(a, b, c, d) = map(int, input().split())
ans = int(0)
for i in range(c, d + 1):
	bi = b
	delta1 = i - b + 1
	if delta1 <= a:
		ans += (c - b + 1) * (b - a + 1)
		continue
	if delta1 > b:
		bi = bi + delta1 - b
		delta1 = b
	if bi > c:
		continue
	ost = c - bi + 1
	if delta1 - ost + 1 < a:
		ans += (b + 1 - a + (b + 1 - delta1)) * (b + 1 - a - (b + 1 - delta1) + 1) // 2
		ans += (a - (delta1 - ost + 1)) * (b - a + 1)
	else:
		ans += (b + 1 - (delta1 - ost + 1) + (b + 1 - delta1)) * (b + 1 - (delta1 - ost + 1) - (b + 1 - delta1) + 1) // 2
print(ans)


(a, b, c, d) = list(map(int, input().split()))
(rows, columns, maxEntry) = (c - b + 1, b - a + 1, d - c + 1)
(count, prev) = (0, 0)
for n in range(1, a):
	prev += rows - sorted([n - maxEntry, rows, 0])[1] - max(0, rows - n)
for n in range(a, b + 1):
	prev += rows - sorted([n - maxEntry, rows, 0])[1] - max(0, rows - n)
	count += prev
print(count)


def search(low, high, check):
	mid = (high + low) // 2
	while high >= low:
		if check(mid):
			low = mid + 1
		else:
			high = mid - 1
		mid = (high + low) // 2
	return mid
(a, b, c, d) = map(int, input().split())
num = min(b - a, c - b)
num2 = max(b - a, c - b)
num3 = a + b
ans = 0
i = num3 - 1

def check(num):
	global i
	return num < i
for i in range(num3, num3 + num2 + num + 1):
	high = search(c, d, check)
	num4 = min(min(i - num3 + 1, num + 1), num3 + num2 + num + 1 - i)
	ans += (high - c + 1) * num4
print(ans)


import sys
input = sys.stdin.readline

def main():
	(A, B, C, D) = map(int, input().split())
	mx = A
	my = C
	mz = C
	iv = 0
	val = 0
	for i in range(1, B + 1):
		val = val + (C - my + 1) * (B - max(i, A) + 1)
		if mz + 1 > D:
			C = C - 1
		my = max(my - 1, B)
		mz = mz + 1
		if C < B:
			break
	print(val)
main()


def sumn(n1, n2):
	return (n2 * (n2 + 1) - (n1 - 1) * n1) // 2
(a, b, c, d) = map(int, input().split())
cnt = 0
for u in range(a, b + 1):
	for i in range(max(b, c - u), c + 1):
		if u + i > d:
			cnt += (d - c + 1) * (c - i + 1)
			break
		elif c < u + i <= d:
			if u + c >= d:
				t = d - u
				cnt += (u - c) * (t - i + 1) + sumn(i, t)
				i = t + 1
				cnt += (d - c + 1) * (c - i + 1)
				break
			else:
				cnt += (u - c) * (c - i + 1) + sumn(i, c)
				break
print(cnt)


(a, b, c, d) = map(int, input().split())
ans = 0
for t in range(a + b, b + c + 1):
	M = min(b, t - b)
	m = max(a, t - c)
	dx = M - m + 1
	if c <= t - 1 <= d:
		dx *= t - 1 - c + 1
		ans += dx
	elif t - 1 > d:
		dx *= d - c + 1
		ans += dx
print(ans)


from collections import defaultdict
from math import log2, floor, sqrt
from pprint import pprint
from bisect import bisect_left, bisect_right
mod = pow(10, 9) + 7
dict = defaultdict(int)

def fun(a, b, c, d):
	z = list(range(c, d + 1))
	lis = list(range(a + b, b + c + 1))
	length = len(lis)
	numbers = [1] * length
	mina = min(b - a + 1, c - b + 1)
	for i in range(1, length):
		if i < length / 2 and i < mina:
			numbers[i] = numbers[i - 1] + 1
		elif i > length - mina:
			numbers[i] = numbers[i - 1] - 1
		else:
			numbers[i] = numbers[i - 1]
	for i in reversed(range(length - 1)):
		numbers[i] += numbers[i + 1]
	numbers.append(0)
	suma = 0
	for i in z:
		theta = bisect_right(lis, i)
		suma += numbers[theta]
	print(suma)
(a, b, c, d) = map(int, input().split())
fun(a, b, c, d)


(a, b, c, d) = map(int, input().split())
xplusy = [0] * (10 ** 6 + 10)
for x in range(a, b + 1):
	xplusy[x + b] += 1
	xplusy[x + c + 1] -= 1
for i in range(1, 10 ** 6 + 10):
	xplusy[i] += xplusy[i - 1]
for i in range(1, 10 ** 6 + 10):
	xplusy[i] += xplusy[i - 1]
ans = 0
allxy = (b - a + 1) * (c - b + 1)
for z in range(c, d + 1):
	ans += allxy - xplusy[z]
print(ans)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(str(i)[2:-1]) for i in input().rstrip()]
a = 0
(a, b, c, d) = li()
arr = [0] * (10 ** 6 + 5)
for i in range(a, b + 1):
	left = i + b
	right = i + c + 1
	arr[left] += 1
	arr[right] -= 1
for i in range(len(arr) - 1):
	arr[i + 1] += arr[i]
for i in range(len(arr) - 1):
	arr[i + 1] += arr[i]
ans = 0
for i in range(c, d + 1, 1):
	ans += arr[-1] - arr[i]
print(ans)


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
	f = iter(open('C.txt').readlines())

	def input():
		return next(f)
else:
	input = lambda : sys.stdin.readline().strip()
fprint = lambda *args: print(*args, flush=True)
(A, B, C, D) = map(int, input().split())
s = 0

def f(K):
	if K <= 0:
		return 0
	return K * (K + 1) // 2
for x in range(A, B + 1):
	T = x
	L = min(C - B, T)
	R = min(D - C, T)
	s += f(T)
	s -= f(T - L - 1)
	s -= f(T - R - 1)
	s += f(T - R - L - 2)
print(s)


import os
import sys
from io import BytesIO, IOBase
import threading
from math import floor
from bisect import bisect_right
from collections import deque
from math import gcd
mod = 998244353

def main():
	(a, b, c, d) = map(int, input().split())
	prefix = [0] * (3 * d)
	for i in range(a, b + 1):
		prefix[b + i] += 1
		prefix[c + i + 1] -= 1
	for i in range(1, 3 * d):
		prefix[i] += prefix[i - 1]
	for i in range(3 * d - 2, 0, -1):
		prefix[i] += prefix[i + 1]
	ans = 0
	for i in range(c, d + 1):
		ans += prefix[i + 1]
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def main():
	(a, b, c, d) = map(int, input().split())
	q = c - b + 1
	su = 0
	d = min(d, b + c - 1)
	for z in range(c, d + 1):
		ma = q - max(b, z - b + 1) + b
		minx = max(a, z - c + 1)
		l = q - max(b, z - minx + 1) + b
		if l == ma:
			su += ma * (b - minx + 1)
		elif b - minx + 1 >= ma - l + 1:
			su += (b - minx + 1 - ma + l - 1) * ma
			su += sun(ma, l)
		else:
			su += sun(b - minx + 1, l)
	print(su)

def sun(a, b):
	return (a + b) * (a - b + 1) // 2
main()


import math

def getVal(n, k):
	num = min(n, k)
	num = num * (num - 1) // 2 + max(n - k, 0) * k
	return num
ans = 0
(a, b, c, d) = [int(i) for i in input().strip().split(' ')]
for x0 in range(a, b + 1):
	low = max(x0 + b - c, 0)
	high = max(x0 + c - c + 1, 0)
	low = getVal(low, d - c + 1)
	high = getVal(high, d - c + 1)
	ans = ans + high - low
print(ans)


l = list(map(int, input().split(' ')))
ans = 0

def ssum(n):
	return n * (n + 1) // 2
a = l[0]
b = l[1]
c = l[2]
d = l[3]
for x in range(a, b + 1):
	yy = c - x + 1
	yyy = d - x + 1
	if yy < b:
		yy = b
	if yyy >= c:
		yyy = c
		ans += x * (yyy - yy + 1) - c * (yyy - yy + 1) + (ssum(yyy) - ssum(yy - 1))
	if yyy < c:
		if yyy >= yy:
			ans += x * (yyy - yy + 1) - c * (yyy - yy + 1) + (ssum(yyy) - ssum(yy - 1))
			ans += (d - c + 1) * (c - yyy)
		if yyy < yy:
			ans += (d - c + 1) * (c - yy + 1)
print(ans)


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	(a, b, c, d) = sep()
	arr = [0] * (b + c + 2)
	for x in range(a, b + 1):
		arr[x + b] += 1
		arr[x + c + 1] -= 1
	for i in range(1, b + c + 2):
		arr[i] += arr[i - 1]
	ans = 0
	for i in range(1, b + c + 1):
		if i > c:
			if i <= d:
				ans += arr[i] * (i - c)
			else:
				ans += arr[i] * (d - c + 1)
	print(ans)
testcase(1)


from bisect import bisect_left, bisect_right
(a, b, c, d) = map(int, input().split())
length = c - b + 1
r = [c, d]
ans = 0
for i in range(a, b + 1):
	first = i
	s = [b - i + 1, i + b - 1]
	e = [c - i + 1, i + c - 1]
	start1 = max(c, s[0])
	end1 = min(d, s[1])
	start2 = max(c, e[0])
	end2 = min(d, e[1])
	s1 = max(end1 - c + 1, 1)
	s2 = min(s[1] - s[0] + 1, min(end2, d) - c + 1)
	s3 = max(min(d, end2) - start2 + 1, 1)
	if s2 < 1:
		ans += 0
	else:
		if e[0] <= c:
			if e[1] <= d:
				t = 1
			else:
				t = min(e[1] - d + 1, c - b + 1)
		else:
			print(1 / 0)
			t = min(c - b + 1, max(e[0] - c + 1, e[1] - d + 1))
		temp = (s2 - 1) * s2 // 2 - (s1 - 1) * s1 // 2
		temp += max(0, (s2 - 1) * s2 // 2 - (s3 - 1) * s3 // 2)
		temp += s2 * t
		ans += temp
print(ans)


(a, b, c, d) = map(int, input().split())
gr = max(a + b, c + 1)
ans = 0
for i in range(gr, c + b + 1):
	x = a
	y = b
	cur = i - a - b
	if cur <= b - a:
		x += cur
		cur = 0
	else:
		cur += a - b
		x = b
	y += cur
	ans += (min(d, i - 1) - c + 1) * (min(x - a, c - y) + 1)
print(ans)


import sys, math
import io, os

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
INF = float('inf')
mod = int(1000000000.0) + 7
(a, b, c, d) = mdata()
ans = 0
l = [0] * (b + c - a + 1)
for i in range(b - a + 1):
	l[i + b - 1] += 1
	l[i + c] -= 1
for i in range(1, b + c - a + 1):
	l[i] += l[i - 1]
for i in range(a + b, b + c + 1):
	ans += l[i - a - 1] * min(max(0, i - c), d - c + 1)
out(ans)


(a, b, c, d) = map(int, input().split())

def ncombi(n):
	if n <= 0:
		return 0
	else:
		return n * (n + 1) // 2
ans = 0
for x in range(c, d + 1):
	ans += ncombi(b + c - x) - ncombi(2 * b - x - 1) - ncombi(a + c - x - 1) + (2 * b - x - 1 > b - a) * ncombi(2 * b - x - 2 + a - b)
print(ans)


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
(a, b, c, d) = int_array()
prefs = [0] * (b + c + 3)
for i in range(a, b + 1):
	prefs[i + b] += 1
	prefs[i + c + 1] -= 1
for i in range(1, len(prefs)):
	prefs[i] += prefs[i - 1]
for i in range(len(prefs) - 2, -1, -1):
	prefs[i] += prefs[i + 1]
ans = 0
for i in range(c, d + 1):
	if i >= len(prefs) - 1:
		break
	ans += prefs[i + 1]
print(ans)


for _ in range(1):
	(a, b, c, d) = map(int, input().split())
	ans = 0
	for i in range(a, b + 1):
		x = b + i - 1
		y = c + i - 1
		if y < c:
			pass
		elif x > d:
			m = y - x + 1
			ans += m * (d - c + 1)
		else:
			m = max(0, y - d)
			ans += m * (d - c + 1)
			m = min(d - c + 1, min(d, y) - c + 1)
			if m < 0:
				pass
			else:
				ans += m * (m + 1) // 2
			m = max(1, max(c, x) - c + 1) - 1
			ans -= m * (m + 1) // 2
	print(ans)


import sys

def solve():
	input = sys.stdin.readline
	(A, B, C, D) = map(int, input().split())
	ans = 0
	fib = [0] * (D + 1)
	fib[1] = 1
	for i in range(2, D + 1):
		if i <= C - B + 1:
			fib[i] = fib[i - 1] + i
		else:
			fib[i] = fib[i - 1] + (C - B + 1)
	for z in range(C, D + 1):
		if B + C <= z:
			continue
		else:
			xlow = z - C + 1
			pN = B - xlow + 1
			psubN = A - xlow
			if xlow >= A:
				ans += fib[pN]
			else:
				ans += fib[pN] - fib[psubN]
	print(ans)
	return 0

def __starting_point():
	solve()
__starting_point()


(A, B, C, D) = map(int, input().split())
ans = 0

def len(a, b):
	return b - a + 1
for z in range(C, D + 1):
	if z - A + 1 < B:
		ans += (C - B + 1) * (B - A + 1)
	elif B <= z - A + 1 <= C:
		l1 = len(z - A + 1, C)
		ans += l1 * (B - A + 1)
		miny1 = B
		miny2 = z - A
		len1 = max(0, B - z + miny1)
		len2 = max(0, B - z + miny2)
		ans += (len1 + len2) * len(len1, len2) // 2
	elif C < z - A + 1:
		miny1 = B
		miny2 = C
		len1 = max(0, B - z + miny1)
		len2 = max(0, B - z + miny2)
		ans += (len1 + len2) * len(len1, len2) // 2
print(ans)


from sys import *
from collections import *
from math import *
input = stdin.readline
global a, b, c, d

def binary_search(y, lo, hi, sum):
	id = -1
	while lo <= hi:
		mid = (lo + hi) // 2
		cnt = min(mid + y, d + 1) - c
		cnt = max(cnt, 0)
		if cnt >= sum:
			hi = mid - 1
			id = mid
		else:
			lo = mid + 1
	return id
(a, b, c, d) = map(int, input().split())
triangle = 0
for y in range(b, c + 1):
	start = binary_search(y, a, b, 1)
	if start is -1:
		continue
	cnt1 = min(start + y, d + 1) - c
	cnt1 = max(cnt1, 0)
	cnt2 = min(b + y, d + 1) - c
	cnt2 = max(cnt2, 0)
	end = binary_search(y, a, b, cnt2)
	if end is -1:
		continue
	len1 = end - start
	sum = (2 * cnt1 + (len1 - 1)) * len1 // 2
	len2 = b - end + 1
	sum += len2 * cnt2
	triangle += sum
print(triangle)


(A, B, C, D) = map(int, input().split())
arr = [0] * 1000077
for i in range(A, B + 1):
	arr[i + B] += 1
	arr[i + C + 1] -= 1
for i in range(1, len(arr)):
	arr[i] += arr[i - 1]
for i in range(1, len(arr)):
	arr[i] += arr[i - 1]
Sum = 0
for i in range(C, D + 1):
	Sum += arr[len(arr) - 1] - arr[i]
print(Sum)


(a, b, c, d) = map(int, input().split())
zmax = b + c
zmin = max(c, a + b)
res = 0
for z in range(zmin, zmax + 1):
	xmax = min(b, z - b)
	xmin = max(a, z - c)
	if xmin <= xmax:
		res += min(z - c, d - c + 1) * (xmax - xmin + 1)
print(res)


def solve(a: int, b: int, c: int, d: int) -> int:
	return sum([(v - u + 1) * w for (u, v, w) in [(max(summa - c, a), min(summa - b, b), min(summa - c, d - c + 1)) for summa in range(c + 1, b + c + 1)] if u <= v])
(a, b, c, d) = map(int, input().split())
print(solve(a, b, c, d))


(a, b, c, d) = map(int, input().split())
(total, temp) = (0, 0)
freq = [0] * 2000007
for x in range(a, b + 1):
	freq[x + b] += 1
	freq[x + c + 1] -= 1
for i in range(1, b + c + 2):
	freq[i] += freq[i - 1]
for i in range(b + c + 1, -1, -1):
	freq[i] += freq[i + 1]
for z in range(c, d + 1):
	total += freq[z + 1]
print(total)


from sys import stdin
input = stdin.readline
(a, b, c, d) = [int(x) for x in input().strip().split()]
ans = 0
n = 10 ** 6 + 100
m = [0] * n
for i in range(a, b + 1):
	m[i + b] += 1
	m[i + c + 1] -= 1
i = 1
while i < n:
	m[i] += m[i - 1]
	i += 1
i = 1
while i < n:
	m[i] += m[i - 1]
	i += 1
ans = 0
for i in range(c, d + 1):
	ans += m[n - 1] - m[i]
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
for diff in range(max(0, c - a - b + 1), c - a + 1):
	k = min(b - a, diff) - max(0, diff - c + b)
	kk = min(diff, c - b) - max(0, diff - b + a)
	ch = (min(a + b + diff, d + 1) - c) * (min(k, kk, diff) + 1)
	if k >= 0 and kk >= 0:
		ans += ch
print(ans)


import sys
input = sys.stdin.readline
(A, B, C, D) = list(map(int, input().split()))

def sm2(x, A, B, C):
	MIN = A + B
	MAX = B + C
	fr = min(B - A, C - B)
	sa = min(x - MIN, MAX - x, fr)
	return sa + 1

def calc(x, C, D):
	return max(0, min(x - 1, D) - C + 1)
ANS = 0
for x in range(A + B, B + C + 1):
	smplus = sm2(x, A, B, C)
	ANS += smplus * calc(x, C, D)
print(ANS)


(a, b, c, d) = map(int, input().split())
s = 0
k = 0
l = 0
rar = min(b - a, c - b)
rer = max(b - a, c - b)
while l <= rar:
	if a + b + l > c:
		s += (k + 1) * (min(a + b + l, d + 1) - c)
	k += 1
	l += 1
k -= 1
while l <= rer:
	if a + b + l > c:
		s += (k + 1) * (min(a + b + l, d + 1) - c)
	l += 1
k -= 1
while k >= 0:
	if a + b + l > c:
		s += (k + 1) * (min(a + b + l, d + 1) - c)
	k -= 1
	l += 1
print(s)


def sigma(x):
	return x * (x + 1) // 2

def answer(A, B, C, D):
	_sum = 0
	for i in range(A, B + 1):
		_sum += i * (i + 1) // 2
		_length = C - B + 1
		if i - _length > 0:
			_sum -= sigma(i - _length)
		headMax = D - C + 1
		if i > headMax:
			_sum -= sigma(i - headMax)
			if i - headMax > _length:
				_sum += sigma(i - headMax - _length)
	return _sum
(A, B, C, D) = map(int, input().split())
print(answer(A, B, C, D))


import sys
input = sys.stdin.readline
(a, b, c, d) = map(int, input().split())
ans = 0
pattern = 0
min_diff = min(b - a + 1, c - b + 1)
for xy_sum in range(a + b, b + c + 1):
	pattern = min(xy_sum - a - b + 1, b + c - xy_sum + 1)
	if pattern > min_diff:
		pattern = min_diff
	z_ceil = min(d + 1, xy_sum)
	z_floor = c
	if z_ceil > z_floor:
		val = (z_ceil - z_floor) * pattern
		ans += val
print(ans)


import itertools
(a, b, c, d) = map(int, input().split())

def solve(a, b, c, d):
	ans = 0
	pre = [0] * (b + c + 2)
	for i in reversed(range(a, b + 1)):
		pre[i + b] += 1
		pre[i + c + 1] -= 1
	pre = list(itertools.accumulate(pre))
	pre = list(itertools.accumulate(pre))
	a = max(pre)
	last = 0
	for i in range(c, d + 1):
		if i < b + c + 1:
			last = a - pre[i]
		ans += last
	return ans
print(solve(a, b, c, d))


def s(j):
	j -= A + B
	if t1 >= j and t2 >= j:
		return j + 1
	elif t1 >= j:
		return t2 + 1
	elif t2 >= j:
		return t1 + 1
	else:
		return t1 + t2 - j + 1
(A, B, C, D) = map(int, input().split())
ans = 0
t1 = B - A
t2 = C - B
for i in range(A + B, B + C + 1):
	if i > D:
		ans += s(i) * (D - C + 1)
	else:
		ans += max(s(i) * (i - C), 0)
print(ans)


(a, b, c, d) = map(int, input().split())
prefsum = [0] * (b + c + 2)
for i in range(a, b + 1):
	prefsum[i + b] += 1
	prefsum[i + c + 1] += -1
for i in range(1, len(prefsum)):
	prefsum[i] += prefsum[i - 1]
for i in range(1, len(prefsum)):
	prefsum[i] += prefsum[i - 1]
ans = 0
for z in range(c, d + 1):
	if z < b + c + 2:
		ans += prefsum[-1] - prefsum[z]
	else:
		break
print(ans)


import sys
(A, B, C, D) = map(int, sys.stdin.readline().split())
N = D * 3
a = [0] * N
ans = 0
for i in range(A, B + 1):
	a[i + B] += 1
	a[i + C + 1] -= 1
for i in range(1, N):
	a[i] += a[i - 1]
for i in range(1, N):
	a[i] += a[i - 1]
for i in range(C, D + 1):
	ans += a[N - 1] - a[i]
print(ans)


from sys import stdin, stdout

def main():
	(a, b, c, d) = list(map(int, stdin.readline().split()))
	arr = [0] * 1000010
	for i in range(a, b + 1):
		arr[i + b] += 1
		arr[i + c + 1] -= 1
	for i in range(1, 1000010):
		arr[i] += arr[i - 1]
	for i in range(1, 1000010):
		arr[i] += arr[i - 1]
	cnt = 0
	for i in range(c, d + 1):
		cnt += arr[1000009] - arr[i]
	stdout.write(str(cnt))
main()


(A, B, C, D) = map(int, input().split())

def n_x(d):
	return min(max(0, B - d), B - A + 1)
c = 0
m = min(D - C + 1, C - B + 1)
dist = D - B + 1
for delta in range(0, dist):
	if delta < m:
		c += (delta + 1) * n_x(delta)
	elif dist - delta - 1 < m:
		c += (dist - delta) * n_x(delta)
	else:
		c += m * n_x(delta)
print(c)


(a, b, c, d) = map(int, input().split())
x = [0] * (2 * 5 * 10 ** 5 + 1)
ans = 0
for i in range(a + b, b + c + 1):
	v = min(i - a - b + 1, b - a + 1, c - b + 1, abs(i - b - c) + 1)
	x[i] += v
for i in range(len(x) - 1):
	x[i + 1] += x[i]
for i in range(c, d + 1):
	ans += x[-1] - x[i]
print(ans)


from sys import stdin, stdout
from itertools import accumulate

def MI():
	return map(int, stdin.readline().split())
(a, b, c, d) = MI()
ans = 0
arr = [0] * (b + c + 2)
for x in range(a, b + 1):
	arr[x + b] += 1
	arr[x + c + 1] -= 1
psum = list(accumulate(arr))
psum = list(accumulate(psum))
e = min(d, b + c)
for z in range(c, e + 1):
	ans += psum[-1] - psum[z]
stdout.write(str(ans) + '\n')


(A, B, C, D) = map(int, input().split())
ans = 0
bg = max(C + 1, A + B)
for i in range(bg, B + C + 1):
	res = i - A - B
	(x, y) = (A, B)
	if res > B - A:
		x = B
		res -= B - A
	else:
		x += res
		res = 0
	y += res
	ans += (min(x - A, C - y) + 1) * (min(i - 1, D) - C + 1)
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
for xy in range(c + 1, b + c + 1):
	if xy < a + b:
		continue
	temp = 0
	if xy - b > b:
		temp = min(b - a, c - (xy - b)) + 1
	else:
		temp = min(xy - b - a, c - b) + 1
	ans += (min(d + 1, xy) - c) * temp
print(ans)


from sys import stdin
from collections import Counter, deque
from heapq import heappush, heappop
from itertools import product
input = stdin.buffer.readline
(a, b, c, d) = map(int, input().split())
(n, mn) = (c - a + 1, min(b - a + 1, c - b + 1))
cc = [mn] * n
for i in range(mn):
	(cc[i], cc[-i - 1]) = (i + 1, i + 1)
for i in reversed(range(0, n - 1)):
	cc[i] += cc[i + 1]
res = 0
for u in range(c, d + 1):
	p = u + 1 - (a + b)
	if p < 0:
		res += cc[0]
	elif p >= n:
		res += 0
	else:
		res += cc[p]
print(res)


import sys
I = sys.stdin.readline

def read_ints():
	return list(map(int, I().split(' ')))

def sum_up_to(num):
	return num * (num + 1) // 2

def solve():
	(a, b, c, d) = read_ints()
	ans = 0
	for z in range(c, d + 1):
		p = max(z + 1 - b, b)
		if p > c:
			break
		x_base = z - p + 1
		if x_base < a:
			x_base = a
		y_range = c - p
		x_range = min(x_base - a, y_range)
		l = b - x_base + 1
		r = l + x_range
		ans += sum_up_to(r) - sum_up_to(l - 1)
		ans += (b - a + 1) * (y_range - x_range)
	return ans
print(solve())


def f(n):
	if n <= 0:
		return 0
	else:
		return n

def l(p, q, s):
	return f(s + 1) - f(s - p) - f(s - q) + f(s - p - q - 1)
(a, b, c, d) = list(map(int, input().split()))
p = b - a
q = c - b
r = d - c
ans = 0
for i in range(1, c + b - d + 1):
	ans += l(p, q, d + i - a - b)
if c == d:
	print(ans)
else:
	ans *= d - c + 1
	for i in range(d, c, -1):
		m = l(p, q, i - a - b)
		ans += m * (i - c)
	print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
for x in range(a, b + 1):
	if x + b <= d:
		x0 = max(0, x + b - c)
		xn = min(x, d - c)
		p = max(0, x + c - d) * (d - c + 1)
		tmp = (x0 + xn) * (xn - x0 + 1) // 2 + p
	else:
		tmp = (c - b + 1) * (d - c + 1)
	ans += tmp
print(ans)


(a, b, c, d) = map(int, input().split())
dp = [0] * (b + c + 2)
su = 0
for i in range(a, b + 1):
	sl = b + i
	sh = c + i
	dp[sl] += 1
	dp[sh + 1] -= 1
for i in range(1, b + c + 2):
	dp[i] = dp[i] + dp[i - 1]
for i in range(len(dp) - 3, -1, -1):
	dp[i] = dp[i + 1] + dp[i]
for i in range(c, d + 1):
	if i < len(dp) - 1:
		su += dp[i + 1]
print(su)


import os
import operator
import sys
from io import BytesIO, IOBase

def summation(num):
	if num == 0:
		return 0
	else:
		return num * (num + 1) // 2

def main():
	(a, b, c, d) = map(int, input().split())
	result = 0
	left = b
	count = 0
	for i in range(c, d + 1):
		while i - left + 1 > b and left <= c:
			left += 1
		if left > c:
			break
		x = i - left + 1
		if x < a:
			x = a
		midrem = c - left
		count = x - a
		if left + count <= c:
			l = b - x + 1
			r = l + count
			result += summation(r) - summation(l - 1)
			rem = midrem - count
			result += rem * r
		else:
			l = b - x + 1
			r = l + midrem
			result += summation(r) - summation(l - 1)
	print(result)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import defaultdict as dc
from collections import Counter
from heapq import *
import math
from bisect import bisect_left, bisect
import sys
from collections import deque as dq
from heapq import heapify, heappush, heappop
mod = 10 ** 9 + 7

def sinp():
	p = str(sys, stdin.readline())
	return p

def seive(n):
	p = [0] * (n + 1)
	for i in range(2, n):
		if p[i] == 0:
			for j in range(2 * i, n + 1, i):
				p[i] = 1
	return p

def lower_bound(a, i, n):
	return bisect_left(a, i)

def upper_bound(a, i, n):
	return n - bisect_left(a, i)

def inp():
	p = int(sys.stdin.readline())
	return p

def line():
	p = list(map(int, sys.stdin.readline().split()))
	return p

def read_mat():
	n = inp()
	a = []
	for i in range(n):
		a.append(line())
	return a

def digit(n):
	s = str(n)
	p = 0
	for i in s:
		p += int(i) ** 2
	return p

def solve(a, b, c, d):
	if a == b and b == c and (c == d):
		return 1
	z = list(range(c, d + 1))
	l = min(c - b + 1, b - a + 1)
	t = c - a + 1
	p = [0] * t
	(x, y) = (0, t - 1)
	(a1, b1) = (1, l)
	c1 = 0
	tot = (d - c + 1) * (c - b + 1) * (b - a + 1)
	while x <= y:
		p[x] = min(a1, l)
		p[y] = min(a1, l)
		a1 += 1
		x += 1
		y -= 1
	t = d - c + 1
	if a + b <= d:
		ans = 0
		for i in range(a + b, b + c + 1, 1):
			ans += upper_bound(z, i, t) * p[i - (a + b)]
		return tot - ans
	if a + b > d:
		return tot
t = 1
for _ in range(t):
	(a, b, c, d) = line()
	l = solve(a, b, c, d)
	print(l)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque

def check(x):
	if x <= 0:
		return 0
	else:
		return x * (x + 1) // 2
(a, b, c, d) = map(int, input().split())
count = 0
for z in range(c, d + 1):
	count += check(b + c - z)
	count -= check(2 * b - z - 1)
	count -= check(a + c - z - 1)
	if b + a - z > 1:
		count += check(b + a - z - 2)
print(count)


(A, B, C, D) = [int(i) for i in input().split()]
ans = 0
for x in range(A, B + 1):
	ans += max(0, C - max(D - x + 1, B) + 1) * (D - C + 1)
	m = max(0, min(C, D - x) - max(B, C - x + 1) + 1)
	ans += m * (x - C)
	ans += m * (min(C, D - x) + max(B, C - x + 1)) // 2
print(ans)


def solve():
	(a, b, c, d) = [int(x) for x in input().split()]
	(count, left) = (0, b)
	for z in range(c, d + 1):
		x = z - left + 1
		while x > b and left <= c:
			left += 1
			x = z - left + 1
		if x < a:
			x = a
		if left > c:
			break
		avail = b - x + 1
		l = x - a
		count += avail * (c - left + 1)
		if c - left + 1 == l:
			l -= 1
			count += l * (l + 1) // 2
		elif c - left + 1 < l:
			l = c - left + 1
			l -= 1
			count += l * (l + 1) // 2
		else:
			l -= 1
			count += l * (l + 1) // 2
			l += 1
			count += l * (c - left + 1 - l)
	print(count)
solve()


(a, b, c, d) = map(int, input().split())
cnt = 0
for s in range(a + b, b + c + 1):
	maxZ = s - 1
	if maxZ < c:
		continue
	goodZ = min(d, maxZ) - c + 1
	minX = a
	maxX = min(s // 2, b)
	if minX > maxX:
		continue
	minY = max(s - s // 2, b)
	maxY = min(s - 1, c)
	if minY > maxY:
		continue
	begin1 = minX - 1
	end1 = maxX - 1 + 1
	begin2 = s - 1 - maxY
	end2 = s - 1 - minY + 1
	begin = max(begin1, begin2)
	end = min(end1, end2)
	if begin < end:
		cnt += goodZ * (end - begin)
print(cnt)


def f(n):
	if n <= 0:
		return 0
	return n * (n + 1) // 2
(a, b, c, d) = map(int, input().split())
ans = 0
for z in range(c, d + 1):
	ans += f(b + c - z) - f(2 * b - z - 1) - f(a + c - z - 1) + (2 * b - z - 1 > b - a) * f(2 * b - z - 2 + a - b)
print(ans)


from sys import stdin, stdout
import collections
import heapq
import itertools
import functools

def read_line():
	return stdin.readline().strip()

def read_int():
	return int(read_line())

def read_arr():
	return read_line().split(' ')

def read_int_arr():
	return [int(x) for x in read_arr()]

def query_interactive(s):
	print(s)
	stdout.flush()

def main():
	(A, B, C, D) = read_int_arr()
	pre = [0] * (2 * 500005)
	for a in range(A, B + 1):
		pre[a + B] += 1
		pre[a + C + 1] -= 1
	pre = list(itertools.accumulate(pre))
	pre = list(itertools.accumulate(pre))
	res = 0
	for c in range(C, D + 1):
		lower = pre[c]
		upper = pre[-1]
		res += upper - lower
	print(res)
main()


(a, b, c, d) = map(int, input().split())
ans = 0
count = [0] * (10 ** 6 + 10)
for x in range(a, b + 1):
	count[x + b] += 1
	count[x + c + 1] -= 1
for j in range(2):
	for i in range(1, 10 ** 6 + 10):
		count[i] += count[i - 1]
for z in range(c, d + 1):
	ans += (b - a + 1) * (c - b + 1) - count[z]
print(ans)


from bisect import *
from sys import *
input = stdin.readline
(A, B, C, D) = map(int, input().split())
n = max(D + 2, B + C + 2)
a = [0] * n
for i in range(A, B + 1):
	l = i + B
	r = i + C + 1
	a[l] += 1
	a[r] -= 1
sn = 0
for i in range(n):
	sn += a[i]
	a[i] = sn
sn = 0
for i in range(n - 1, -1, -1):
	sn += a[i]
	a[i] = sn
sn = 0
for i in range(C, D + 1):
	sn += a[i + 1]
stdout.write(str(sn) + '\n')


inp = input().split()
a = int(inp[0])
b = int(inp[1])
c = int(inp[2])
d = int(inp[3])
ans = c + 1
other = ans - a + 1 - b
other2 = b - a + 1
correct = min(other, other2)
if correct < 0:
	correct = 0
i = ans
k = 0
condition = min(b * 2, c + a)
condition2 = max(b * 2, c + a)
count = 0
while i <= b + c:
	if k <= d - c:
		k = k + 1
	count = count + k * correct
	if i < condition:
		correct = correct + 1
	elif i >= condition2:
		correct = correct - 1
	if other < 0:
		correct = 0
		other += 1
	i = i + 1
print(count)


ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
(a, b, c, d) = ip()
dp = [0] * (b + c + 2)
for i in range(a, b + 1):
	dp[i + b] += 1
	dp[i + c + 1] -= 1
for i in range(1, len(dp)):
	dp[i] += dp[i - 1]
ans = 0
for i in range(a + b, b + c + 2):
	if i > d:
		ans += dp[i] * (d - c + 1)
	elif i > c:
		ans += dp[i] * (i - c)
print(ans)


(A, b, c, d) = map(int, input().split())
ans = 0
for a in range(A, b + 1):
	if a + b < c:
		t = c - a
		t += 1
		if t > c:
			continue
		else:
			sv = c - t + 1
			tv = d - c + 1
			if sv > tv:
				ans = ans + sv * tv - (tv - 1) * tv // 2
			else:
				ans = ans + sv * (sv + 1) // 2
	elif a + b >= c:
		t = a + b
		if t > d:
			ans = ans + (c - b + 1) * (d - c + 1)
			continue
		tb = b
		ans = ans + (t - c) * (c - b + 1)
		b += 1
		sv = c - b + 1
		tv = d - t + 1
		if sv > tv:
			ans = ans + sv * tv - (tv - 1) * tv // 2
		else:
			ans = ans + sv * (sv + 1) // 2
		b = tb
print(ans)


import sys
readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
	(a, b, c, d) = nm()
	m = 10 ** 6 + 10
	l = [0] * m
	for i in range(a, b + 1):
		l[i + b] += 1
		l[i + c + 1] += -1
	for i in range(m - 1):
		l[i] += l[i - 1]
	for i in range(m - 2, -1, -1):
		l[i] += l[i + 1]
	print(sum((l[i + 1] for i in range(c, d + 1))))
	return
solve()


(a, b, c, d) = list(map(int, input().split()))
arr = [0] * 1500005
for i in range(a, b + 1):
	arr[i + b] += 1
	arr[i + c + 1] -= 1
for i in range(1, len(arr)):
	arr[i] += arr[i - 1]
for i in range(len(arr) - 1, 0, -1):
	arr[i - 1] += arr[i]
ans = 0
for i in range(c, d + 1):
	ans += arr[i + 1]
print(ans)


(a, b, c, d) = map(int, input().split())
l = []
l1 = []
for i in range(a + b, b + c + 1):
	l.append(i)
	x = i - c
	y = i - b
	l1.append(max(0, min(b, y) - max(a, x) + 1))
ans = 0
for i in range(len(l)):
	if l[i] > c:
		ans += l1[i] * min(l[i] - c, d - c + 1)
print(ans)


def main():
	t = 1
	for _ in range(t):
		(a, b, c, d) = map(int, input().split())
		ans = 0
		for i in range(b, c + 1):
			mix = min(b, max(a, c - i + 1))
			mx = min(b, max(a, d - i + 1))
			if mx + i <= c:
				continue
			ok = min(d, mx + i - 1) - c + 1
			l = min(ok, mix + i - c)
			l -= 1
			ans += ok * (ok + 1) // 2
			ans -= l * (l + 1) // 2
			ans += (b - mx) * ok
		print(ans)
	return
main()


(a, b, c, d) = [int(i) for i in input().split()]
count = 0
for x in range(a, b + 1):
	min_b = c - x + 1
	if min_b < b:
		min_b = b
	iters = c - min_b
	max_sum = x + min_b
	if max_sum > d:
		max_sum = d + 1
	i0 = max_sum - c
	last_max_sum = max_sum + iters
	if last_max_sum > d:
		extra_iters = last_max_sum - d - 1
		iters = iters - extra_iters
		last_max_sum = max_sum + iters
		count += extra_iters * (last_max_sum - c)
	i_n = last_max_sum - c
	if iters == 0:
		total = i0
	else:
		total = i_n * (i_n + 1) // 2 - i0 * (i0 - 1) // 2
	count += total
print(count)


import sys, math, cmath, time, bisect
from collections import deque
start_time = time.time()
tt = 0

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def solve():
	(a, b, c, d) = invr()
	q = 0
	w = 0
	for i in range(b + c, c - 1, -1):
		if i <= d:
			q = q + w
		w = w + max(a // (c + 1), min(i - b, b) - max(i - c, a) + 1)
	print(q)

def main():
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
	sys.stdout.close()

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


(a, b, c, d) = list(map(int, input().split()))
ans = 0
y = b
x = a
while y + a < d + 1 and y <= c:
	if y + b > c:
		j = max(a, c + 1 - y) + y - c
		i = min(y + b - c, d - c + 1)
		if (d - c + 1) * (y + b - d - 1) > 0:
			ans += i * (i + 1) // 2 - j * (j - 1) // 2 + (d - c + 1) * (y + b - d - 1)
		else:
			ans += i * (i + 1) // 2 - j * (j - 1) // 2
	y += 1
ans += (c - y + 1) * (b - a + 1) * (d - c + 1)
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
ru = [0] * (10 ** 6 + 10)
for x in range(a, b + 1):
	l = x + b
	r = x + c + 1
	ru[l] += 1
	ru[r] -= 1
for i in range(10 ** 6 + 9):
	ru[i + 1] += ru[i]
for i in range(10 ** 6 + 9):
	ru[i + 1] += ru[i]
ans = 0
for z in range(c, d + 1):
	ans += ru[-1] - ru[z]
print(ans)


import sys
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	tc = 0
	if tc:
		input = sys.stdin.readline
	else:
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')

def solve():
	(a, b, c, d) = mi()
	f = [0] * 1000005
	for i in range(a, b + 1):
		f[i + b] += 1
		f[i + c + 1] -= 1
	for i in range(a + b, b + c + 1):
		f[i] += f[i - 1]
	ans = 0
	for i in range(a + b, b + c + 1):
		x = min(i - 1, d)
		ans += f[i] * max(x - c + 1, 0)
	print(ans)
solve()


def findFor(x):
	reqForb1 = x - B[1]
	reqForb0 = x - B[0]
	ans = 0
	if reqForb1 > A[1]:
		return 0
	else:
		if A[0] <= reqForb1 <= A[1]:
			no1 = A[1] - reqForb1 + 1
			ans += (no1 + 1) * no1 // 2
			if reqForb0 > A[1]:
				no2 = 1
			else:
				no2 = A[1] - reqForb0 + 1
			ans -= (no2 - 1) * no2 // 2
		else:
			no1 = A[1] - reqForb1 + 1
			ans += (no1 + 1) * no1 // 2
			if reqForb0 < A[0]:
				ans = (A[1] - A[0] + 1) * (B[1] - B[0] + 1)
			elif reqForb0 > A[1]:
				ans = (A[1] - A[0] + 1) * (A[0] - reqForb1)
				no1 = A[1] - A[0] + 1
				ans += no1 * (no1 + 1) // 2
			else:
				ans = (A[1] - A[0] + 1) * (A[0] - reqForb1)
				no1 = A[1] - A[0] + 1
				no2 = A[1] - reqForb0
				ans += no1 * (no1 + 1) // 2
				ans -= no2 * (no2 + 1) // 2
		return ans
(a, b, c, d) = list(map(int, input().split()))
A = [a, b]
B = [b, c]
C = [c, d]
sum = 0
for i in range(c, d + 1, 1):
	sum += findFor(i + 1)
print(sum)


import sys
from math import ceil, sqrt
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())

def isp(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
primes = []

def mxd(a):
	mx = 0
	while a > 0:
		mx = max(a % 10, mx)
		a //= 10
	return mx

def mnd(a):
	mn = 10
	while a > 0:
		mn = min(mn, a % 10)
		a //= 10
	return mn
for _ in range(1):
	(a, b, c, d) = mi()
	pref = [0] * (b + c + 10)
	for i in range(a, b + 1):
		(l, r) = (i + b, i + c)
		pref[l] += 1
		pref[r + 1] -= 1
	for i in range(1, b + c + 10):
		pref[i] += pref[i - 1]
	ans = 0
	for i in range(a + b, b + c + 1):
		ans += pref[i] * max(0, min(i, d + 1) - c)
	print(ans)


(a, b, c, d) = [int(i) for i in input().split()]
cnt = 0
cmp = [0] * (c + d + 2)
for x in range(a, b + 1):
	cmp[x + b] += 1
	cmp[x + c + 1] -= 1
for i in range(1, c + d + 1):
	cmp[i + 1] += cmp[i]
res = 0
for i in range(c + d + 2):
	res += cmp[i] * max(0, min(d, i - 1) - c + 1)
print(res)


(a, b, c, d) = map(int, input().split(' '))
li = [0] * int(pow(10, 6) + 2)
n = int(pow(10, 6) + 2)
for i in range(a, b + 1):
	li[i + b] += 1
	li[i + c + 1] -= 1
for i in range(1, n):
	li[i] += li[i - 1]
for i in range(1, n):
	li[i] += li[i - 1]
ans = 0
for i in range(c, d + 1):
	ans += li[n - 1] - li[i]
print(ans)


import sys
input = sys.stdin.buffer.readline
(A, B, C, D) = map(int, input().split())
tmp = 0
ans = 0
for k in reversed(range(C, B + C + 1)):
	if k <= D:
		ans += tmp
	tmp += max(min(B, k - B) - max(A, k - C) + 1, 0)
print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
vals = list(map(int, input().split()))
a = vals[0]
b = vals[1]
c = vals[2]
d = vals[3]
triang = 0
for j in range(a + b, b + c + 1):
	xm = max(a, min(b, j - c))
	ym = max(b, min(c, j - a))
	if j >= c + 1 and j <= d + 1:
		triang += min(b + 1 - xm, ym + 1 - b) * (j - c)
	elif j >= d + 1:
		triang += min(b + 1 - xm, ym + 1 - b) * (d + 1 - c)
print(triang)


(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(a, b + 1):
	x = i + b
	y = i + c
	if y <= c:
		continue
	if x <= c and y <= d:
		ans += (y - c + 1) * (y - c) // 2
		continue
	if x >= d:
		ans += (d - c + 1) * (y - x + 1)
		if x == d:
			ans -= 1
		continue
	if x > c and y <= d:
		ans += (y - c) * (y - c + 1) // 2
		ans -= (x - c) * (x - c - 1) // 2
		continue
	if x > c and y > d:
		ans += (d - c) * (d - c + 1) // 2
		ans -= (x - c) * (x - c - 1) // 2
		ans += (y - d) * (d - c + 1)
		continue
	if x <= c and y > d:
		ans += (d - c + 1) * (d - c) // 2
		ans += (y - d) * (d - c + 1)
		continue
print(ans)


import sys
(A, B, C, D) = map(int, sys.stdin.readline().split())

def count(n):
	upper = min(B - A, C - B) + 1
	est = min(n - A, C) - max(B, n - B) + 1
	return min(upper, est)
ans = 0
for rest in range(max(A + B, C + 1), B + C + 1):
	num_avail_z = min(D - C + 1, rest - C)
	ans += num_avail_z * count(rest)
print(ans)


(a, b, c, d) = map(int, input().split())
ans = 0
arr = [0] * (2 * d + 5)
for i in range(a, b + 1):
	arr[i + b] += 1
	arr[i + c + 1] -= 1
s = 0
for ii in range(2 * d + 5):
	s += arr[ii]
	if c < ii:
		ans += min(ii - c, d - c + 1) * s
print(ans)


(A, B, C, D) = map(int, input().split())
X = (A, B)
Y = (B, C)
Z = (C, D)
z_max = min(B + C - 1, D)
z_min = C
ans = 0
for z in range(z_min, z_max + 1):
	x_min = max(0, A)
	x_max = min(B, z)
	y_min = max(0, B)
	y_max = min(C, z)
	if z < x_min + y_min:
		ans += (x_max - x_min + 1) * (y_max - y_min + 1)
	elif z > x_max + y_max:
		continue
	else:
		z_up = z - y_max
		z_down = z - y_min
		z_left = z - x_min
		z_right = z - x_max
		if z_left <= y_max and z_left >= y_min:
			if z_down <= x_max and z_down >= x_min:
				ans += (x_max - x_min + 1) * (y_max - y_min + 1) - (z_left - y_min + 1) * (z_left - y_min + 2) // 2
			else:
				ke = y_max - z_right
				ek = y_max - z_left
				ans += (ke - ek + 1) * (ke + ek) // 2
		elif z_right <= y_max and z_right >= y_min:
			ans += (y_max - z_right + 1) * (y_max - z_right) // 2
		else:
			ans += (y_max - y_min + 1) * (x_max - z_up + x_max - z_down) // 2
print(ans)


import sys
input = sys.stdin.readline
N = 10 ** 6 + 77
(A, B, C, D) = map(int, input().strip().split())
cnt = 0
a = [0] * N
for i in range(A, B + 1):
	a[i + B] += 1
	a[i + C + 1] -= 1
for i in range(1, N):
	a[i] += a[i - 1]
for i in range(1, N):
	a[i] += a[i - 1]
ans = 0
for i in range(C, D + 1):
	ans += a[N - 1] - a[i]
print(ans)


(a, b, c, d) = map(int, input().split())
pref = [0 for i in range(2 * d + 10)]
for i in range(a, b + 1):
	l = i + b
	r = i + c
	pref[l] += 1
	pref[r + 1] -= 1
sums = [0 for i in range(2 * d + 10)]
for i in range(1, len(sums)):
	sums[i] = sums[i - 1] + pref[i]
ans = 0
j = 1
for i in range(c + 1, d + 2):
	ans += sums[i] * j
	j += 1
for i in range(d + 2, len(sums)):
	ans += (d - c + 1) * sums[i]
print(ans)


(a, b, c, d) = map(int, input().split())
s = 0
f = [0 for i in range(2000003)]
for i in range(a, b + 1, 1):
	f[b + i] += 1
	f[c + i + 1] -= 1
u = 0
for i in range(1, 1000002, 1):
	u += f[i]
	f[i] = f[i - 1] + u
for i in range(c, d + 1, 1):
	s += f[1000001] - f[i]
print(s)


(a, b, c, d) = map(int, input().split())
f = b - c
ans = [0]
for x in range(1, b + 1):
	asd = ans[-1]
	asd -= max(0, f)
	asd += min(d - c + 1, x)
	f = min(f + 1, min(d - c + 1, x))
	ans.append(asd)
print(sum(ans[a:]))


(a, b, c, d) = map(int, input().split())
sol = 0
z = c
sm = min(c - b, b - a)
bg = max(c - b, b - a)
while b + c >= z and z <= d:
	mn = a + b
	m1 = mn + sm
	mx = b + c
	m2 = mx - sm
	maxi = (sm + 1) * (bg + 1)
	if mn > z:
		sol += maxi
	if mn <= z and m1 > z:
		foo = z + 1 - mn
		sol += maxi
		sol -= foo * (foo + 1) // 2
	if m1 <= z and m2 > z:
		foo = sm * (sm + 1)
		foo //= 2
		maxi -= foo
		foo = z + 1 - m1
		maxi -= (sm + 1) * foo
		sol += maxi
	if m2 <= z and mx > z:
		foo = mx - z
		sol += foo * (foo + 1) // 2
	z += 1
print(sol)


import bisect

def solve(a, b, c, d):
	n = 10 ** 6 + 10
	arr = [0 for i in range(n)]
	for i in range(a, b + 1):
		arr[i + b] += 1
		arr[i + c + 1] += -1
	for i in range(1, n):
		arr[i] += arr[i - 1]
	for i in range(1, n):
		arr[i] += arr[i - 1]
	ans = 0
	for z in range(c, d + 1):
		ans += arr[n - 1] - arr[z]
	return ans

def read():
	(a, b, c, d) = list(map(int, input().strip().split()))
	ans = solve(a, b, c, d)
	print(ans)
read()


(a, b, c, d) = [int(x) for x in input().split()]
maxAmt = min(b - a + 1, c - b + 1)
low = b + a
high = c + b
sums = {high: 1}
for x in range(high - 1, low - 1, -1):
	sums[x] = sums[x + 1] + min(maxAmt, x - low + 1, high - x + 1)
total = 0
for x in range(c, d + 1):
	if x + 1 <= high:
		if x + 1 in sums:
			total += sums[x + 1]
		elif x < high:
			total += sums[low]
print(total)


(a, b, c, d) = map(int, input().split())
mi = a + b
mx = c + b
ans = 0
p = min(c - b + 1, b - a + 1)
q = max(c - b, b - a)
f = {}
for i in range(mi, mx + 1):
	t = 0
	(l, r) = (a, b + 1)
	for kek in range(20):
		j = (l + r) // 2
		if i - j <= c:
			r = j
		else:
			l = j
	funny1 = r
	(l, r) = (funny1, b + 1)
	for kek in range(20):
		j = (l + r) // 2
		if b <= i - j:
			l = j
		else:
			r = j
	funny2 = l
	f[i] = funny2 - funny1 + 1 + f.get(i - 1, 0)
for z in range(c, d + 1):
	if z >= mx:
		break
	g = min(mx - z, mx - mi + 1)
	ans += f[mx] - f.get(mx - g, 0)
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	(a, b, c, d) = map(int, input().split())
	al = rec(a, b, b, c)
	ans = 0
	for z in range(c, d + 1):
		t = 0
		if b + c <= z:
			break
		if a + b > z:
			t = al
		elif c - b <= b - a:
			if z <= a + c:
				t = al - tri1(a, b, z - b, z - a)
			elif z >= 2 * b:
				t = tri2(z - c, z - b, b, c)
			else:
				t = rec(z - b, b, b, c) + tri2(z - c, b, z - b, c) - (c - b + 1)
		elif z <= 2 * b:
			t = al - tri1(a, b, z - b, z - a)
		elif z >= a + c:
			t = tri2(z - c, z - b, b, c)
		else:
			t = rec(a, b, z - a, c) + tri2(a, z - b, b, z - a) - (b - a + 1)
		ans += t
	print(ans)

def rec(l, r, b, t):
	return (r - l + 1) * (t - b + 1)

def tri1(l, b, r, t):
	assert r - l == t - b
	R = rec(l, r, b, t)
	ol = r - l + 1
	return (R - ol) // 2 + ol

def tri2(l, b, r, t):
	assert r - l == t - b
	R = rec(l, r, b, t)
	ol = r - l + 1
	return (R - ol) // 2
main()


from collections import *
from bisect import *
from math import *
from heapq import *
import sys
input = sys.stdin.readline
t = 1

def ss(n):
	if n <= 0:
		return 0
	return n * (n + 1) // 2
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if a == b == c == d:
		print(1)
		continue
	r = 0
	for i in range(a, b + 1):
		req = i + b
		s1 = req - c
		req1 = i + c
		s2 = req1 - c
		if s1 >= d - c + 1:
			r += (c - b + 1) * (d - c + 1)
			continue
		if s2 > d - c + 1:
			r += (d - c + 1) * (s2 - d + c - 1)
			s2 = d - c + 1
		if s1 != s2 and s1 < s2:
			r += ss(s2) - ss(s1 - 1)
		if s1 == s2:
			r += s1
	print(r)


(A, B, C, D) = map(int, input().split())

def gauss_count(n):
	if n <= 0:
		return 0
	return n * (n + 1) // 2
count = 0
for z in range(C, D + 1):
	if A + B > z:
		count += (B - A + 1) * (C - B + 1)
	else:
		x0 = z - C + 1
		y0 = z - B + 1
		incl1 = gauss_count(B - x0 + 1)
		excl1 = gauss_count(A - x0)
		excl2 = gauss_count(B - y0)
		count += incl1 - excl1 - excl2
print(count)


def main():
	(a, b, c, d) = map(int, input().split())
	cnt = [0] * (b + c + 1)
	mid1 = min(a + c, 2 * b)
	mid2 = max(a + c, 2 * b)
	for z in range(c + 1, b + c + 1):
		if z < a + b:
			continue
		if z > 2 * c:
			continue
		if z < mid1:
			cnt[z] = z - a - b + 1
		elif z > mid2:
			cnt[z] = b + c - z + 1
		else:
			cnt[z] = mid1 - a - b + 1
	cum = [0] * (b + c + 2)
	for z in range(b + c, c, -1):
		cum[z] = cum[z + 1] + cnt[z]
	res = sum(cum[c + 1:d + 2])
	return res
print(main())


import collections as cc, math as mt
import sys
I = lambda : list(map(int, input().split()))
(a, b, c, d) = I()
N = 10 * 10 ** 5 + 100
pref = [0] * N
for i in range(a, b + 1):
	pref[i + b] += 1
	pref[i + c + 1] -= 1
for i in range(1, N):
	pref[i] += pref[i - 1]
pref = pref[::-1]
for i in range(1, N):
	pref[i] += pref[i - 1]
pref = pref[::-1]
ans = 0
for i in range(c, d + 1):
	ans += pref[i + 1]
print(ans)


(a, b, c, d) = map(int, input().split())
j = a
s = 0
while j <= b:
	l = j + b
	r = j + c
	p = l - c
	q = r - c
	if l > d:
		s += (r - l + 1) * (d - c + 1)
	elif r > d and l <= d:
		if p > 0:
			s += (r - d) * (d - c + 1) + (d - c) * (d - c + 1) // 2 - (p - 1) * p // 2
		else:
			s += (r - d) * (d - c + 1) + (d - c) * (d - c + 1) // 2
	elif r <= d:
		if p <= 0 and q <= 0:
			s += 0
		elif p <= 0 and q > 0:
			s += q * (q + 1) // 2
		elif p > 0 and q > 0:
			s += q * (q + 1) // 2 - (p - 1) * p // 2
	j += 1
print(s)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]
(A, B, C, D) = inputnums()
ans = 0
for i in range(A + B, B + C + 1):
	if i <= C:
		continue
	z = min(D, i) - C
	if i > D:
		z += 1
	minx = max(A, i - C)
	maxx = min(B, i - B)
	ans += z * (maxx - minx + 1)
print(ans)


(a, b, c, d) = map(int, input().split())

def countt(z):
	if b + c <= z:
		return 0
	if a + b > z:
		return (b - a + 1) * (c - b + 1)
	xstart = max(z - c + 1, a)
	xfin = min(b, z + 1 - b)
	ilo = (abs(xstart - xfin) + 1) * (c + 1 - (z - xstart + 1) + c + 1 - (z - xfin + 1)) // 2
	ilo += (b - xfin) * (c - b + 1)
	return ilo
wyn = 0
for z in range(c, d + 1):
	wyn += countt(z)
print(wyn)


from sys import stdin as si

def evaluate(a, b, c, d):
	count = 0
	for i in range(max(c + 1, a + b), b + c + 1):
		count += (min(d + 1, i) - c) * (min(i - b, b) - max(i - c, a) + 1)
	return count
(a, b, c, d) = map(int, si.readline().strip().split())
print(evaluate(a, b, c, d))


def cnt(x):
	return x * (x + 1) // 2
t = 1
while t > 0:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	ans = 0
	for x in range(a, b + 1):
		(l, r) = (x + b, x + c)
		ans += cnt(min(r, d) - c) - cnt(max(min(l - 1, d) - c, 0))
		ans += max(0, r - max(d, l - 1)) * (d - c + 1)
	print(ans)


(a, b, c, d) = map(int, input().split())
N = 10 ** 6 + 2
ans = [0] * N
for i in range(a, b + 1):
	ans[i + b] += 1
	ans[i + c + 1] -= 1
for i in range(1, N):
	ans[i] += ans[i - 1]
for i in range(1, N):
	ans[i] += ans[i - 1]
final = 0
for i in range(c, d + 1):
	final += ans[N - 1] - ans[i]
print(final)


(a, b, c, d) = map(int, input().split())
ans = 0
minz = c
for i in range(a, b + 1):
	lenz = d - c + 1
	ymore = minz - i
	if ymore < b - 1:
		alpha = min(b - ymore, lenz)
		ans += alpha * (c - b + 1)
		cury = c - b
		lenz = lenz - alpha
	else:
		cury = c - ymore
	if cury > lenz:
		ans += lenz * (lenz + 1) // 2 + (cury - lenz) * lenz
	else:
		ans += cury * (cury + 1) // 2
print(ans)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 1000000007
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
a = array()
low = sum(a[:2])
high = sum(a[1:3])
ans = 0
c = 1
for i in range(low, high + 1):
	if i <= a[-2]:
		continue
	here = min(i - 1, a[-1]) - a[-2] + 1
	a_low = a[0]
	a_high = min(a[1], i // 2)
	b_high = min(i - a_low, a[2])
	b_low = max(i - a_high, a[1])
	k = min(b_high - b_low + 1, a_high - a_low + 1)
	here *= k
	ans += here
print(ans)


(a, b, c, d) = map(int, input().split())
ans = (b - a + 1) * (c - b + 1) * (d - c + 1)
arr = [0] * (d + 5)
for i in range(a, b + 1):
	if i + b < len(arr):
		arr[i + b] += 1
	if i + c + 1 < len(arr):
		arr[i + c + 1] -= 1
for i in range(1, len(arr)):
	arr[i] += arr[i - 1]
for i in range(1, len(arr)):
	arr[i] += arr[i - 1]
for i in range(c, d + 1):
	ans -= arr[i]
print(ans)


import sys
import math as mt
input = sys.stdin.buffer.readline
t = 1
for __ in range(t):
	(a, b, c, d) = map(int, input().split())
	l = [0] * (c + b + 2)
	for i in range(a, b + 1):
		l[b + i] += 1
		l[c + i + 1] += -1
	for i in range(1, c + b + 2):
		l[i] += l[i - 1]
	ans = 0
	for i in range(1, c + b + 2):
		if i > c:
			if i > d:
				diff = d - c + 1
			else:
				diff = i - c
			ans += l[i] * diff
	print(ans)


(a, b, c, d) = [int(x) for x in input().split()]
ans = 0
for xpy in range(a + b, b + c + 1):
	v = min(b - a + 1, c - b + 1, min(xpy - a - b + 1, b + c - xpy + 1))
	l = c
	r = min(xpy - 1, d)
	ans += v * max(0, r - l + 1)
print(ans)


(a, b, c, d) = map(int, input().split())
maxa = c + b
mini = b + a
lene = maxa - mini + 1
ans = [0 for i in range(lene)]
c1 = b - a + 1
c2 = c - b + 1
mini = min(c1, c2)
ans[0] = 1
ans[-1] = 1
for i in range(1, len(ans) // 2):
	if i + 1 > mini:
		ans[i] = mini
		ans[-1 * (i + 1)] = mini
	else:
		ans[i] = i + 1
		ans[-1 * (i + 1)] = i + 1
if len(ans) % 2 == 1:
	ans[len(ans) // 2] = mini
total = 0
mini = a + b
for i in range(len(ans)):
	ram = i + mini
	if ram <= c:
		continue
	elif ram > d:
		total += ans[i] * (d - c + 1)
	else:
		total += ans[i] * (ram - c)
print(total)


import sys
readline = sys.stdin.readline
(A, B, C, D) = map(int, readline().split())
ans = 0
for z in range(C, D + 1):
	a = max(A, z + 1 - C)
	b = min(B, z - B)
	c = max(A, z - B + 1)
	d = B
	if a <= b:
		ans += (C - z + a + C - z + b) * (b - a + 1) // 2
	if c <= d:
		ans += (C - B + 1) * (d - c + 1)
print(ans)


from sys import stdin
input = stdin.buffer.readline
(a, b, c, d) = map(int, input().split())
(n, ans) = (1000005, 0)
pref = [0] * n
for i in range(a, b + 1):
	pref[i + b] += 1
	pref[i + c + 1] -= 1
for i in range(1, n):
	pref[i] += pref[i - 1]
for i in range(1, n):
	pref[i] += pref[i - 1]
for i in range(c, d + 1):
	ans += pref[-1] - pref[i]
print(ans)


def fun(x):
	val = x * (x + 1) // 2
	return val
(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(a, b + 1):
	rr = min(c + i - 1, d)
	s3 = rr - c + 1
	x = rr - i + 1
	if x <= b:
		s2 = c - b + 1
		ans += s2 * s3
	else:
		ans += (c - x) * s3
		s2 = x - b + 1
		if s2 >= s3:
			ans += fun(s3)
		else:
			ans += fun(s3) - fun(s3 - s2)
print(ans)


strli = input().split(' ')
A = int(strli[0])
B = int(strli[1])
C = int(strli[2])
D = int(strli[3])
count = 0
numX = B - A + 1
numY = C - B + 1
for z in range(C, D + 1, 1):
	yG = z - A
	if yG < B:
		count += numY * numX
		continue
	elif yG >= C:
		validYs = C - yG
		add1sLeft = numX + validYs - 1
		validYs = 0
		if add1sLeft > 0:
			if add1sLeft <= numY:
				count += (1 + add1sLeft) * add1sLeft / 2
			else:
				count += (1 + numY) * numY / 2
				add1sLeft -= numY
				count += numY * add1sLeft
	else:
		validYs = C - yG
		if validYs + numX - 1 <= numY:
			count += (validYs + (validYs + numX - 1)) * numX / 2
		else:
			opsToMax = numY - validYs
			count += (validYs + numY) / 2 * (opsToMax + 1)
			count += numY * (numX - 1 - opsToMax)
print(round(count))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	(a, b, c, d) = sep()
	pre = [0] * (b + c + 4)
	for i in range(a, b + 1):
		l = i + b
		r = i + c
		pre[l] += 1
		pre[r + 1] -= 1
	for i in range(len(pre)):
		pre[i] += pre[i - 1]
	ans = 0
	for i in range(a + b, b + c + 1):
		ans += pre[i] * max(0, i - c if i <= d else d - c + 1)
	print(ans)
testcase(1)


(A, B, C, D) = map(int, input().split())
X = B - A + 1
Y = C - B + 1

def cnt(c):
	if A + B > c:
		return X * Y
	if B + C <= c:
		return 0
	m = B + C - c
	if X > Y:
		if m <= Y:
			return m * (m + 1) // 2
		elif m <= X:
			return (m + m - Y + 1) * Y // 2
		else:
			n = X + Y - 1 - m
			return X * Y - n * (n + 1) // 2
	elif m <= X:
		return m * (m + 1) // 2
	elif m <= Y:
		return (m + m - X + 1) * X // 2
	else:
		n = X + Y - 1 - m
		return X * Y - n * (n + 1) // 2
ans = 0
for c in range(C, D + 1):
	ans += cnt(c)
print(ans)


(a, b, c, d) = map(int, input().split())
N = int(10 ** 6) + 2
prefix = list(bytearray(N))
for i in range(a, b + 1):
	prefix[i + b] += 1
	prefix[i + c + 1] -= 1
for i in range(1, N):
	prefix[i] += prefix[i - 1]
for i in range(1, N):
	prefix[i] += prefix[i - 1]
ans = 0
for i in range(c, d + 1):
	ans += prefix[N - 1] - prefix[i]
print(ans)


import math
import sys
from collections import defaultdict, Counter, deque, OrderedDict
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
from itertools import accumulate, groupby
import sys
import heapq
Ans = []
import sys
(a, b, c, d) = ilele()
m = 10 ** 6 + 10
l = [0] * m
tot = 0
for i in range(a, b + 1):
	l[i + b] += 1
	l[i + c + 1] += -1
for i in range(m - 1):
	l[i] += l[i - 1]
for i in range(m - 2, -1, -1):
	l[i] += l[i + 1]
for i in range(c, d + 1):
	tot += l[i + 1]
print(tot)


import sys
input = sys.stdin.readline
(a, b, c, d) = list(map(int, input().split()))
ans = 0
for i in range(a + b, b + c + 1):
	if i - a <= c:
		tempa = a
		tempb = i - a
	else:
		tempb = c
		tempa = i - c
	if i > d:
		ans += min(b - tempa + 1, tempb - b + 1) * (d - c + 1)
	elif min(b - tempa + 1, tempb - b + 1) * (i - c) >= 0:
		ans += min(b - tempa + 1, tempb - b + 1) * (i - c)
print(ans)


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
from typing import List

def solve(A, B, C, D):
	INF = D * 2 + 10
	starts = [0] * INF
	ends = [0] * INF
	for x in range(A, B + 1):
		(ys, yt) = (x + B, x + C + 1)
		starts[ys] += 1
		ends[yt] += 1
	xy = [0] * INF
	for i in range(1, INF):
		xy[i] = xy[i - 1] + starts[i] - ends[i]
	presum = [0] * INF
	for i in range(1, INF):
		presum[i] = presum[i - 1] + xy[i]
	ans = 0
	for z in range(C, D + 1):
		ans += presum[-1] - presum[z]
	return ans
(A, B, C, D) = map(int, input().split())
print(solve(A, B, C, D))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def ctd(chr):
	return ord(chr) - ord('a')
mod = 998244353
INF = float('inf')

def main():
	(a, b, c, d) = RL()
	res = 0
	for i in range(a + b, b + c + 1):
		(lb, hb) = (max(a, i - c), min(b, i - b))
		le = hb - lb + 1
		if le < 0:
			continue
		llb = min(d, i - 1)
		res += max(0, llb - c + 1) * le
	print(res)
main()


(a, b, c, d) = list(map(int, input().split()))
prefix = [0] * 1000003
for i in range(a, b + 1):
	prefix[i + b] += 1
	prefix[i + c + 1] -= 1
for k in range(1, len(prefix)):
	prefix[k] += prefix[k - 1]
for l in range(len(prefix) - 1, -1, -1):
	prefix[l - 1] += prefix[l]
ans = 0
for q in range(c, d + 1):
	ans += prefix[q + 1]
print(ans)


(A, B, C, D) = map(int, input().split())
res = 0
for i in range(max(A + B, C), C + D + 1):
	res += (i - A - B + 1 + max(0, i - B - C - 1) - max(0, i - A - C) - max(0, i - B - B)) * min(i - C, D - C + 1)
print(int(res))


import sys
pprint = lambda s: print(' '.join(map(str, s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
(a, b, c, d) = map(int, input().split())
z = d
dp = 0
for y in range(b, c + 1):
	dp += max(0, b - max(z - y + 1, a) + 1)
z += 1
ans = dp
for i in range(d - c):
	z -= 1
	dp += min(max(0, min(z - a, c) - max(b, z - b) + 1), max(0, min(z - b, b) - max(a, z - c) + 1))
	ans += dp
print(ans)


def summ(x):
	return x * (x + 1) // 2
(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(a, b + 1):
	lb = i + b
	if lb > d:
		ans += (c - b + 1) * (d - c + 1)
		continue
	x = b
	if lb < c:
		rb = 0
		x = c - i
	if i > d - c + 1:
		ans += (i - (d - c + 1)) * (d - c + 1)
	ans += summ(min(d - c + 1, c + i - c)) - summ(max(0, x + i - c - 1))
print(ans)


def func(z, a, b, c):
	if c + b <= z:
		return 0
	yMin = z - b + 1
	if yMin < b:
		yMin = b
	xMax = max(z + 1 - yMin, a)
	num = min([c - yMin + 1, xMax - a + 1])
	s = b - xMax + 1
	res = (s + s + num - 1) * num // 2
	res += max(c - yMin + 1 - (xMax - a + 1), 0) * (b - a + 1)
	return res
(a, b, c, d) = map(int, input().split())
ans = 0
for z in range(c, d + 1):
	ans += func(z, a, b, c)
print(ans)


def main():
	(a, b, c, d) = map(int, input().split())
	ans = 0
	for x in range(a, b + 1):
		k = max(c + 1 - x, b)
		p = min(d - c + 1, x)
		q = min(d - c + 1, x + k - c)
		ans += p * (p + 1) // 2 - q * (q - 1) // 2 + (c - k - p + q) * p
	print(ans)
main()


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
(a, b, c, d) = map(int, input().split())
ans = 0
for i in range(a, b + 1):
	ans += 1
	if c + (i - 1) <= d:
		ans += i - 1
	else:
		ans += d - c
	if c - (i - 1) >= b:
		ans += i - 1
	else:
		ans += c - b
	if c + (i - 2) <= d:
		calculate = (i - 2) * (i - 1) // 2
		if c - (i - 2) >= b:
			pass
		else:
			sub = b - (c - (i - 2))
			subSum = sub * (sub + 1) // 2
			calculate -= subSum
		ans += calculate
	else:
		calculate = (i - 2) * (i - 1) // 2
		if c - (i - 2) >= b:
			sub = c + (i - 2) - d
			subSum = sub * (sub + 1) // 2
			ans += calculate - subSum
		else:
			sub = c + (i - 2) - d
			subSum = sub * (sub + 1) // 2
			sub2 = b - (c - (i - 2))
			subSum2 = sub2 * (sub2 + 1) // 2
			ans += calculate - subSum - subSum2
			gar = max(sub2 - (d - c), 0)
			ans += gar * (gar + 1) // 2
print(max(ans, 1))


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt

def solve():
	(A, B, C, D) = read()
	array = [0] * (10 ** 6 + 2)
	for i in range(A, B + 1):
		array[i + B] += 1
		array[i + C + 1] -= 1
	for i in range(1, len(array)):
		array[i] += array[i - 1]
	for i in range(len(array) - 2, 0, -1):
		array[i - 1] += array[i]
	s = 0
	for i in range(C, D + 1):
		s += array[i + 1]
	print(s)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = 1
for i in range(t):
	solve()


from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
for _ in range(t):
	(a, b, c, d) = rinput()
	mx = 2 * int(1000000.0) + 1
	prefix = [0] * mx
	for i in range(a, b + 1):
		prefix[i + b] += 1
		prefix[i + c + 1] -= 1
	prefix = list(accumulate(prefix))
	for i in range(mx - 1, 0, -1):
		prefix[i - 1] += prefix[i]
	ans = 0
	for i in range(c, d + 1):
		ans += prefix[i + 1]
	print(ans)


import sys
input = sys.stdin.readline
(A, B, C, D) = map(int, input().split())
imos = [0] * (2 * D + 2)
res = 0
for x in range(A, B + 1):
	imos[x + B] += 1
	imos[x + C + 1] -= 1
for i in range(2 * D + 1):
	imos[i + 1] += imos[i]
for i in range(2 * D + 1, 0, -1):
	imos[i - 1] += imos[i]
for z in range(C + 1, D + 2):
	res += imos[z]
print(res)


(a, b, c, d) = map(int, input().split())
s = 0
mz = d - c + 1
for x in range(a, b + 1):
	ym = max(b, c - x + 1)
	ny = c - ym + 1
	nzm = min(ym + x - c, mz)
	if x > mz:
		n = mz - nzm + 1
		s += n * (2 * nzm + n - 1) // 2 + (ny - n) * mz
	else:
		s += ny * (nzm * 2 + ny - 1) // 2
print(s)


(a, b, c, d) = [int(j) for j in input().split()]
l1 = [i for i in range(a + b, b + c + 1)]
l2 = [0 for i in range(a + b, b + c + 1)]
stop1 = min(2 * b, a + c) - (a + b)
stop2 = max(2 * b, a + c) - (a + b)
n = len(l1)
l2[0] = 1
flag1 = True
flag2 = True
if stop1 == 0:
	flag1 = False
for i in range(1, n):
	if flag1 and flag2:
		l2[i] = l2[i - 1] + 1
	elif flag1 == False and flag2 == True:
		l2[i] = l2[i - 1]
	else:
		l2[i] = max(0, l2[i - 1] - 1)
	if i == stop1:
		flag1 = False
	if i == stop2:
		flag2 = False
c = [i + 1 for i in range(c, d + 1)]
ans = 0
for i in range(n - 2, -1, -1):
	l2[i] += l2[i + 1]
for i in range(len(c)):
	ind = max(c[i] - (a + b), 0)
	if ind >= n:
		continue
	ans += l2[ind]
print(ans)


def read_int():
	return int(input())

def read_ints():
	return list(map(int, input().split(' ')))
(a, b, c, d) = read_ints()
ans = 0
m = min(c - b, d - c)
n = max(c - b, d - c)
for delta in range(d - b + 1):
	right = 0
	if delta <= m:
		right = delta + 1
	elif delta <= n:
		right = m + 1
	else:
		right = d - b - delta + 1
	left = max(0, b - max(delta + 1, a) + 1)
	ans += left * right
print(ans)


(a, b, c, d) = map(int, input().split())
mn = a + b
mx = b + c
cnt = [0] * (10 ** 6 + 4)
l = 1
for i in range(mn, mx + 1):
	if l == min(c - b + 1, b - a + 1):
		break
	cnt[i] = l
	l += 1
l = 1
for i in range(mx, mn - 1, -1):
	if l == min(c - b + 1, b - a + 1):
		break
	cnt[i] = l
	l += 1
for i in range(mn, mx + 1):
	if cnt[i] == 0:
		cnt[i] = l
for i in range(len(cnt) - 2, -1, -1):
	cnt[i] += cnt[i + 1]
ans = 0
for i in range(c, d + 1):
	ans += cnt[i + 1]
print(ans)


(a, b, c, d) = map(int, input().split())
count = 0
for s in range(max(a + b, c), b + c + 1):
	m = max(min(s - b, b) - max(s - c, a) + 1, 0)
	n = max(min(s - 1, d) - c + 1, 0)
	count += m * n
print(count)


(A, B, C, D, ans) = [int(s) for s in input().split()] + [0]
for z in range(C, D + 1):
	(l, r) = (B - min(z - B + 1, B) + 1, B - max(z - C + 1, A) + 1)
	ans += (B - A + 1) * (min(A, z - B + 2) - (z - C + 1)) if z - C + 1 < A else 0
	ans += (l + r) * (r - l + 1) // 2 if l <= r else 0
print(ans)


l = list(map(int, input().split()))
lt = [0] * (l[1] + l[2] + 2)
for i in range(l[0], l[1] + 1):
	lt[i + l[1]] += 1
	lt[i + l[2] + 1] -= 1
for i in range(1, l[1] + l[2] + 2):
	lt[i] += lt[i - 1]
c = 0
for i in range(len(lt)):
	if i > l[2]:
		c += lt[i] * (min(i, l[3] + 1) - l[2])
print(c)


(a, b, c, d) = map(int, input().split())
N = 10 ** 6 + 77
arr = [0] * N
for i in range(a, b + 1):
	arr[i + b] += 1
	arr[i + c + 1] -= 1
for i in range(1, N):
	arr[i] += arr[i - 1]
for i in range(1, N):
	arr[i] += arr[i - 1]
ans = 0
for i in range(c, d + 1):
	ans += arr[-1] - arr[i]
print(ans)


N = int(1000000.0 + 7)
seg = [0 for i in range(N)]
(a, b, c, d) = map(int, input().split())
for i in range(a, b + 1):
	seg[i + c + 1] -= 1
	seg[i + b] += 1
for i in range(1, N):
	seg[i] += seg[i - 1]
for i in range(1, N):
	seg[i] += seg[i - 1]
ans = 0
for i in range(c, d + 1):
	ans += seg[N - 1] - seg[i]
print(ans)


import sys
import math
input = sys.stdin.readline
from functools import cmp_to_key

def pi():
	return int(input())

def pl():
	return int(input(), 16)

def ti():
	return list(map(int, input().split()))

def ts():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
mod = 1000000007
f = []

def fact(n, m):
	global f
	f = [1 for i in range(n + 1)]
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % m

def fast_mod_exp(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b = b >> 1
	return res

def inverseMod(n, m):
	return fast_mod_exp(n, m - 2, m)

def ncr(n, r, m):
	if r == 0:
		return 1
	return f[n] * inverseMod(f[n - r], m) % m * inverseMod(f[r], m) % m

def main():
	C()

def dfs(root, v, sub, parent):
	st = [root]
	while len(st) > 0:
		node = st.pop()
		for i in range(len(v[node])):
			if v[node][i] != node:
				st.append(v[node][i])

def X():
	try:
		t = pi()
		while t:
			t -= 1
			n = pi()
			v = [[] for i in range(n)]
			for i in range(n - 1):
				[x, y] = ti()
				v[x - 1].append(y - 1)
				v[y - 1].append(x - 1)
			m = pi()
			p = ti()
			e = [0 for i in range(n - 1)]
			sub = [0 for i in range(n)]
			dfs(0, v, sub, -1)
			for i in range(1, n):
				e[i - 1] = sub[i] * (n - sub[i])
			if len(p) < n - 1:
				while len(p) < n - 1:
					p.append(1)
			p = sorted(p)
			if len(p) > n - 1:
				x = 1
				for i in range(n - 2, len(p)):
					x = x * p[i]
				while len(p) > n - 2:
					p.pop()
				p.append(x)
			e = sorted(e)
			res = 0
			for i in range(n - 1):
				res = (res + p[i] * e[i]) % mod
			print(res)
	except:
		print(sys.exc_info())

def C():
	[a, b, c, d] = ti()
	res = 0
	left = b
	for z in range(c, d + 1):
		while left <= c and z - left + 1 > b:
			left += 1
		if left <= c:
			x = z - left + 1
			if x < a:
				x = a
			st = b - x + 1
			if x - a < c - left:
				n = x - a + 1
				en = st + (n - 1)
				res += n / 2 * (st + en)
				rem = c - left - x + a
				res += rem * (b - a + 1)
			else:
				n = c - left + 1
				en = st + (n - 1)
				res += n / 2 * (st + en)
	print(int(res))
main()


import sys
input = sys.stdin.readline
(A, B, C, D) = map(int, input().split())
ans = 0
dic2 = {z: 0 for z in range(C, min(D, B + C - 1) + 1)}
for z in range(C, min(D, B + C - 1) + 1):
	if 2 * B >= z + 1:
		if A >= z + 1 - B:
			ans += (B - A + 1) * (C - B + 1)
			dic2[z] = (B - A + 1) * (C - B + 1)
		elif A >= z + 1 - C:
			ans += (B + C - z) * (B + C + 1 - z) // 2 - (2 * B - z) * (2 * B - z - 1) // 2 - (A + C - z) * (A + C - z - 1) // 2
			dic2[z] = (B + C - z) * (B + C + 1 - z) // 2 - (2 * B - z) * (2 * B - z - 1) // 2 - (A + C - z) * (A + C - z - 1) // 2
		else:
			ans += (B + C - z) * (B + C + 1 - z) // 2 - (2 * B - z) * (2 * B - z - 1) // 2
			dic2[z] = (B + C - z) * (B + C + 1 - z) // 2 - (2 * B - z) * (2 * B - z - 1) // 2
	elif A >= z + 1 - C:
		ans += (B + C - z) * (B + C - z + 1) // 2 - (A + C - z) * (A + C - z - 1) // 2
		dic2[z] = (B + C - z) * (B + C - z + 1) // 2 - (A + C - z) * (A + C - z - 1) // 2
	else:
		ans += (B + C - z) * (B + C - z + 1) // 2
		dic2[z] = (B + C - z) * (B + C - z + 1) // 2
print(ans)


import sys
input = sys.stdin.readline
from collections import *
(A, B, C, D) = map(int, input().split())
cnt = [0] * (B + C + 2)
for x in range(A, B + 1):
	cnt[x + B] += 1
	cnt[x + C + 1] -= 1
for i in range(1, B + C + 2):
	cnt[i] += cnt[i - 1]
ans = 0
for i in range(A + B, B + C + 1):
	up = min(D, i - 1)
	ans += cnt[i] * max(0, up - C + 1)
print(ans)


def check(a, b, c, d):
	ans = 0
	for z in range(c, d + 1):
		z_ans = 0
		for x in range(a, b + 1):
			for y in range(b, c + 1):
				if x + y > z:
					z_ans += 1
		print(z, z_ans)
		ans += z_ans
	print(ans)
(a, b, c, d) = map(int, input().split())
ans = 0
h = c - b + 1
w = b - a + 1
for z in range(c, d + 1):
	if b + c <= z:
		break
	if a + b > z:
		ans += h * w
		continue
	e = z + 1 - b
	f = z + 1 - a
	if e < b:
		if f < c:
			ans += h * w - (e - a) * (f - b + 1) // 2
		else:
			ans += (b - e + 1) * h + h * (h - 1) // 2
	elif f < c:
		ans += (c - f + 1) * w + w * (w - 1) // 2
	else:
		ans += (h + b - e) * (h + b - e + 1) // 2
print(ans)


import io
import os
from collections import Counter
DEBUG = False

def sumUpToN(n):
	return n * (n + 1) // 2

def solveFormula(A, B, C, D):
	total = 0
	for z in range(C, D + 1):
		if DEBUG:
			brute = 0
			for x in range(A, B + 1):
				for y in range(B, C + 1):
					assert x + z > y
					assert y + z > x
					if x + y > z:
						brute += 1
			brute2 = 0
			for x in range(A, B + 1):
				yMin = max(B, z - x + 1)
				yMax = C
				if yMin <= yMax:
					brute2 += yMax - yMin + 1
			assert brute == brute2
			brute3 = 0
			for x in range(max(A, z - C + 1), B + 1):
				yMin = max(B, z - x + 1)
				yMax = C
				assert yMin <= yMax
				numY = yMax - yMin + 1
				brute3 += numY
			assert brute == brute3
		brute4 = 0
		xMin = max(A, z - C + 1)
		xMax = B
		if xMin <= xMax:
			yMin = max(B, z - xMin + 1)
			yMax = C
			assert yMin <= yMax
			numYStart = yMax - yMin + 1
			yMin = max(B, z - xMax + 1)
			yMax = C
			assert yMin <= yMax
			numYEnd = yMax - yMin + 1
			if numYEnd == yMax - B + 1:
				numYEnd -= 1
			if numYStart <= numYEnd:
				brute4 += sumUpToN(numYEnd) - sumUpToN(numYStart - 1)
			xMid = max(xMin, z - B + 1)
			if xMin <= xMid <= xMax:
				brute4 += (xMax - xMid + 1) * (C - B + 1)
		if DEBUG:
			assert brute == brute4
		total += brute4
	return total

def solve(A, B, C, D):
	M = max(B + C + 1, D + 1) + 1 + 10
	intervalDelta = [0 for i in range(M)]
	for x in range(A, B + 1):
		minY = B
		maxY = C
		intervalDelta[x + minY] += 1
		intervalDelta[x + maxY + 1] += -1
	countXPlusY = [0] * M
	countXPlusY[0] = intervalDelta[0]
	for i in range(1, M):
		countXPlusY[i] = countXPlusY[i - 1] + intervalDelta[i]
	if DEBUG:
		counts = Counter()
		for x in range(A, B + 1):
			for y in range(B, C + 1):
				counts[x + y] += 1
		for (k, v) in counts.items():
			assert countXPlusY[k] == v
		assert sum(countXPlusY) == sum(counts.values())
	xPlusYGreaterThanEqualTo = [0] * M
	xPlusYGreaterThanEqualTo[M - 1] = countXPlusY[M - 1]
	for i in range(M - 2, -1, -1):
		xPlusYGreaterThanEqualTo[i] = xPlusYGreaterThanEqualTo[i + 1] + countXPlusY[i]
	ans = 0
	for z in range(C, D + 1):
		ans += xPlusYGreaterThanEqualTo[z + 1]
	return ans
if DEBUG:
	M = 10
	for A in range(1, M + 1):
		for B in range(A, M + 1):
			for C in range(B, M + 1):
				for D in range(C, M + 1):
					ans1 = solve(A, B, C, D)
					ans2 = solveFormula(A, B, C, D)
					assert ans1 == ans2
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(A, B, C, D) = [int(x) for x in input().split()]
ans = solve(A, B, C, D)
print(ans)


(a, b, c, d) = map(int, input().split())
Min1 = max(c + 1, a + b)
cnt = 0
for i in range(Min1, b + c + 1):
	cntZ = min(d, i - 1) - c + 1
	cntXY = min(b, i - b) - max(a, i - c) + 1
	cnt = cnt + cntZ * cntXY
print(cnt)


def readStr():
	return input()

def readInts():
	return list(map(int, readStr().split(' ')))

def s(a, b, c, z):
	s = 0
	if b + c - z > 0:
		s += (b + c - z) * (b + c - z + 1) // 2
	if a + c - z > 1:
		s -= (a + c - z - 1) * (a + c - z) // 2
	if 2 * b - z > 1:
		s -= (2 * b - z - 1) * (2 * b - z) // 2
	if a + b - z > 2:
		s += (a + b - z - 2) * (a + b - z - 1) // 2
	return s

def main(a, b, c, d):
	v = 0
	for z in range(c, d + 1):
		v += s(a, b, c, z)
	return v
(a, b, c, d) = readInts()
x = main(a, b, c, d)
print(x)


import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
(a, b, c, d) = list(map(int, stdin.readline().split()))
ans = [0] * (b + c + 5)
for i in range(b - a + 1):
	ans[a + b + i] += 1
	ans[a + b + i + c - b + 1] -= 1
for i in range(1, b + c + 5):
	ans[i] = ans[i] + ans[i - 1]
fin = 0
for i in range(c, b + c + 1, 1):
	fin += ans[i] * (min(i, d + 1) - c)
print(fin)


(a, b, c, d) = map(int, input().split())
tri2 = lambda x: x * (x + 1) // 2
tri = lambda x: tri2(max(x, 0))
total = 0
for x in range(a, b + 1):
	total += tri(x) - tri(c - (d - x) - 1) - tri(b - (c - x) - 1) + tri(b - (d - x) - 2)
print(total)


import sys

def MI():
	return map(int, sys.stdin.readline().split())

def main():
	(a, b, c, d) = MI()
	cnt = []
	for z in range(c + 1, c + b + 1):
		b1 = z - b
		c1 = z - a
		l = max(b, b1)
		r = min(c, c1)
		cur = max(r - l + 1, 0)
		cnt.append(cur)
	m = len(cnt)
	for i in range(m - 2, -1, -1):
		cnt[i] += cnt[i + 1]
	print(sum(cnt[:d - c + 1]))
main()


def ip():
	return int(input())

def Ip():
	return map(int, input().split())
N = 500001
(a, b, c, d) = Ip()
l = [0] * (2 * N)
k = [0] * (2 * N)
for i in range(a, b + 1):
	l[i + b] += 1
	l[i + c + 1] -= 1
for i in range(1, 2 * N):
	k[i] = l[i] + k[i - 1]
for i in range(len(k) - 2, -1, -1):
	k[i] += k[i + 1]
ans = 0
for i in range(c, d + 1):
	ans += k[i + 1]
print(ans)


def rn():
	a = int(input())
	return a

def rl():
	a = list(map(int, input().split()))
	return a
[a, b, c, d] = rl()
C = b - a + 1
R = c - b + 1
Per = min(R, C)
mul = [Per for i in range(R + C)]
for i in range(1, Per):
	mul[i] = i
	mul[-i] = i
curr = a + b
mul2 = [0 for i in range(R + C)]
ans = 0
for i in range(1, R + C):
	mul2[i] = max(0, min(curr - 1, d) - c + 1)
	ans += mul[i] * mul2[i]
	curr += 1
print(ans)


import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def solve():
	max_counts = 0
	(A, B, C, D) = getInts()
	N = 10 ** 6 + 100
	counts = [0] * N
	for x in range(A, B + 1):
		counts[x + B] += 1
		counts[x + C + 1] -= 1
		max_counts = max(x + B, x + C + 1, max_counts)
	for i in range(1, N):
		counts[i] += counts[i - 1]
	for i in range(1, N):
		counts[i] += counts[i - 1]
	ans = 0
	for z in range(C, D + 1):
		ans += counts[N - 1] - counts[z]
	return ans
print(solve())


def solve(x, a, b, c, d):
	low = b
	high = c
	yleft = -1
	while low <= high:
		mid = (low + high) // 2
		if x + mid > c:
			yleft = mid
			high = mid - 1
		else:
			low = mid + 1
	if yleft < b or yleft > c:
		return 0
	zleft = min(x + yleft - 1, d)
	if zleft < c or zleft > d:
		return 0
	a0 = zleft - c + 1
	ydiff = c - yleft + 1
	zdiff = d - zleft + 1
	if ydiff <= zdiff:
		ans = ydiff * (2 * a0 + ydiff - 1) // 2
	else:
		ans = zdiff * (2 * a0 + zdiff - 1) // 2
		ydiff -= zdiff
		ans += ydiff * (d - c + 1)
	return ans

def main():
	(a, b, c, d) = map(int, input().split())
	ans = 0
	for x in range(a, b + 1):
		ans += solve(x, a, b, c, d)
	print(ans)
main()


def count_triangles(a, b, c, d):
	bc = c - b + 1
	dc = d - c + 1
	mx = min(bc, dc)
	l = -1
	r = d - b + 1
	ttl = 0
	cnt = [0 for i in range(d + 1)]
	while l < r:
		l += 1
		r -= 1
		ttl += 1
		cnt[l] = min(ttl, mx)
		cnt[r] = cnt[l]
	sum = [0 for i in range(d + 1)]
	sum[0] = cnt[0]
	for i in range(1, d + 1):
		sum[i] = sum[i - 1] + cnt[i]
	res = 0
	for i in range(a, b + 1):
		res += sum[i - 1]
	return res

def count_triangles2(a, b, c, d):
	res = 0
	for i in range(c, d + 1):
		if a + b > i:
			res += (b - a + 1) * (c - b + 1)
			continue
		if b + c < i:
			break
		l = a
		h = c
		comb = 0
		while l <= b <= h:
			if l + h > i:
				comb += b - l + 1
				h -= 1
			else:
				l += 1
		res += comb
	return res
abcd = input().split(' ')
a = int(abcd[0])
b = int(abcd[1])
c = int(abcd[2])
d = int(abcd[3])
res = count_triangles(a, b, c, d)
print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
for _ in range(1):
	(a, b, c, d) = map(int, input().split())
	li = [0] * (b + c + 10)
	for i in range(a, b + 1):
		li[i + b] += 1
		li[i + c + 1] -= 1
	ans = 0
	for i in range(a + b, b + c + 2):
		li[i] += li[i - 1]
	for i in range(a + b, b + c + 1):
		li[i] += li[i - 1]
	for i in range(c, d + 1):
		if i < b + c:
			ans += li[b + c] - li[i]
	print(ans)


def num_x(A, B, C, D, t):
	low = max(C - t, A)
	high = min(B, D - t)
	return max(0, high - low + 1)

def ans(A, B, C, D):
	ret = 0
	for t in range(0, D + 1):
		nx = num_x(A, B, C, D, t)
		f = C - max(B, t + 1) + 1
		if nx > 0 and f > 0:
			ret += nx * f
	return ret
(A, B, C, D) = input().split(' ')
A = int(A)
B = int(B)
C = int(C)
D = int(D)
print(ans(A, B, C, D))


import os
import sys
from io import BytesIO, IOBase

def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))

def main():

	def fn(n):
		return n * (n + 1) // 2
	(a, b, c, d) = lin()
	ans = 0
	sm = c + b
	(a1, b1) = (b - a + 1, c - b + 1)
	for i in range(c, d + 1):
		df = sm - i
		if df > 0:
			k = df
			k1 = k - min(k, a1)
			k2 = k - k1
			df1 = k - b1
			if df1 > 0:
				k = k - df1
				ans += min(df1, k2) * b1
			if k > k1:
				ans += fn(k) - fn(k1)
		else:
			break
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(A, B, C, D) = list(map(int, input().strip().split()))
triangles = 0
for x in range(A, B + 1):
	y_min = C + 1 - x
	y_for_z_max = D + 1 - x
	if y_min < B and y_for_z_max < B:
		triangles += (D - C + 1) * (C - B + 1)
	elif y_min < B and B <= y_for_z_max <= C:
		triangles += (C - y_for_z_max) * (D - C + 1) + (y_for_z_max - B + 1) * ((x + B - C + (D - C + 1)) / 2)
	elif y_min < B and y_for_z_max > C:
		triangles += (x + B - C + x) / 2 * (C - B + 1)
	elif B <= y_min <= C and B <= y_for_z_max <= C:
		triangles += (C - y_for_z_max) * (D - C + 1) + (y_for_z_max - y_min + 1) * ((1 + (D - C + 1)) / 2)
	elif B <= y_min <= C and y_for_z_max > C:
		triangles += (x + 1) / 2 * (C - y_min + 1)
	else:
		print('ERROR')
print(int(triangles))


import sys
(a, b, c, d) = map(int, sys.stdin.readline().split())
n = b + c + d
li = [0] * n
for i in range(a, b + 1):
	li[b + i] += 1
	li[c + i + 1] -= 1
for i in range(1, n):
	li[i] += li[i - 1]
for i in range(1, n):
	li[i] += li[i - 1]
ans = 0
for z in range(c, d + 1):
	ans += li[n - 1] - li[z]
print(ans)


import sys
input = lambda : sys.stdin.readline().strip()
(a, b, c, d) = map(int, input().split())
dp = [0] * (11 * 10 ** 5)
for x in range(a, b + 1):
	dp[x + b] += 1
	dp[x + c + 1] -= 1
for i in range(1, 11 * 10 ** 5):
	dp[i] += dp[i - 1]
for i in range(11 * 10 ** 5 - 2, 0, -1):
	dp[i] += dp[i + 1]
ans = 0
for z in range(c, d + 1):
	ans += dp[z + 1]
print(ans)


(a, b, c, d) = [int(x) for x in input().split()]
xymin = a + b
xymax = b + c
cntsPeak = min(b - a, c - b) + 1
xytotal = list(range(xymin, xymax + 1))
cnts = list(range(len(xytotal), 0, -1))
for i in range(len(xytotal)):
	cnts[i] = min(i + 1, cntsPeak)
j = 1
for i in range(len(cnts) - 1, -1, -1):
	if j == cntsPeak:
		break
	cnts[i] = j
	j += 1
ans = 0
for i in range(len(xytotal)):
	ans += cnts[i] * max(0, min(xytotal[i] - 1, d) - c + 1)
print(ans)


(A, B, C, D) = map(int, input().split())
ans = 0
for i in range(A, B + 1):
	a = i - C + max(C - i + 1, B)
	d = 1
	n = max(min(D + 1 - i + 1, C + 1) - max(C - i + 1, B), 0)
	ans += a * n + n * (n - 1) * d // 2
	ans += (D + 1 - C) * max(0, C + 1 - max(min(D + 1 - i + 1, C + 1), B))
print(ans)


import sys
(*data,) = sys.stdin.read().split('\n')[::-1]

def input():
	return data.pop()

def fprint(*args, **kwargs):
	print(*args, **kwargs, flush=True)

def eprint(*args, **kwargs):
	print(*args, **kwargs, file=sys.stderr)

def ari(start, end, n):
	return (start + end) * n // 2
(a, b, c, d) = map(int, input().split())
ans = 0
for x in range(a, b + 1):
	mn_y = max(c - x + 1, b)
	mx_y = c
	start_d_num = min(mn_y + x, d + 1) - c
	end_d_num = min(mx_y + x, d + 1) - c
	n = end_d_num - start_d_num + 1
	ans += ari(start_d_num, start_d_num + n - 1, n)
	mn_y += n
	ans += (mx_y + 1 - mn_y) * (d + 1 - c)
print(ans)


array = list(map(int, input().split()))
a = array[0]
b = array[1]
c = array[2]
d = array[3]
(x, y) = (a + b, b + c)
(m, n) = (b + b, a + c)
if m > n:
	t = n
	n = m
	m = t
add = 0
jod = 0
flag = 0
i = x
while i <= y:
	if flag == 0:
		jod += 1
	if i >= m and i <= n:
		flag = 1
	elif flag == 1:
		jod -= 1
	if i > d:
		add += (d - c + 1) * jod
	elif i <= d and i > c:
		add += (i - c) * jod
	i += 1
print(add)


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')

def solve(a, b, c, d):
	result = 0
	p1 = b
	p2 = c
	while p2 - p1 + 1 > b:
		p1 += 1
	while p1 <= c and p2 <= d:
		x = p2 - p1 + 1
		if x <= a:
			result += (b - a + 1) * (c - p1 + 1)
		elif x <= b:
			n = b - x + 1
			m = n + x - a
			m = min(m, n + c - p1)
			result += m * (m + 1) // 2 - n * (n - 1) // 2 + m * max(c - p1 - m + n, 0)
		else:
			break
		p2 += 1
		while p2 - p1 + 1 > b:
			p1 += 1
	return result
(a, b, c, d) = arr_in()
out(solve(a, b, c, d))


N = int(1000000.0) + 77
(a, b, c, d) = map(int, input().split())
l = [0] * N
for i in range(a, b + 1):
	l[i + b] += 1
	l[i + c + 1] -= 1
for i in range(a, b + c + 2):
	l[i] = l[i] + l[i - 1]
for i in range(a, d + c + 2):
	l[i] = l[i] + l[i - 1]
ans = 0
for i in range(c, d + 1):
	z = l[d + c + 1] - l[i]
	ans += z
print(ans)


def int_array():
	return list(map(int, input().strip().split()))

def total(start, times):
	an = start + times - 1
	summ = times * (start + an) // 2
	return summ
(a, b, c, d) = int_array()
ans = 0
for i in range(c, d + 1):
	b_first = i + 1 - b
	b_last = i + 1 - a
	if b_first > c:
		continue
	if b_last <= c:
		if b_last < b:
			b_range = c - b + 1
			a_range = b - a + 1
			ans += a_range * b_range
		else:
			extra = c - b_last
			b_range = b_last - b_first + 1
			ans += b_range * (b_range + 1) // 2
			an = 1 + (b_range - 1)
			ans += extra * an
			if b_first < b:
				extra = b - b_first
				ans -= extra * (extra + 1) // 2
	else:
		b_range = min(b_last, c) - b_first + 1
		ans += b_range * (b_range + 1) // 2
		if b_first < b:
			extra = b - b_first
			ans -= extra * (extra + 1) // 2
print(ans)


(a, b, c, d) = map(int, input().split())
N = c + d + b
ans = 0
s = [0 for i in range(N)]
for i in range(a, b + 1):
	s[b + i] += 1
	s[c + i + 1] -= 1
for i in range(1, N):
	s[i] += s[i - 1]
for i in range(1, N):
	s[i] += s[i - 1]
for i in range(c, d + 1):
	ans += s[N - 1] - s[i]
print(ans)


l = input().split()
A = int(l[0])
B = int(l[1])
C = int(l[2])
D = int(l[3])
num = 0
for x in range(A, B + 1):
	left = max(C - x, B)
	right = min(C, D + 1 - x)
	if right < left:
		rem = C - left + 1
		num += rem * (D - C + 1)
	else:
		rem = C - right
		num += rem * (D - C + 1)
		curr = max(0, x + left - C)
		num += (right - left + 1) * (right - left) // 2
		num += (right - left + 1) * curr
print(num)


(a, b, c, d) = map(int, input().split())

def an(l, r):
	return (r - l) * (r - l + 1) // 2 + (r - l + 1) * l
ans = 0
for x in range(a, b + 1):
	l = b + x - 1
	r = c + x - 1
	if l > d:
		ans += (d - c + 1) * (c - b + 1)
	elif l >= c and r <= d:
		ans += an(l - c + 1, r - c + 1)
	elif l >= c and r > d:
		ans += an(l - c + 1, d - c + 1) + (d - c + 1) * (r - d)
	elif l < c and r <= d:
		ans += an(c - c + 1, r - c + 1)
	elif l < c and r > d:
		ans += an(c - c + 1, d - c + 1) + (d - c + 1) * (r - d)
print(ans)


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
(a, b, c, d) = mapi()
res = 0
for i in range(a, b + 1):
	if i + b > d:
		res += (c - b + 1) * (d - c + 1)
	else:
		res += max(0, (d - c + 1) * (i + c - d))
		x = max(0, i + b - c)
		y = min(i, d - c)
		res += (x + y) * (y - x + 1) // 2
print(res)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
import math as mt
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
mod = int(1000000000.0) + 7

def power(k, n):
	if n == 0:
		return 1
	if n % 2:
		return power(k, n - 1) * k % mod
	t = power(k, n // 2)
	return t * t % mod

def totalPrimeFactors(n):
	count = 0
	if n % 2 == 0:
		count += 1
		while n % 2 == 0:
			n //= 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			count += 1
			while n % i == 0:
				n //= i
		i += 2
	if n > 2:
		count += 1
	return count

def main():
	(a, b, c, d) = map(int, input().split())
	pre = [0 for i in range(c + b + 10)]
	for i in range(a, b + 1):
		pre[b + i] += 1
		pre[c + i + 1] -= 1
	curr = 0
	ans = 0
	for i in range(len(pre)):
		curr += pre[i]
		if i > c:
			ans += curr * (min(i - 1, d) - c + 1)
	print(ans)
	return
main()