跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F1295. Foe Pairs

    ID: 1301 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>组合数学排序双指针数学编程与模拟算法思想进阶难度共享题库Codeforces英文题面题目来源题面语言

Foe Pairs

题目描述

C. Foe Pairs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a permutation p of length n. Also you are given m foe pairs (ai,bi) (1≤ai,bin,aibi).

Your task is to count the number of different intervals (x,y) (1≤xyn) that do not contain any foe pairs. So you shouldn't count intervals (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] and foe pairs are {(3,2),(4,2)}. The interval (1,3) is incorrect because it contains a foe pair (3,2). The interval (1,4) is also incorrect because it contains two foe pairs (3,2) and (4,2). But the interval (1,2) is correct because it doesn't contain any foe pair.

Input

The first line contains two integers n and m (1≤n,m≤3·105) − the length of the permutation p and the number of foe pairs.

The second line contains n distinct integers pi (1≤pin) − the elements of the permutation p.

Each of the next m lines contains two integers (ai,bi) (1≤ai,bin,aibi) − the i-th foe pair. Note a foe pair can appear multiple times in the given list.

Output

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

Note that the answer can be too large, so you should use 64-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.

Examples
Input
4 2
1 3 2 4
3 2
2 4
Output
5
Input
9 5
9 7 2 3 1 4 6 5 8
1 6
4 5
2 7
7 2
2 7
Output
20
Note

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


题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 786 题。保留原作者与原赛事署名。