using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Globalization;
class
GFG {
public
static
void
Sort<T>(T[][] data,
int
col)
{
Comparer<T> comparer = Comparer<T>.Default;
Array.Sort<T[]>(data, (x,y) => comparer.Compare(x[col],y[col]));
}
public
static
int
sumInRange(
int
L,
int
R)
{
int
Sum = ((R - L + 1) / 2) * (2 * L + (R - L));
return
Sum;
}
public
static
int
calcSum(
int
[][] data,
int
n)
{
Sort<
int
>(data, 0);
int
i = 0;
int
idx = 0;
for
(i = 1; i < n; i++) {
if
((data[idx][1] >= data[i][0]) || (data[i][0] == data[idx][1] + 1)) {
data[idx][1] = Math.Max(data[idx][1], data[i][1]);
}
else
{
idx++;
data[idx][1] = data[i][1];
data[idx][0] = data[i][0];
}
}
int
Sum = 0;
for
(i = 0; i <= idx; i++) {
Sum += sumInRange(data[i][0], data[i][1]);
}
return
Sum;
}
static
void
Main() {
int
[][] vec =
new
int
[][] {
new
int
[] {-12, 15},
new
int
[] {3, 9 },
new
int
[] {-5, -2 },
new
int
[] {20, 25},
new
int
[] {16, 20}
};
Console.WriteLine(calcSum(vec, vec.Length));
}
}