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

用v数组保存每个数的指标,b[x]表示指标x上的数的pair对数的指标,若把x称为起点,则b[x]为终点,没有pair对数的指标指向指标n,bad[x]表示当前指标x上有几个终点。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define inf 0x3f3f3f3f
#define maxn 300010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;int a[maxn];
int b[maxn];
int v[maxn];
int bad[maxn];
int main()
{int i,n,m;mem(b,0);mem(bad,0);Si(n);Si(m);for(i=0;i<n;i++){Si(a[i]);v[a[i]]=i;b[i]=n;}W(m){int x,y;Si(x);Si(y);x=v[x];y=v[y];if(x>y)swap(x,y);b[x]=min(b[x],y);}int l,r=0;LL ans=0;for(l=0;l<n;l++){while(r<n&&bad[r]==0){bad[b[r]]++;r++;}ans+=r-l;bad[b[l]]--;}Pl(ans);return 0;
}

Note

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

codeforces_652C. 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 time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

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

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

  4. Code Forces 652C Foe Pairs

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

  5. 【Codeforces 652 C Foe Pairs 】

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

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

  7. Codeforces 652C Foe Pairs 【dp】

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

  8. C. Foe Pairs

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

  9. 【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 o ...

最新文章

  1. CVPR2020:点云三维目标跟踪的点对盒网络(P2B)
  2. Veeam 跻身 Gartner 2016 数据中心备份和恢复软件魔力象限领导者象限
  3. python selenium 处理弹窗_python selenium 弹出框处理的实现
  4. 在linux上配置JDK环境变量
  5. 【Groovy】闭包 Closure ( 闭包定义 | 闭包类型 | 查看编译后的字节码文件中的闭包类型变量 )
  6. 畅谈程序人生暨孙鑫老师与读者交流会
  7. Ionic Angular自动捕获错误 配置Angular2.x +
  8. tar linux 举例,linux 的tar 命令详解举例
  9. 用户画像之Spark ML实现
  10. Kali Linux 2019.4用U盘安装以及解决Kali Linux 2019.4中文乱码问题
  11. 晨哥真有料丨盘点追女生的作死行为!
  12. 阿里达摩院420集python_阿里达摩院推荐的420集的python教程,入门到精通简直不要太简单...
  13. Atitit.故障排除系列---NoClassDefFoundError NoClassDefFoundError
  14. Nero8刻录引导系统光盘镜像图文教程
  15. Drools教程(基础篇)—— Eclipse插件安装
  16. Apk脱壳圣战之---脱掉 爱加密 的壳
  17. 文娱干货丨如何体系化构建优质社区氛围?
  18. 下载网易云音乐的MV
  19. SQL SERVER 使用stuff函数进行分组且合并
  20. Corona建筑表现作品:岩石上的住宅

热门文章

  1. [用友问题] 系统存在的问题
  2. da转化输出不同波形8086和DAC0832的Proteus仿真
  3. css 微信红包,用React加CSS3实现微信拆红包动画
  4. Oracle VM VirtualBox VBX 虚拟机 Windows 10主机安装Ubuntu18.4 文件共享和root登录
  5. 【考研政治】思维导图及笔记
  6. 流量回放:保证业务技术升级的神器
  7. IT安全交给MSP,企业能当“甩手掌柜”吗?
  8. Required String parameter XXX is not present
  9. 选机器学习还是计算机视觉,当机器学习遇到计算机视觉--上篇
  10. github-july-回文判断