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;

}





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