puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 063

C++ solution:
#include <iostream>

#include <vector>

#include <map>

#include <algorithm>

using namespace std;



int H, W;

int P[19][10009];



int maximum_same(vector<int> R) {

	map<int, int> Map; int ret = 0;

	for (int i = 0; i < R.size(); i++) {

		Map[R[i]] += 1;

		ret = max(ret, Map[R[i]]);

	}

	return ret;

}



int main() {

	// Step #1. “ü—Í

	cin >> H >> W;

	for (int i = 0; i < H; i++) {

		for (int j = 0; j < W; j++) cin >> P[i][j];

	}



	// Step #2. ‘S’T�õ

	int Answer = 0;

	for (int i = 1; i < (1 << H); i++) {

		vector<int> R;

		for (int j = 0; j < W; j++) {

			int idx = -1; bool flag = false;

			for (int k = 0; k < H; k++) {

				if ((i & (1 << k)) == 0) continue;

				if (idx == -1) idx = P[k][j];

				else if (idx != P[k][j]) flag = true;

			}

			if (flag == false) R.push_back(idx);

		}



		int cntH = 0, cntW = maximum_same(R);

		for (int j = 0; j < H; j++) {

			if ((i & (1 << j)) != 0) cntH += 1;

		}

		Answer = max(Answer, cntH * cntW);

	}

	cout << Answer << endl;

	return 0;

}





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var p: [][]int :: #[h, w]int
	for y(0, h - 1)
		for x(0, w - 1)
			do p[y][x] :: cui@inputInt()
		end for
	end for
	
	var ans: int :: 0
	for id(0, 2 ^ h - 1)
		var i: int :: id
		var f: []bool :: #[h]bool
		for j(0, h - 1)
			do f[j] :: i % 2 = 1
			do i :/ 2
		end for
		
		var map: dict<int, int> :: #dict<int, int>
		for x(0, w - 1)
			var prev: int :: -1
			var cnt: int :: 0
			for y(0, h - 1)
				if(f[y])
					if(prev <> -1 & prev <> p[y][x])
						skip x
					end if
					do prev :: p[y][x]
					do cnt :+ 1
				end if
			end for
			var val: int :: map.get(prev, &) + cnt
			do map.add(prev, val)
			do ans :: [ans, val].max()
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 063

C++ solution:

Crystal solution: h, w = read_line.split.map(&.to_i)
a = (1..h).map { read_line.split.map(&.to_i) }
puts (1...1 << h).max_of { |bit|
  ind = (0...h).select { |i| bit.bit(i) == 1 }
  ((0...w).select { |j|
    ind.all? { |i| a[i][j] == a[ind[0]][j] }
  }.map { |j| a[ind[0]][j] }.tally.max_of?(&.[1]) || 0) * ind.size
}


Python solution: from itertools import product
from collections import Counter

def calc(A: list) -> int:
    "良いグリッドを求める"

    # bitの立っている行数を求める
    colum = 0
    for i in range(len(A)):
        if A[i] == 1:
            colum += 1
     
    cnt = Counter()
    for i in range(w):
        num = [] 
        for j in range(h):
            if A[j] == 1:
                num.append(p[j][i])
        # 列に入っている数字が全て同じである時
        if len(set(num)) == 1:
            cnt[num[0]] += 1
   
    return max(cnt.values()) * colum if len(cnt) > 0 else 0

h, w = map(int,input().split())
p = [list(map(int,input().split())) for _ in range(h)]

ans = 0
for bit in product([0, 1], repeat = h):
    ans = max(ans, calc(bit))

print(ans)
AtCoder problem 063

C++ solution:

Crystal solution: h, w = read_line.split.map(&.to_i)
a = (1..h).map { read_line.split.map(&.to_i) }
puts (1...1 << h).max_of { |bit|
  ind = (0...h).select { |i| bit.bit(i) == 1 }
  ((0...w).select { |j|
    ind.all? { |i| a[i][j] == a[ind[0]][j] }
  }.map { |j| a[ind[0]][j] }.tally.max_of?(&.[1]) || 0) * ind.size
}





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var p: [][]int :: #[h, w]int
	for y(0, h - 1)
		for x(0, w - 1)
			do p[y][x] :: cui@inputInt()
		end for
	end for
	
	var ans: int :: 0
	for id(0, 2 ^ h - 1)
		var i: int :: id
		var f: []bool :: #[h]bool
		for j(0, h - 1)
			do f[j] :: i % 2 = 1
			do i :/ 2
		end for
		
		var map: dict<int, int> :: #dict<int, int>
		for x(0, w - 1)
			var prev: int :: -1
			var cnt: int :: 0
			for y(0, h - 1)
				if(f[y])
					if(prev <> -1 & prev <> p[y][x])
						skip x
					end if
					do prev :: p[y][x]
					do cnt :+ 1
				end if
			end for
			var val: int :: map.get(prev, &) + cnt
			do map.add(prev, val)
			do ans :: [ans, val].max()
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 063

C++ solution:


Python solution: from itertools import product
from collections import Counter

def calc(A: list) -> int:
    "良いグリッドを求める"

    # bitの立っている行数を求める
    colum = 0
    for i in range(len(A)):
        if A[i] == 1:
            colum += 1
     
    cnt = Counter()
    for i in range(w):
        num = [] 
        for j in range(h):
            if A[j] == 1:
                num.append(p[j][i])
        # 列に入っている数字が全て同じである時
        if len(set(num)) == 1:
            cnt[num[0]] += 1
   
    return max(cnt.values()) * colum if len(cnt) > 0 else 0

h, w = map(int,input().split())
p = [list(map(int,input().split())) for _ in range(h)]

ans = 0
for bit in product([0, 1], repeat = h):
    ans = max(ans, calc(bit))

print(ans)



Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var p: [][]int :: #[h, w]int
	for y(0, h - 1)
		for x(0, w - 1)
			do p[y][x] :: cui@inputInt()
		end for
	end for
	
	var ans: int :: 0
	for id(0, 2 ^ h - 1)
		var i: int :: id
		var f: []bool :: #[h]bool
		for j(0, h - 1)
			do f[j] :: i % 2 = 1
			do i :/ 2
		end for
		
		var map: dict<int, int> :: #dict<int, int>
		for x(0, w - 1)
			var prev: int :: -1
			var cnt: int :: 0
			for y(0, h - 1)
				if(f[y])
					if(prev <> -1 & prev <> p[y][x])
						skip x
					end if
					do prev :: p[y][x]
					do cnt :+ 1
				end if
			end for
			var val: int :: map.get(prev, &) + cnt
			do map.add(prev, val)
			do ans :: [ans, val].max()
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 064

C++ solution:
#include <iostream>

using namespace std;



long long N, A[1 << 18], B[1 << 18];

long long Q, L[1 << 18], R[1 << 18], V[1 << 18];



int main() {

	// Step #1. 入力

	cin >> N >> Q;

	for (int i = 1; i <= N; i++) cin >> A[i];

	for (int i = 1; i <= Q; i++) cin >> L[i] >> R[i] >> V[i];



	// Step #2. 最初の答え

	long long Answer = 0;

	for (int i = 1; i <= N - 1; i++) {

		B[i] = A[i + 1] - A[i];

		Answer += abs(B[i]);

	}

	

	// Step #3. シミュレーション

	for (int i = 1; i <= Q; i++) {

		long long mae = abs(B[L[i] - 1]) + abs(B[R[i]]);

		if (L[i] >= 2) B[L[i] - 1] += V[i];

		if (R[i] <= N - 1) B[R[i]] -= V[i];

		long long ato = abs(B[L[i] - 1]) + abs(B[R[i]]);

		Answer += (ato - mae);

		cout << Answer << endl;

	}

	return 0;

}

Crystal solution: n, q = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i64)
d = a.each_cons(2).map { |(x, y)| x - y }.to_a
ans = d.sum(&.abs)
q.times do
  l, r, v = read_line.split.map(&.to_i)
  if l != 1
    ans -= d[l - 2].abs
    d[l - 1] -= v
    ans += d[l - 2].abs
  end
  if r != n
    ans -= d[r - 1].abs
    d[r - 1] += v
    ans += d[r - 1].abs
  end
  puts ans
end
AtCoder problem 064

C++ solution:
#include <iostream>

using namespace std;



long long N, A[1 << 18], B[1 << 18];

long long Q, L[1 << 18], R[1 << 18], V[1 << 18];



int main() {

	// Step #1. 入力

	cin >> N >> Q;

	for (int i = 1; i <= N; i++) cin >> A[i];

	for (int i = 1; i <= Q; i++) cin >> L[i] >> R[i] >> V[i];



	// Step #2. 最初の答え

	long long Answer = 0;

	for (int i = 1; i <= N - 1; i++) {

		B[i] = A[i + 1] - A[i];

		Answer += abs(B[i]);

	}

	

	// Step #3. シミュレーション

	for (int i = 1; i <= Q; i++) {

		long long mae = abs(B[L[i] - 1]) + abs(B[R[i]]);

		if (L[i] >= 2) B[L[i] - 1] += V[i];

		if (R[i] <= N - 1) B[R[i]] -= V[i];

		long long ato = abs(B[L[i] - 1]) + abs(B[R[i]]);

		Answer += (ato - mae);

		cout << Answer << endl;

	}

	return 0;

}






Ruby solution: class OnlineSum
    def initialize(n)
        @n = 2 ** Math.log2(n).ceil
        @data = [0] * (@n + 1)
    end

    def add(i, val)
        while i <= @n
            @data[i] += val
            i += i & -i
        end
    end

    def sum(i)
        acc = 0
        while i > 0
            acc += @data[i]
            i -= i & -i
        end
        acc
    end
end

def main
    n, q = readline.split.map!(&:to_i)

    a = ['0']
    a.concat(readline.split)
    a.map!(&:to_i)

    ans = (2..n).inject(0) do |sum, i|
        sum + (a[i-1] - a[i]).abs
    end

    os = OnlineSum.new(n)
    q.times do
        l, r, v = readline.split.map(&:to_i)

        if l > 1 then
            lo = a[l - 1] + os.sum(l - 1)
            hi = a[l - 0] + os.sum(l - 0)
            ans -= (lo - hi).abs
        end
        if r < n then
            lo = a[r + 0] + os.sum(r + 0)
            hi = a[r + 1] + os.sum(r + 1)
            ans -= (lo - hi).abs
        end

        os.add(l, v)
        os.add(r + 1, -v) if r < n

        if l > 1 then
            lo = a[l - 1] + os.sum(l - 1)
            hi = a[l - 0] + os.sum(l - 0)
            ans += (lo - hi).abs
        end
        if r < n then
            lo = a[r + 0] + os.sum(r + 0)
            hi = a[r + 1] + os.sum(r + 1)
            ans += (lo - hi).abs
        end

        puts ans
    end
end

main if __FILE__ == $0
AtCoder problem 064

C++ solution:
#include <iostream>

using namespace std;



long long N, A[1 << 18], B[1 << 18];

long long Q, L[1 << 18], R[1 << 18], V[1 << 18];



int main() {

	// Step #1. 入力

	cin >> N >> Q;

	for (int i = 1; i <= N; i++) cin >> A[i];

	for (int i = 1; i <= Q; i++) cin >> L[i] >> R[i] >> V[i];



	// Step #2. 最初の答え

	long long Answer = 0;

	for (int i = 1; i <= N - 1; i++) {

		B[i] = A[i + 1] - A[i];

		Answer += abs(B[i]);

	}

	

	// Step #3. シミュレーション

	for (int i = 1; i <= Q; i++) {

		long long mae = abs(B[L[i] - 1]) + abs(B[R[i]]);

		if (L[i] >= 2) B[L[i] - 1] += V[i];

		if (R[i] <= N - 1) B[R[i]] -= V[i];

		long long ato = abs(B[L[i] - 1]) + abs(B[R[i]]);

		Answer += (ato - mae);

		cout << Answer << endl;

	}

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var ans: int :: 0
	var diff: []int :: #[n - 1]int
	for i(0, n - 2)
		do diff[i] :: a[i + 1] - a[i]
		do ans :+ diff[i].abs()
	end for
	
	for(1, q)
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt() - 1
		var v: int :: cui@inputInt()
		if(l <> 0)
			do ans :- diff[l - 1].abs()
			do diff[l - 1] :+ v
			do ans :+ diff[l - 1].abs()
		end if
		if(r <> n - 1)
			do ans :- diff[r].abs()
			do diff[r] :- v
			do ans :+ diff[r].abs()
		end if
		do cui@print("\{ans}\n")
	end for
end func
AtCoder problem 064

C++ solution:

Crystal solution: n, q = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i64)
d = a.each_cons(2).map { |(x, y)| x - y }.to_a
ans = d.sum(&.abs)
q.times do
  l, r, v = read_line.split.map(&.to_i)
  if l != 1
    ans -= d[l - 2].abs
    d[l - 1] -= v
    ans += d[l - 2].abs
  end
  if r != n
    ans -= d[r - 1].abs
    d[r - 1] += v
    ans += d[r - 1].abs
  end
  puts ans
end






Ruby solution: class OnlineSum
    def initialize(n)
        @n = 2 ** Math.log2(n).ceil
        @data = [0] * (@n + 1)
    end

    def add(i, val)
        while i <= @n
            @data[i] += val
            i += i & -i
        end
    end

    def sum(i)
        acc = 0
        while i > 0
            acc += @data[i]
            i -= i & -i
        end
        acc
    end
end

def main
    n, q = readline.split.map!(&:to_i)

    a = ['0']
    a.concat(readline.split)
    a.map!(&:to_i)

    ans = (2..n).inject(0) do |sum, i|
        sum + (a[i-1] - a[i]).abs
    end

    os = OnlineSum.new(n)
    q.times do
        l, r, v = readline.split.map(&:to_i)

        if l > 1 then
            lo = a[l - 1] + os.sum(l - 1)
            hi = a[l - 0] + os.sum(l - 0)
            ans -= (lo - hi).abs
        end
        if r < n then
            lo = a[r + 0] + os.sum(r + 0)
            hi = a[r + 1] + os.sum(r + 1)
            ans -= (lo - hi).abs
        end

        os.add(l, v)
        os.add(r + 1, -v) if r < n

        if l > 1 then
            lo = a[l - 1] + os.sum(l - 1)
            hi = a[l - 0] + os.sum(l - 0)
            ans += (lo - hi).abs
        end
        if r < n then
            lo = a[r + 0] + os.sum(r + 0)
            hi = a[r + 1] + os.sum(r + 1)
            ans += (lo - hi).abs
        end

        puts ans
    end
end

main if __FILE__ == $0
AtCoder problem 064

C++ solution:

Crystal solution: n, q = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i64)
d = a.each_cons(2).map { |(x, y)| x - y }.to_a
ans = d.sum(&.abs)
q.times do
  l, r, v = read_line.split.map(&.to_i)
  if l != 1
    ans -= d[l - 2].abs
    d[l - 1] -= v
    ans += d[l - 2].abs
  end
  if r != n
    ans -= d[r - 1].abs
    d[r - 1] += v
    ans += d[r - 1].abs
  end
  puts ans
end





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var ans: int :: 0
	var diff: []int :: #[n - 1]int
	for i(0, n - 2)
		do diff[i] :: a[i + 1] - a[i]
		do ans :+ diff[i].abs()
	end for
	
	for(1, q)
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt() - 1
		var v: int :: cui@inputInt()
		if(l <> 0)
			do ans :- diff[l - 1].abs()
			do diff[l - 1] :+ v
			do ans :+ diff[l - 1].abs()
		end if
		if(r <> n - 1)
			do ans :- diff[r].abs()
			do diff[r] :- v
			do ans :+ diff[r].abs()
		end if
		do cui@print("\{ans}\n")
	end for
end func
AtCoder problem 064

C++ solution:





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	
	var ans: int :: 0
	var diff: []int :: #[n - 1]int
	for i(0, n - 2)
		do diff[i] :: a[i + 1] - a[i]
		do ans :+ diff[i].abs()
	end for
	
	for(1, q)
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt() - 1
		var v: int :: cui@inputInt()
		if(l <> 0)
			do ans :- diff[l - 1].abs()
			do diff[l - 1] :+ v
			do ans :+ diff[l - 1].abs()
		end if
		if(r <> n - 1)
			do ans :- diff[r].abs()
			do diff[r] :- v
			do ans :+ diff[r].abs()
		end if
		do cui@print("\{ans}\n")
	end for
end func


Ruby solution: class OnlineSum
    def initialize(n)
        @n = 2 ** Math.log2(n).ceil
        @data = [0] * (@n + 1)
    end

    def add(i, val)
        while i <= @n
            @data[i] += val
            i += i & -i
        end
    end

    def sum(i)
        acc = 0
        while i > 0
            acc += @data[i]
            i -= i & -i
        end
        acc
    end
end

def main
    n, q = readline.split.map!(&:to_i)

    a = ['0']
    a.concat(readline.split)
    a.map!(&:to_i)

    ans = (2..n).inject(0) do |sum, i|
        sum + (a[i-1] - a[i]).abs
    end

    os = OnlineSum.new(n)
    q.times do
        l, r, v = readline.split.map(&:to_i)

        if l > 1 then
            lo = a[l - 1] + os.sum(l - 1)
            hi = a[l - 0] + os.sum(l - 0)
            ans -= (lo - hi).abs
        end
        if r < n then
            lo = a[r + 0] + os.sum(r + 0)
            hi = a[r + 1] + os.sum(r + 1)
            ans -= (lo - hi).abs
        end

        os.add(l, v)
        os.add(r + 1, -v) if r < n

        if l > 1 then
            lo = a[l - 1] + os.sum(l - 1)
            hi = a[l - 0] + os.sum(l - 0)
            ans += (lo - hi).abs
        end
        if r < n then
            lo = a[r + 0] + os.sum(r + 0)
            hi = a[r + 1] + os.sum(r + 1)
            ans += (lo - hi).abs
        end

        puts ans
    end
end

main if __FILE__ == $0
AtCoder problem 066

C++ solution:
// マージソートをベースにした O(N log N) 解法
// 期待値が次のように言い換えられることを利用した：
// 0-based, 各区間は [L[i], R[i]) に変換済み, range[i] := R[i] - L[i] として、
// sum_{ 0 <= i < j < N, L[i] <= x < R[i], L[j] <= y < R[j], x > y} ( 1.0 / (range[i] * range[j]) )
#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
using T = double;
const T two_inv = 1.0 / 2.0;

int N;
vector<int> L, R;
vector<P> val;
vector<T> range_inv;
T res;

// mergesort(left_id, right_id) では、上記 sum の内、left_id <= i < center_id <= j < right_id を満たすものを計算する
// 計算方法： L, R をまとめたもの (val) を小さい順に見て、( val による区間内の y ) * ( ありうる x ) を計算している
void mergesort(int left_id, int right_id){
    if(right_id - left_id == 1) return;
    int center_id = left_id + (right_id - left_id) / 2;
    mergesort(left_id, center_id);
    mergesort(center_id, right_id);
    vector<P> lval((center_id - left_id) * 2), rval((right_id - center_id) * 2);

    copy(val.begin() + left_id * 2, val.begin() + center_id * 2, lval.begin());
    lval.emplace_back(1<<30, 1<<30);
    copy(val.begin() + center_id * 2, val.begin() + right_id * 2, rval.begin());
    rval.emplace_back(1<<30, 1<<30);

    int i{}, j{}, k = left_id * 2, last = -1;
    T left_seg{}, right_seg{}, left_sum = center_id - left_id;
    while(k < right_id * 2){
        bool use_left = lval[i] < rval[j];
        val[k] = use_left ? lval[i] : rval[j];
        int d = val[k].first - last, id = val[k].second;
        if(d > 0) {
            // 幅 d の区間内に x, y があるパターン
            res += left_seg * right_seg * d * (d - 1) * two_inv;
            left_sum -= left_seg * d;
            // 幅 d の区間内に y, それより上に x があるパターン
            res += right_seg * d * left_sum;
            last += d;
        }
        (use_left ? left_seg : right_seg) += (id&1 ? - range_inv[id>>1] : range_inv[id>>1]);
        use_left ? ++i : ++j;
        ++k;
    }
}

int main() {
    cin>>N;
    L.resize(N); R.resize(N);
    range_inv.resize(N);
    for(int i = 0; i < N; ++i) {
        cin>>L[i]>>R[i];
        --L[i];
        range_inv[i] = (T)1 / (R[i] - L[i]);
        val.emplace_back(L[i], i * 2);
        val.emplace_back(R[i], i * 2 + 1);
    }
    mergesort(0, N);
    cout<<fixed<<setprecision(10)<<res<<endl;
}


Crystal solution: n = read_line.to_i
ranges = (1..n).map {
  l, r = read_line.split.map(&.to_i)
  l..r
}
puts ranges.each_combination(2).sum { |(r1, r2)|
  r2.sum { |x| (r1.end - x).clamp(0, r1.size) } / r1.size / r2.size
}
AtCoder problem 066

C++ solution:
// マージソートをベースにした O(N log N) 解法
// 期待値が次のように言い換えられることを利用した：
// 0-based, 各区間は [L[i], R[i]) に変換済み, range[i] := R[i] - L[i] として、
// sum_{ 0 <= i < j < N, L[i] <= x < R[i], L[j] <= y < R[j], x > y} ( 1.0 / (range[i] * range[j]) )
#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
using T = double;
const T two_inv = 1.0 / 2.0;

int N;
vector<int> L, R;
vector<P> val;
vector<T> range_inv;
T res;

// mergesort(left_id, right_id) では、上記 sum の内、left_id <= i < center_id <= j < right_id を満たすものを計算する
// 計算方法： L, R をまとめたもの (val) を小さい順に見て、( val による区間内の y ) * ( ありうる x ) を計算している
void mergesort(int left_id, int right_id){
    if(right_id - left_id == 1) return;
    int center_id = left_id + (right_id - left_id) / 2;
    mergesort(left_id, center_id);
    mergesort(center_id, right_id);
    vector<P> lval((center_id - left_id) * 2), rval((right_id - center_id) * 2);

    copy(val.begin() + left_id * 2, val.begin() + center_id * 2, lval.begin());
    lval.emplace_back(1<<30, 1<<30);
    copy(val.begin() + center_id * 2, val.begin() + right_id * 2, rval.begin());
    rval.emplace_back(1<<30, 1<<30);

    int i{}, j{}, k = left_id * 2, last = -1;
    T left_seg{}, right_seg{}, left_sum = center_id - left_id;
    while(k < right_id * 2){
        bool use_left = lval[i] < rval[j];
        val[k] = use_left ? lval[i] : rval[j];
        int d = val[k].first - last, id = val[k].second;
        if(d > 0) {
            // 幅 d の区間内に x, y があるパターン
            res += left_seg * right_seg * d * (d - 1) * two_inv;
            left_sum -= left_seg * d;
            // 幅 d の区間内に y, それより上に x があるパターン
            res += right_seg * d * left_sum;
            last += d;
        }
        (use_left ? left_seg : right_seg) += (id&1 ? - range_inv[id>>1] : range_inv[id>>1]);
        use_left ? ++i : ++j;
        ++k;
    }
}

int main() {
    cin>>N;
    L.resize(N); R.resize(N);
    range_inv.resize(N);
    for(int i = 0; i < N; ++i) {
        cin>>L[i]>>R[i];
        --L[i];
        range_inv[i] = (T)1 / (R[i] - L[i]);
        val.emplace_back(L[i], i * 2);
        val.emplace_back(R[i], i * 2 + 1);
    }
    mergesort(0, N);
    cout<<fixed<<setprecision(10)<<res<<endl;
}



Python solution: # 期待値の線形性
n = int(input())
l, r = [0] * n, [0] * n
for i in range(n):
    l[i], r[i] = map(int,input().split())

ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        x = r[i] - l[i] + 1
        y = r[j] - l[j] + 1
        under = x * y
        for k in range(l[i], r[i] + 1):
            top = max(0, min(k - 1, r[j]) - l[j] + 1)
            ans += top / under

print(ans)
AtCoder problem 066

C++ solution:
// マージソートをベースにした O(N log N) 解法
// 期待値が次のように言い換えられることを利用した：
// 0-based, 各区間は [L[i], R[i]) に変換済み, range[i] := R[i] - L[i] として、
// sum_{ 0 <= i < j < N, L[i] <= x < R[i], L[j] <= y < R[j], x > y} ( 1.0 / (range[i] * range[j]) )
#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
using T = double;
const T two_inv = 1.0 / 2.0;

int N;
vector<int> L, R;
vector<P> val;
vector<T> range_inv;
T res;

// mergesort(left_id, right_id) では、上記 sum の内、left_id <= i < center_id <= j < right_id を満たすものを計算する
// 計算方法： L, R をまとめたもの (val) を小さい順に見て、( val による区間内の y ) * ( ありうる x ) を計算している
void mergesort(int left_id, int right_id){
    if(right_id - left_id == 1) return;
    int center_id = left_id + (right_id - left_id) / 2;
    mergesort(left_id, center_id);
    mergesort(center_id, right_id);
    vector<P> lval((center_id - left_id) * 2), rval((right_id - center_id) * 2);

    copy(val.begin() + left_id * 2, val.begin() + center_id * 2, lval.begin());
    lval.emplace_back(1<<30, 1<<30);
    copy(val.begin() + center_id * 2, val.begin() + right_id * 2, rval.begin());
    rval.emplace_back(1<<30, 1<<30);

    int i{}, j{}, k = left_id * 2, last = -1;
    T left_seg{}, right_seg{}, left_sum = center_id - left_id;
    while(k < right_id * 2){
        bool use_left = lval[i] < rval[j];
        val[k] = use_left ? lval[i] : rval[j];
        int d = val[k].first - last, id = val[k].second;
        if(d > 0) {
            // 幅 d の区間内に x, y があるパターン
            res += left_seg * right_seg * d * (d - 1) * two_inv;
            left_sum -= left_seg * d;
            // 幅 d の区間内に y, それより上に x があるパターン
            res += right_seg * d * left_sum;
            last += d;
        }
        (use_left ? left_seg : right_seg) += (id&1 ? - range_inv[id>>1] : range_inv[id>>1]);
        use_left ? ++i : ++j;
        ++k;
    }
}

int main() {
    cin>>N;
    L.resize(N); R.resize(N);
    range_inv.resize(N);
    for(int i = 0; i < N; ++i) {
        cin>>L[i]>>R[i];
        --L[i];
        range_inv[i] = (T)1 / (R[i] - L[i]);
        val.emplace_back(L[i], i * 2);
        val.emplace_back(R[i], i * 2 + 1);
    }
    mergesort(0, N);
    cout<<fixed<<setprecision(10)<<res<<endl;
}





Rust solution: use proconio::{fastout, input};

#[fastout()]
fn main() {
    input! {
        n:usize,lr:[(usize,usize);n]
    }
    // 各値の個数の期待値
    let mut num_exp = vec![0f64; 101];
    // 各値以下の数の個数の期待値
    let mut num_exp_sum = vec![0f64; 101];

    let mut ans = 0f64;
    for i in 0..n - 1 {
        let (l0, r0) = lr[i];
        let rate0 = 1f64 / (r0 - l0 + 1) as f64;
        for j in l0..=r0 {
            num_exp[j] += rate0;
        }
        for j in 1..=100 {
            num_exp_sum[j] = num_exp_sum[j - 1] + num_exp[j];
        }

        let (l1, r1) = lr[i + 1];
        // a[i+1]を選ぶ
        let rate1 = 1f64 / (r1 - l1 + 1) as f64;
        // 事前にi+1個の値がある。
        for j in l1..=r1 {
            ans += rate1 * ((i + 1) as f64 - num_exp_sum[j]);
        }
    }

    println!("{}", ans);
}