#P16698. [CSPro 29] 施肥

    ID: 18751 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>2023吉司机线段树 segment tree beatsCSPro

[CSPro 29] 施肥

Background

Luogu’s testdata is for community exchange only and is not official testdata. Official judging link: https://www.cspro.org/.

Problem Description

Spring has come, and the nn fields on Xixi-Aifu Island need to be fertilized. The nn fields are numbered 1,2,⋯ ,n1, 2, \cdots, n, arranged in a line in increasing order of their indices.

To fertilize the fields, Dundun prepared mm fertilizing trucks. However, due to different soil softness and different truck weights, not every truck can fertilize every field. Specifically, the ii-th truck can only exactly drive from field lil_i to field rir_i, and fertilize all fields whose indices are between lil_i and rir_i (including lil_i and rir_i). Here, 1≤li<ri≤n1 \le l_i < r_i \le n.

Dundun wants to make a fertilization plan. First, he will choose an ordered pair (L,R)(L, R) (1≤L<R≤n1 \le L < R \le n), and decide to fertilize only the fields with indices between LL and RR (including LL and RR). Then, he will choose some (or all) of the mm trucks to fertilize the fields. He wants to ensure that: every field with index in [L,R][L, R] is fertilized at least once by some truck, and no field outside this range is fertilized.

Now he wants to know how many different ordered pairs (L,R)(L, R) he can choose as the fertilization range such that he can select some (or all) trucks to achieve his goal.

Input Format

Read input from standard input.

The first line contains two positive integers n,mn, m, representing the number of fields and the number of trucks. It is guaranteed that 2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, 1≤m≤2⋅1051 \le m \le 2 \cdot 10^5.

The next mm lines describe the trucks. The ii-th line contains two positive integers li,ril_i, r_i, representing that the ii-th truck fertilizes from field lil_i to field rir_i. It is guaranteed that 1≤li<ri≤n1 \le l_i < r_i \le n.

Output Format

Output to standard output.

Output one positive integer, the number of different ordered pairs (L,R)(L, R) that Dundun can choose as the fertilization range such that he can select some (or all) trucks to achieve his goal.

4 3
1 2
3 4
2 3
6

Hint

Explanation for Sample 1

In this sample, Dundun can choose 66 different ordered pairs (L,R)(L, R).

The first: choose (L,R)=(1,2)(L, R) = (1, 2), and select only the 11-st truck.

The second: choose (L,R)=(3,4)(L, R) = (3, 4), and select only the 22-nd truck.

The third: choose (L,R)=(2,3)(L, R) = (2, 3), and select only the 33-rd truck.

The fourth: choose (L,R)=(1,4)(L, R) = (1, 4), and select the 11-st and the 22-nd trucks.

The fifth: choose (L,R)=(1,3)(L, R) = (1, 3), and select the 11-st and the 33-rd trucks.

The sixth: choose (L,R)=(2,4)(L, R) = (2, 4), and select the 22-nd and the 33-rd trucks.

Sample 2

See 2.in and 2.ans in the problem directory.

This sample satisfies n,m≤18n, m \le 18.

Sample 3

See 3.in and 3.ans in the problem directory.

This sample satisfies n,m≤50n, m \le 50.

Sample 4

See 4.in and 4.ans in the problem directory.

This sample satisfies n,m≤400n, m \le 400.

Sample 5

See 5.in and 5.ans in the problem directory.

This sample satisfies n,m≤3000n, m \le 3000.

Sample 6

See 6.in and 6.ans in the problem directory.

This sample satisfies special property A.

Sample 7

See 7.in and 7.ans in the problem directory.

This sample satisfies n,m≤200000n, m \le 200000.

Subtasks

Test Point ID n≤n \le m≤m \le Special Property
11 1818 None
22 ^ ^
33
44 5050
55 ^
66 400400
77 ^
88 30003000
99 ^
1010
1111
1212
1313 200000200000 A
1414 ^ ^
1515
1616 None
1717 ^
1818
1919
2020

Special property A: It is guaranteed that for any two trucks, their fertilization ranges do not contain each other. That is, for any 1≤i<j≤m1 \le i < j \le m, either li<lj,ri<rjl_i < l_j, r_i < r_j or li>lj,ri>rjl_i > l_j, r_i > r_j.

Translated by ChatGPT 5