【Educational Codeforces Round 10C】【脑洞 SET】Foe Pairs 不含有敌对pair的区间数

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).

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template inline void gmin(T1 &a, T2 b) { if (bb[N];
multisetsot;
int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		sot.clear();
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &a[i]);
			p[a[i]] = i;
			b[i].clear();
		}
		for (int i = 1; i <= m; ++i)
		{
			int x, y; scanf("%d%d", &x, &y);
			x = p[x]; y = p[y];
			if (x > y)swap(x, y);
			b[x].push_back(y);
			sot.insert(y);
		}
		LL ans = 0;
		for (int i = 1; i <= n; ++i)
		{
			if (sot.empty())ans += n - i + 1;
			else ans += *sot.begin() - i;
			for (int j = b[i].size() - 1; ~j; --j)sot.erase(sot.find(b[i][j]));
		}
		printf("%lld\n", ans);
	}
	return 0;
}
/*
【trick&&吐槽】
1,这题读题十分关键,否则会误入歧途。
2,multiset的用法要注意先find再erase

【题意】
给你一个长度为n(3e5)的全排列。
我们还有m(3e5)个敌对pair(x,y) (x!=y,x与y的前后关系并不重要)。
问你一共有多少个区间[l,r](l<=r),使得区间内不存在任何一个敌对pair

【类型】
脑洞

【分析】
我们只要枚举一下区间左界l,然后判定一下最大符合要求的区间右界r即可。
这个要如何做呢?
我们维护一个multiset(或者priority_queue),multiset中存敌对pair的右界
我们枚举区间左界的时候,把这个区间之前的所有区间(即set中的右界)都清除。
这样,假如说l=x,*sot.begin()==y,对答案的贡献就是y-x。

【时间复杂度&&优化】
O(nlogn)

*/



你可能感兴趣的:(题库-CF,CodeForces,脑洞,STL-set)