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;

}

Crystal solution: # ac-library.cr by hakatashi https://github.com/google/ac-library.cr
#
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module AtCoder
  # Implements [atcoder::fenwick_tree](https://atcoder.github.io/ac-library/master/document_en/fenwicktree.html).
  #
  # ```
  # tree = AtCoder::FenwickTree(Int64).new(10)
  # tree.add(3, 10)
  # tree.add(5, 20)
  # tree[3..5]  # => 30
  # tree[3...5] # => 10
  # ```
  class FenwickTree(T)
    getter size : Int64
    getter bits : Array(T)

    def initialize(@size)
      @bits = Array(T).new(@size, T.zero)
    end

    # Implements atcoder::fenwick_tree.add(index, value)
    def add(index, value)
      index += 1 # convert to 1-indexed
      while index <= @size
        @bits[index - 1] += value
        index += index & -index
      end
    end

    # Exclusive left sum
    def left_sum(index)
      ret = T.zero
      while index >= 1
        ret += @bits[index - 1]
        index -= index & -index
      end
      ret
    end

    # Implements atcoder::fenwick_tree.sum(left, right)
    def sum(left, right)
      left_sum(right) - left_sum(left)
    end

    # :ditto:
    def [](range : Range(Int, Int))
      left = range.begin
      right = range.exclusive? ? range.end : range.end + 1
      sum(left, right)
    end
  end
end

n, m = read_line.split.map(&.to_i)
lr = (1..m).map { {Int32, Int32}.from read_line.split.map(&.to_i.pred) }

cnt = [0i64] * n
cnt_r = [0i64] * n
lr.each do |(l, r)|
  cnt[l] += 1
  cnt[r] += 1
  cnt_r[r] += 1
end
sum_r = cnt_r.reduce([0i64]) do |acc, x|
  acc << acc[-1] + x
end

ans = m.to_i64 * m.pred // 2
ans -= cnt.sum { |x| x * x.pred // 2 }
ans -= lr.sum { |(l, r)| sum_r[l] }

sum_l = AtCoder::FenwickTree(Int64).new(n.to_i64)
lr.sort_by { |(l, r)| {r, l} }.each do |(l, r)|
  ans -= sum_l[l + 1...r]
  sum_l.add(l, 1i64)
end

puts ans