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

给你一个n的排列(n<=3*10^5),然后给你m个禁止的组合(m<=3*10^5),问你有多少个区间不包含任何一个禁止的组合。

思路:

1.O(n+m)

考虑每个位置,他的最远的右区间是多少,我们可以在输入m个禁止组合的时候,维护位置较小的那个数的最远位置,这样我们便知道了那些出现过的每个较小位置对应的最右位置。

然后逆序更新一遍每个位置的最右位置就行了,因为如果在他右边的位置的最远位置已经确定了,那么在他左边的最远位置的数也一定不能取到这个最远位置。

2.O(nlogn)

对禁止区间按l从小到大排序,逆序加入禁止区间,维护最右边的最小值。

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=300100;
int num[maxn],maxv[maxn];int main(){int n,m,x,y;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&x);num[x]=i;maxv[i]=INF;}for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);x=num[x],y=num[y];if(x>y)swap(x,y);maxv[x]=min(maxv[x],y);}for(int i=n-1;i>=1;i--)maxv[i]=min(maxv[i+1],maxv[i]);ll ans=0;for(int i=1;i<=n;i++){if(maxv[i]==INF)ans=ans+n-i+1;elseans=ans+maxv[i]-i;}printf("%I64d\n",ans);return 0;
}

Educational Codeforces Round 10 C. Foe Pairs相关推荐

  1. Educational Codeforces Round 10 C. Foe Pairs —— 后缀和

    题目链接:http://codeforces.com/problemset/problem/652/C C. Foe Pairs time limit per test 1 second memory ...

  2. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

  3. coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)

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

  4. Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟

    A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...

  5. Educational Codeforces Round 10 B. z-sort

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. Educational Codeforces Round 106 (Rated for Div. 2)D. The Number of Pairs

    Educational Codeforces Round 106 (Rated for Div. 2)D. The Number of Pairs 题目大意 给你三个正整数c,d,k\displays ...

  7. Educational Codeforces Round 113 (Rated for Div. 2) D. Inconvenient Pairs

    Educational Codeforces Round 113(Rated for Div. 2)的其他题解点我 D. Inconvenient Pairs 原题链接 题目大意: 在一个大小为 1e ...

  8. Educational Codeforces Round 100 (Rated for Div. 2)

    文章目录 Educational Codeforces Round 100 (Rated for Div. 2) A. Dungeon B. Find The Array C. Busy Robot ...

  9. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

最新文章

  1. jsonSuggest插件如何在使用前激活
  2. eclipse中output folder和deployment assembly用法有什么不同?
  3. 如何使用windbg在驱动加载时下断
  4. linux中 probe函数的何时调用的?
  5. 使用python操作postgresql 查询
  6. centos6.2 64位LNMP(linux+nginx+mysql+php)实现
  7. python监控服务器是否在线_python检测服务器是否正常
  8. 【Java从入门到天黑|03】JavaSE入门之流程控制
  9. python写空气质量提醒_Python数据可视化:2018年空气质量分析
  10. stm32按键输入实验c语言,stm32f103学习笔记(三)按键输入(IO口输入)
  11. 关于用turbo c 编译出现的 Declaration syntax error 错误 (未解决)
  12. 起步 - 安装 Git
  13. linuxoracle静默安装应答文件修改_巧用组策略分发软件—让IT不再手忙脚乱地奔走在各个工位安装软件...
  14. 00 VirtualBox安装Windows7 32位系统
  15. 大手笔!舒印彪对核电发展表态,华能抢到了核电开发入场券!
  16. 微信邮箱格式是什么意思?为什么我们都在使用微信邮箱?
  17. Linux驱动子系统之I2C(一)
  18. 2017河南省第四届互联网大会圆满落幕 云计算大数据创新成热点
  19. v2订阅一键同步自定义混淆
  20. FPGA开发 Quartus Signaltap的基本使用

热门文章

  1. python-报错和解决方法汇总
  2. 用CSS方法提取网页内容
  3. 劳务报酬收入的计税方法
  4. HTTP流量神器Goreplay核心源码详解
  5. torch.nn.functional
  6. 2021年P气瓶充装新版试题及P气瓶充装模拟考试题库
  7. oracle分开中文和英文_英文简历绝对不能这样写
  8. 【毕设参考】ESP32 + HaaS Python 打造可以远程控制的智能风扇
  9. java字符与ASCII码相互转换
  10. ld 和 ld.so命令