#abc377d. D - Many Segments 2

D - Many Segments 2

Score : 400400 points

问题陈述

给定两个长度为 NN 的正整数序列 L=(L1,L2,,LN)L=(L_1,L_2,\ldots,L_N)R=(R1,R2,,RN)R=(R_1,R_2,\ldots,R_N),以及一个整数 MM

找出满足以下两个条件的整数对 (l,r)(l,r) 的数量:

  • 1lrM1\le l \le r \le M
  • 对于每一个 1iN1\le i\le N,区间 [l,r][l,r] 没有完全包含区间 [Li,Ri][L_i,R_i]

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given two sequences of positive integers of length NN, L=(L1,L2,,LN)L=(L_1,L_2,\ldots,L_N) and R=(R1,R2,,RN)R=(R_1,R_2,\ldots,R_N), and an integer MM.

Find the number of pairs of integers (l,r)(l,r) that satisfy both of the following conditions:

  • 1lrM1\le l \le r \le M
  • For every 1iN1\le i\le N, the interval [l,r][l,r] does not completely contain the interval [Li,Ri][L_i,R_i].

Constraints

  • 1N,M2×1051\le N,M\le 2\times 10^5
  • 1LiRiM1\le L_i\le R_i\le M
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

L1L_1 R1R_1

L2L_2 R2R_2

\vdots

LNL_N RNR_N

Output

Print the answer.

Sample Input 1

2 4
1 2
3 4

Sample Output 1

5

The five pairs (l,r)=(1,1),(2,2),(2,3),(3,3),(4,4)(l,r)=(1,1),(2,2),(2,3),(3,3),(4,4) satisfy the conditions.

For example, (l,r)=(1,3)(l,r)=(1,3) does not satisfy the conditions because the interval [1,3][1,3] completely contains the interval [1,2][1,2].

Sample Input 2

6 5
1 1
2 2
3 3
4 4
5 5
1 5

Sample Output 2

0

There may be cases where no pairs of integers satisfy the conditions.

Sample Input 3

6 20
8 12
14 20
11 13
5 19
4 11
1 6

Sample Output 3

102