1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 【Codeforces 652 C Foe Pairs 】

【Codeforces 652 C Foe Pairs 】

时间:2023-01-10 23:17:58

相关推荐

【Codeforces 652 C 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, bi ≤ n, ai ≠ bi).

Your task is to count the number of different intervals (x, y) (1 ≤ x ≤ y ≤ n) 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 ≤ pi ≤ n) — the elements of the permutation p.

Each of the next m lines contains two integers (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi) — 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).

题意:给你1-n的一个排列和m组数对,问不包含任意一个数对的区间数

思路 : 找出不满足的区间,与总个数相减

AC代码:

#include<bits/stdc++.h>using namespace std;typedef long long LL;const int K = 3e5 + 10;int o[K],p[K];int main(){int N,M,a,x,y;scanf("%d %d",&N,&M);for(int i = 1 ; i <= N ; i++){scanf("%d",&a);o[a] = i; p[i] = N + 1;}while(M--){scanf("%d %d",&x,&y);if(o[x] > o[y]) swap(x,y);p[o[x]] = min(p[o[x]],o[y]); // 更新能达到的最大右区间}for(int i = N - 1 ; i >= 1; i--)p[i] = min(p[i],p[i + 1]);LL ans = (LL)N * (N + 1) / 2;for(int i = 1 ; i < N; i++)ans -= (N + 1 - p[i]);printf("%I64d\n",ans);return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。