CF652C.Foe Pairs

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation pp of length nn . Also you are given mm foe pairs (ai,bi)(a_{i},b_{i}) ( 1<=ai,bi<=n,aibi1<=a_{i},b_{i}<=n,a_{i}≠b_{i} ).

Your task is to count the number of different intervals (x,y)(x,y) ( 1<=x<=y<=n1<=x<=y<=n ) that do not contain any foe pairs. So you shouldn't count intervals (x,y)(x,y) that contain at least one foe pair in it (the positions and order of the values from the foe pair are not important).

Consider some example: p=[1,3,2,4]p=[1,3,2,4] and foe pairs are (3,2),(4,2){(3,2),(4,2)} . The interval (1,3)(1,3) is incorrect because it contains a foe pair (3,2)(3,2) . The interval (1,4)(1,4) is also incorrect because it contains two foe pairs (3,2)(3,2) and (4,2)(4,2) . But the interval (1,2)(1,2) is correct because it doesn't contain any foe pair.

输入格式

The first line contains two integers nn and mm ( 1<=n,m<=31051<=n,m<=3·10^{5} ) — the length of the permutation pp and the number of foe pairs.

The second line contains nn distinct integers pip_{i} ( 1<=pi<=n1<=p_{i}<=n ) — the elements of the permutation pp .

Each of the next mm lines contains two integers (ai,bi)(a_{i},b_{i}) ( 1<=ai,bi<=n,aibi1<=a_{i},b_{i}<=n,a_{i}≠b_{i} ) — the ii -th foe pair. Note a foe pair can appear multiple times in the given list.

输出格式

Print the only integer cc — the number of different intervals (x,y)(x,y) that does not contain any foe pairs.

Note that the answer can be too large, so you should use 6464 -bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

输入输出样例

  • 输入#1

    4 2
    1 3 2 4
    3 2
    2 4
    

    输出#1

    5
    
  • 输入#2

    9 5
    9 7 2 3 1 4 6 5 8
    1 6
    4 5
    2 7
    7 2
    2 7
    

    输出#2

    20
    

说明/提示

In the first example the intervals from the answer are (1,1)(1,1) , (1,2)(1,2) , (2,2)(2,2) , (3,3)(3,3) and (4,4)(4,4) .

首页