puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 012

C++ solution:

Crystal solution: class UnionFind
  @d : Array(Int32)

  def initialize(n : Int32)
    @d = Array.new(n, -1)
  end

  def root(x : Int32)
    @d[x] < 0 ? x : (@d[x] = root(@d[x]))
  end

  def unite(x : Int32, y : Int32)
    x = root(x)
    y = root(y)
    return false if x == y
    x, y = y, x if @d[x] > @d[y]
    @d[x] += @d[y]
    @d[y] = x
    true
  end

  def same?(x : Int32, y : Int32)
    root(x) == root(y)
  end

  def size(x : Int32)
    -@d[root(x)]
  end
end

h, w = read_line.split.map(&.to_i)
uf = UnionFind.new(h * w)
red = Array.new(h) { [false] * w }

read_line.to_i.times do |i|
  query = read_line.split.map(&.to_i.pred)
  case query.shift
  when 0
    i, j = query
    red[i][j] = true
    [{i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}].each do |(i2, j2)|
      if (0...h).includes?(i2) && (0...w).includes?(j2) && red[i2][j2]
        uf.unite(i * w + j, i2 * w + j2)
      end
    end
  when 1
    i1, j1, i2, j2 = query
    puts red[i1][j1] && uf.same?(i1 * w + j1, i2 * w + j2) ? "Yes" : "No"
  end
end





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var m: [][]bool :: #[h, w]bool
	var uni: UnionFind :: (#UnionFind).init(h * w)
	
	var q: int :: cui@inputInt()
	for(1, q)
		var t: int :: cui@inputInt()
		if(t = 1)
			var r: int :: cui@inputInt() - 1
			var c: int :: cui@inputInt() - 1
			do m[r][c] :: true
			var u: int :: r * w + c
			var dx: []int :: [1, 0, -1, 0]
			var dy: []int :: [0, 1, 0, -1]
			for i(0, 3)
				var c2: int :: c + dx[i]
				var r2: int :: r + dy[i]
				if(c2 < 0 | w <= c2)
					skip i
				end if
				if(r2 < 0 | h <= r2)
					skip i
				end if
				if(!m[r2][c2])
					skip i
				end if
				var v: int :: r2 * w + c2
				do uni.unite(u, v)
			end for
		else
			var ra: int :: cui@inputInt() - 1
			var ca: int :: cui@inputInt() - 1
			var rb: int :: cui@inputInt() - 1
			var cb: int :: cui@inputInt() - 1
			var ans: bool :: false
			if(m[ra][ca] & m[rb][cb])
				do ans :: uni.same(ra * w + ca, rb * w + cb)
			end if
			do cui@print(ans ?("Yes\n", "No\n"))
		end if
	end for
	
	class UnionFind()
		var parents: []int
		+func init(n: int): UnionFind
			do me.parents :: [-1].repeat(n)
			ret me
		end func
		+func size(x: int): int
			ret - me.parents[me.root(x)]
		end func
		+func same(x: int, y: int): bool
			ret me.root(x) = me.root(y)
		end func
		+func unite(x: int, y: int): bool
			do x :: me.root(x)
			do y :: me.root(y)
			if(x = y)
				ret false
			end if
			if(me.size(x) < me.size(y))
				do me.parents[y] :+ me.parents[x]
				do me.parents[x] :: y
			else
				do me.parents[x] :+ me.parents[y]
				do me.parents[y] :: x
			end if
			ret true
		end func
		
		func root(x: int): int
			if(me.parents[x] < 0)
				ret x
			else
				do me.parents[x] :: me.root(me.parents[x])
				ret me.parents[x]
			end if
		end func
	end class
end func
AtCoder problem 013

C++ solution:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

const int inf = 2e9;

vector<int> bfs_with_cost(int n, vector<vector<pair<int, int>>> to, int start) {
  vector<int> dist(n + 1, inf);
  queue<int> q;
  dist[start] = 0;
  q.push(start);
  while (q.size() > 0) {
    int now = q.front();
    q.pop();
    for (auto next_pair : to[now]) {
      int next = next_pair.first;
      int cost = next_pair.second;
      if (dist[next] <= dist[now] + cost) {
        continue;
      }
      dist[next] = dist[now] + cost;
      q.push(next);
    }
  }
  return dist;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> to(n + 1, vector<pair<int, int>>(0));
  rep(i, m) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    to[a].push_back({b, c});
    to[b].push_back({a, c});
  }
  vector<int> dist_start = bfs_with_cost(n, to, 1);
  vector<int> dist_end = bfs_with_cost(n, to, n);
  rep1(i, n) { cout << dist_start[i] + dist_end[i] << endl; }
}


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
from heapq import heappush, heappop
INF = 10**18
def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    G = [[] for _ in range(N)]
    for _ in range(M):
        x, y, c = map(int, input().split())
        x, y = x - 1, y - 1
        G[x].append((y, c))
        G[y].append((x, c))

    def f(s):
        q = [(0, s)]
        dist = [INF] * N
        dist[s] = 0
        while q:
            d, v = heappop(q)
            if dist[v] != d: continue
            for nv, c in G[v]:
                nd = d + c
                if dist[nv] <= nd: continue
                dist[nv] = nd
                heappush(q, (nd, nv))
        return dist

    D1 = f(0)
    Dn = f(N - 1)
    for i in range(N):
        print(D1[i] + Dn[i])

if __name__ == '__main__':
    main()
AtCoder problem 014

C++ solution:

Crystal solution: read_line
a = read_line.split.map(&.to_i64).sort
b = read_line.split.map(&.to_i64).sort
puts a.zip(b).sum { |i, j| (i - j).abs }


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
print(sum(abs(a - b) for a, b in zip(A, B)))
AtCoder problem 014

C++ solution:

Crystal solution: read_line
a = read_line.split.map(&.to_i64).sort
b = read_line.split.map(&.to_i64).sort
puts a.zip(b).sum { |i, j| (i - j).abs }





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	var b: []int :: #[n]int
	for i(0, n - 1)
		do b[i] :: cui@inputInt()
	end for
	do a.sort()
	do b.sort()
	
	var ans: int :: 0
	for i(0, n - 1)
		do ans :+ (a[i] - b[i]).abs()
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 014

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
print(sum(abs(a - b) for a, b in zip(A, B)))




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	var b: []int :: #[n]int
	for i(0, n - 1)
		do b[i] :: cui@inputInt()
	end for
	do a.sort()
	do b.sort()
	
	var ans: int :: 0
	for i(0, n - 1)
		do ans :+ (a[i] - b[i]).abs()
	end for
	
	do cui@print("\{ans}\n")
end func
AtCoder problem 015

C++ solution:
#include <iostream>
#include <vector>
using ll = long long;
#define mod 1000000007
using namespace std;

//34[ms]
struct mint{
  ll x;
  mint(ll x = 0) : x((x + mod) % mod){}
  mint operator-() const{ return mint(-x); }
  mint operator+=(const mint &a){
    if((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint &operator++(){
    if(++x == mod) x = 0;
    return *this;
  }
  mint operator++(int){
    mint temp = *this;
    if(++x == mod) x = 0;
    return temp;
  }
  mint &operator-=(const mint &a){
    if((x -= a.x) < 0) x += mod;
    return *this;
  }
  mint &operator--(){
    if(--x < 0) x += mod;
    return *this;
  }
  mint operator--(int){
    mint temp = *this;
    if(--x < 0) x += mod;
    return temp;
  }
  mint &operator*=(const mint &a){
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const{ return mint(*this) += a; }
  mint operator-(const mint &a) const{ return mint(*this) -= a; }
  mint operator*(const mint &a) const{ return mint(*this) *= a; }
  mint pow(ll t) const{
    if(!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const{ return pow(mod - 2); }
  mint &operator/=(const mint &a){ return (*this) *= a.inv(); }
  mint operator/(const mint &a) const{ return mint(*this) /= a; }
  friend istream &operator>>(istream &is, mint &a){ return is >> a.x; }
  friend ostream &operator<<(ostream &os, const mint &a){ return os << a.x; }
};
const int len = 100010;
vector<mint> fact(len), invfact(len);
mint choose(int n, int r){
  return fact[n] * invfact[n - r] * invfact[r];
}

int main(){
  fact[0] = fact[1] = 1;
  for(int i = 2; i < len; i++) fact[i] = fact[i - 1] * i;
  invfact[len - 1] = fact[len - 1].inv();
  for(int i = len - 2; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1);

  int n;
  cin >> n;
  for(int k = 1; k <= n; k++){
    mint ans = 0;
    for(int j = 1; (j-1)*k+1 <= n; j++){
      int num = n - ((j-1)*k + 1);
      //j+1 H num
      ans += choose(j + num, j);
    }
    cout << ans << "\n";
  }
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	
	const mod: int :: 1000000007
	var comb: @Comb :: (#@Comb).init(n, mod)
	var ans: []int :: #[n + 1]int
	for k(1, n)
		var numMax: int :: (n - 1) / k + 1
		for num(1, numMax)
			var remains: int :: n - 1 - k * (num - 1)
			do ans[k] :+ comb.comb(num + remains, remains)
			do ans[k] :% mod
		end for
	end for
	for k(1, n)
		do cui@print("\{ans[k]}\n")
	end for
end func

class Comb()
	var mod: int
	var fact: []int
	var inv: []int
	
	+func init(n: int, mod: int): Comb
		do me.mod :: mod
		do me.fact :: #[n + 1]int
		do me.inv :: #[n + 1]int
		do me.fact[0] :: 1
		for i(1, n)
			do me.fact[i] :: me.fact[i - 1] * i % mod
		end for
		do me.inv[n] :: math@modPow(me.fact[n], mod - 2, mod)
		for i(n, 1, -1)
			do me.inv[i - 1] :: me.inv[i] * i % mod
		end for
		ret me
	end func
	
	+func comb(n: int, r: int): int
		ret me.fact[n] * me.inv[n - r] % me.mod * me.inv[r] % me.mod
	end func
end class
AtCoder problem 016

C++ solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

// ap + bq = gcd(a, b)
pll extGCD(ll a, ll b) {
    ll p11 = 1, p12 = 0, p21 = 0, p22 = 1;
    while (b) {
        ll s = a / b;
        // matrix{{0, 1}, {1, -s}}
        a -= s * b;
        swap(a, b);

        p11 -= s * p21;
        p12 -= s * p22;
        swap(p11, p21);
        swap(p12, p22);
    }
    return {p11, p12};
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, A, B, C;
    cin >> N >> A >> B >> C;

    if (A < B)
        swap(A, B);
    if (A < C)
        swap(A, C);

    ll ans = INT_MAX;

    if (B > C)
        swap(B, C);
    ll g = gcd(B, C);
    ll CC = C / g, BB = B / g;
    auto [p, q] = extGCD(B, C);

    for (int i = 0; i < 10000; ++i) {
        ll rem = N - i * A;
        if (rem < 0)
            break;
        if (rem % g != 0)
            continue;

        ll mul = rem / g;
        ll ptmp = p, qtmp = q;
        ptmp *= mul, qtmp *= mul;
        if (ptmp >= 0) {
            ll d = ptmp / CC;
            ptmp -= d * CC;
            qtmp += d * BB;
        } else {
            ll d = (-ptmp + CC - 1) / CC;
            ptmp += d * CC;
            qtmp -= d * BB;
        }

        if (qtmp < 0)
            continue;

        ans = min(ans, i + ptmp + qtmp);
    }

    cout << ans << "\n";

    return 0;
}


Crystal solution: n = read_line.to_i
a, b, c = read_line.split.map(&.to_i)

limit = 10000
ans = limit
(0..{limit, n // a}.min).each do |i|
  n2 = n - a * i
  (0..{limit, n2 // b}.min).each do |j|
    n3 = n2 - b * j
    ans = {ans, i + j + n3 // c}.min if n3 % c == 0
  end
end
puts ans
AtCoder problem 016

C++ solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

// ap + bq = gcd(a, b)
pll extGCD(ll a, ll b) {
    ll p11 = 1, p12 = 0, p21 = 0, p22 = 1;
    while (b) {
        ll s = a / b;
        // matrix{{0, 1}, {1, -s}}
        a -= s * b;
        swap(a, b);

        p11 -= s * p21;
        p12 -= s * p22;
        swap(p11, p21);
        swap(p12, p22);
    }
    return {p11, p12};
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, A, B, C;
    cin >> N >> A >> B >> C;

    if (A < B)
        swap(A, B);
    if (A < C)
        swap(A, C);

    ll ans = INT_MAX;

    if (B > C)
        swap(B, C);
    ll g = gcd(B, C);
    ll CC = C / g, BB = B / g;
    auto [p, q] = extGCD(B, C);

    for (int i = 0; i < 10000; ++i) {
        ll rem = N - i * A;
        if (rem < 0)
            break;
        if (rem % g != 0)
            continue;

        ll mul = rem / g;
        ll ptmp = p, qtmp = q;
        ptmp *= mul, qtmp *= mul;
        if (ptmp >= 0) {
            ll d = ptmp / CC;
            ptmp -= d * CC;
            qtmp += d * BB;
        } else {
            ll d = (-ptmp + CC - 1) / CC;
            ptmp += d * CC;
            qtmp -= d * BB;
        }

        if (qtmp < 0)
            continue;

        ans = min(ans, i + ptmp + qtmp);
    }

    cout << ans << "\n";

    return 0;
}



Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
C, B, A = sorted(map(int, input().split()))
M = 9999
ans = M
for a in range(min(M, N // A) + 1):
    n1 = N - a * A
    for b in range(min(M, n1 // B) + 1):
        n2 = n1 - b * B
        if n2 % C == 0:
            ans = min(ans, a + b + n2 // C)
print(ans)
AtCoder problem 016

C++ solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

// ap + bq = gcd(a, b)
pll extGCD(ll a, ll b) {
    ll p11 = 1, p12 = 0, p21 = 0, p22 = 1;
    while (b) {
        ll s = a / b;
        // matrix{{0, 1}, {1, -s}}
        a -= s * b;
        swap(a, b);

        p11 -= s * p21;
        p12 -= s * p22;
        swap(p11, p21);
        swap(p12, p22);
    }
    return {p11, p12};
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, A, B, C;
    cin >> N >> A >> B >> C;

    if (A < B)
        swap(A, B);
    if (A < C)
        swap(A, C);

    ll ans = INT_MAX;

    if (B > C)
        swap(B, C);
    ll g = gcd(B, C);
    ll CC = C / g, BB = B / g;
    auto [p, q] = extGCD(B, C);

    for (int i = 0; i < 10000; ++i) {
        ll rem = N - i * A;
        if (rem < 0)
            break;
        if (rem % g != 0)
            continue;

        ll mul = rem / g;
        ll ptmp = p, qtmp = q;
        ptmp *= mul, qtmp *= mul;
        if (ptmp >= 0) {
            ll d = ptmp / CC;
            ptmp -= d * CC;
            qtmp += d * BB;
        } else {
            ll d = (-ptmp + CC - 1) / CC;
            ptmp += d * CC;
            qtmp -= d * BB;
        }

        if (qtmp < 0)
            continue;

        ans = min(ans, i + ptmp + qtmp);
    }

    cout << ans << "\n";

    return 0;
}






Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: int :: cui@inputInt()
	var b: int :: cui@inputInt()
	var c: int :: cui@inputInt()
	const max: int :: 9999
	var ans: int :: max
	for i(0, max)
		for j(0, max - i)
			var remains: int :: n - a * i - b * j
			if(remains >= 0 & remains % c = 0)
				do ans :: lib@min(ans, i + j + remains / c)
			end if
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 016

C++ solution:

Crystal solution: n = read_line.to_i
a, b, c = read_line.split.map(&.to_i)

limit = 10000
ans = limit
(0..{limit, n // a}.min).each do |i|
  n2 = n - a * i
  (0..{limit, n2 // b}.min).each do |j|
    n3 = n2 - b * j
    ans = {ans, i + j + n3 // c}.min if n3 % c == 0
  end
end
puts ans


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
C, B, A = sorted(map(int, input().split()))
M = 9999
ans = M
for a in range(min(M, N // A) + 1):
    n1 = N - a * A
    for b in range(min(M, n1 // B) + 1):
        n2 = n1 - b * B
        if n2 % C == 0:
            ans = min(ans, a + b + n2 // C)
print(ans)
AtCoder problem 016

C++ solution:

Crystal solution: n = read_line.to_i
a, b, c = read_line.split.map(&.to_i)

limit = 10000
ans = limit
(0..{limit, n // a}.min).each do |i|
  n2 = n - a * i
  (0..{limit, n2 // b}.min).each do |j|
    n3 = n2 - b * j
    ans = {ans, i + j + n3 // c}.min if n3 % c == 0
  end
end
puts ans





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: int :: cui@inputInt()
	var b: int :: cui@inputInt()
	var c: int :: cui@inputInt()
	const max: int :: 9999
	var ans: int :: max
	for i(0, max)
		for j(0, max - i)
			var remains: int :: n - a * i - b * j
			if(remains >= 0 & remains % c = 0)
				do ans :: lib@min(ans, i + j + remains / c)
			end if
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 016

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
C, B, A = sorted(map(int, input().split()))
M = 9999
ans = M
for a in range(min(M, N // A) + 1):
    n1 = N - a * A
    for b in range(min(M, n1 // B) + 1):
        n2 = n1 - b * B
        if n2 % C == 0:
            ans = min(ans, a + b + n2 // C)
print(ans)




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var a: int :: cui@inputInt()
	var b: int :: cui@inputInt()
	var c: int :: cui@inputInt()
	const max: int :: 9999
	var ans: int :: max
	for i(0, max)
		for j(0, max - i)
			var remains: int :: n - a * i - b * j
			if(remains >= 0 & remains % c = 0)
				do ans :: lib@min(ans, i + j + remains / c)
			end if
		end for
	end for
	do cui@print("\{ans}\n")
end func
AtCoder problem 017

C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;



class BIT {

public:

	int size_ = 1;

	vector<long long> bit;



	void init(int sz) {

		size_ = sz + 2;

		bit.resize(size_ + 2, 0);

	}

	void add(int pos, long long x) {

		pos++;

		while (pos <= size_) {

			bit[pos] += x;

			pos += (pos & -pos);

		}

	}

	long long sum(int pos) {

		long long s = 0; pos++;

		while (pos >= 1) {

			s += bit[pos];

			pos -= (pos & -pos);

		}

		return s;

	}

};



// Input

long long N, M;

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



// Kazoeage

long long V1[1 << 18], V2[1 << 18], V3[1 << 18];



int main() {

	// Step #1. Input

	cin >> N >> M;

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



	// Step #2. Get Answer1

	long long Answer1 = 0;

	for (int i = 1; i <= M; i++) V3[L[i]] += 1;

	for (int i = 1; i <= M; i++) V3[R[i]] += 1;

	for (int i = 1; i <= N; i++) Answer1 += V3[i] * (V3[i] - 1LL) / 2LL;



	// Step #3. Get Answer2

	long long Answer2 = 0;

	for (int i = 1; i <= M; i++) V1[R[i]] += 1;

	for (int i = 1; i <= M; i++) V2[L[i] - 1] += 1;

	for (int i = 1; i <= N; i++) V1[i] += V1[i - 1];

	for (int i = 1; i <= N; i++) Answer2 += V1[i] * V2[i];



	// Step #4. Sorting

	vector<pair<long long, long long>> vec;

	for (int i = 1; i <= M; i++) vec.push_back(make_pair(R[i], L[i]));

	sort(vec.begin(), vec.end());



	// Step #5. Get Answer3

	long long Answer3 = 0;

	BIT Z; Z.init(N + 2);

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

		long long cl = vec[i].second, cr = vec[i].first;

		long long ret = Z.sum(cr) - Z.sum(cl);

		Answer3 += ret;

		Z.add(cl, 1);

	}



	// Step #6. Output The Answer!

	long long Total = M * (M - 1) / 2LL;

	long long SumAns = Answer1 + Answer2 + Answer3;

	cout << Total - SumAns << endl;

	return 0;

}






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

    def inc(i)
        m = 1
        while m <= @n
            @data[i] += 1 if (i & m) == 0
            i |= m; m <<= 1
        end
    end

    def sum(i)
        i += 1
        m = 1
        acc = 0
        while i > 0
            acc += @data[i - 1] if (i & m) != 0
            i &= ~m; m <<= 1
        end
        acc
    end
end

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

    graph = Array.new(n) { [] }
    m.times do
        a, b = readline.split.map(&:to_i)
        graph[a - 1].push(b - 1)
    end
    os = OnlineSum.new(n)
    ans = 0
    for u in 0...n
        graph[u].sort!.reverse!
        for v in graph[u]
            ans += os.sum(v - 1) - os.sum(u)
            os.inc(v)
        end
    end
    puts ans
end

main if __FILE__ == $0
AtCoder problem 017

C++ solution:





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var m: int :: cui@inputInt()
	var lr: []Lr :: #[m]Lr
	for i(0, m - 1)
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt() - 1
		do lr[i] :: (#Lr).init(l, r)
	end for
	do lr.sort()
	
	var bitL: @Bit :: (#@Bit).init(n)
	var bitR: @Bit :: (#@Bit).init(n)
	var ans: int :: 0
	for i(0, m - 1)
		var l: int :: lr[i].l
		var r: int :: lr[i].r
		if(l <> 0)
			do ans :+ bitL.sum(l - 1)
			do ans :- bitR.sum(l)
		end if
		do bitL.add(l, 1)
		do bitR.add(r, 1)
	end for
	
	do cui@print("\{ans}\n")
	
	class Lr()
		+var l: int
		+var r: int
		+func init(l: int, r: int): Lr
			do me.l :: l
			do me.r :: r
			ret me
		end func
		+*func cmp(t: kuin@Class): int
			if(me.r < (t $ Lr).r)
				ret - 1
			elif(me.r > (t $ Lr).r)
				ret 1
			elif(me.l > (t $ Lr).l)
				; 今回の方法の場合、
				; rが等しいときは、lが大きい順にしないと、
				; 数え上げ時に重複しやすい。
				ret - 1
			end if
			ret 0
		end func
	end class
end func

class Bit()
	var n: int
	var data: []int
	+func init(n: int): Bit
		do me.n :: n
		do me.data :: #[n + 1]int
		ret me
	end func
	+func add(i: int, v: int)
		var x: int :: i + 1
		while(x <= me.n)
			do me.data[x] :+ v
			do x :+ (x $ bit64).and(((-x) $ bit64)) $ int
		end while
	end func
	+func sum(i: int): int
		var x: int :: i + 1
		var res: int :: 0
		while(x > 0)
			do res :+ me.data[x]
			do x :- (x $ bit64).and(((-x) $ bit64)) $ int
		end while
		ret res
	end func
end class


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

    def inc(i)
        m = 1
        while m <= @n
            @data[i] += 1 if (i & m) == 0
            i |= m; m <<= 1
        end
    end

    def sum(i)
        i += 1
        m = 1
        acc = 0
        while i > 0
            acc += @data[i - 1] if (i & m) != 0
            i &= ~m; m <<= 1
        end
        acc
    end
end

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

    graph = Array.new(n) { [] }
    m.times do
        a, b = readline.split.map(&:to_i)
        graph[a - 1].push(b - 1)
    end
    os = OnlineSum.new(n)
    ans = 0
    for u in 0...n
        graph[u].sort!.reverse!
        for v in graph[u]
            ans += os.sum(v - 1) - os.sum(u)
            os.inc(v)
        end
    end
    puts ans
end

main if __FILE__ == $0
AtCoder problem 018

C++ solution:
#include <iostream>

#include <cmath>

using namespace std;



int Q;

long double T, L, X, Y, E;

long double PI = 3.14159265358979;



long double query(long double I) {

	long double cx = 0;

	long double cy = -(L / 2.0) * sin(I / T * 2.0 * PI);

	long double cz = (L / 2.0) - (L / 2.0) * cos(I / T * 2.0 * PI);

	long double d1 = sqrt((cx - X) * (cx - X) + (cy - Y) * (cy - Y));

	long double d2 = cz;

	long double kaku = atan2(d2, d1);

	return kaku * 180.0L / PI;

}



int main() {

	cin >> T;

	cin >> L >> X >> Y;

	cin >> Q;

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

		cin >> E;

		printf("%.12Lf\n", query(E));

	}

	return 0;

}

Crystal solution: TAU = Math::PI * 2
t = read_line.to_i
l, tx, ty = read_line.split.map(&.to_i)
read_line.to_i.times do
  e = read_line.to_i
  angle = TAU / t * e
  y = -Math.sin(angle) * l / 2
  height = l / 2 - Math.cos(angle) * l / 2
  dist = Math.hypot(tx, ty - y)
  puts Math.atan(height / dist) * 360 / TAU
end