题目链接:http://codeforces.com/problemset/problem/652/C

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.pos[val]记录val的下标,lim[l]记录以l为左端点,其敌人作为右端点的最小下标。

2.为使得每个间隔不出现死敌,所以需要对lim[l]进行修改,做法是从右往左更新lim[l]的最小值。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 3e5+10;int n, m;
int pos[maxn], lim[maxn];void init()
{scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++){int v;scanf("%d",&v);pos[v] = i;lim[i] = n + 1;}
}void solve()
{for(int i = 1; i<=m; i++){int x, y;scanf("%d%d",&x,&y);int l = pos[x];int r = pos[y];if(l>r) swap(l,r);lim[l] = min(lim[l], r);}for(int i = n-1; i>=1; i--)lim[i] = min(lim[i], lim[i+1]);LL ans = 0;for(int i = 1; i<=n; i++)ans += 1LL*lim[i] - 1LL*i;cout<<ans<<endl;
}int main()
{init();solve();
}

Educational Codeforces Round 10 C. Foe Pairs —— 后缀和相关推荐

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

  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 133 (Rated for Div. 2)(CD题解)

    Educational Codeforces Round 133 (Rated for Div. 2)CD题解 过AB补CD C. Robot in a Hallway 题意 题意:现有 2∗m 的方 ...

  9. Educational Codeforces Round 76 (Rated for Div. 2) 题解

    Educational Codeforces Round 76 题解 比赛链接 A. Two Rival Students #include<bits/stdc++.h> using na ...

最新文章

  1. h5 解决ios端输入框失去焦点后页面不回弹或者底部留白问题
  2. 11. jsp与servlet之间页面跳转及参数传递实例
  3. 《深入理解Java虚拟机》笔记6——高效并发
  4. simditor存储数据到前台展示去掉标签
  5. 小米回应将放弃“MI”字logo:不存在停止使用
  6. JQuery绑定及效果
  7. 禁用DropDownList某一选项
  8. 以“基”取胜:戴尔科技云平台 释放企业新动能
  9. C语言代码输出星期几,C语言程序设计: 输入年月日 然后输出是星期几
  10. RHEL下SendMail修改发邮箱地址
  11. matlab点乘的实现
  12. WebLogic 服务器概述 与 部署 Java Web 应用
  13. savehtml php,PHP DOMDocument saveHTMLFile()用法及代码示例
  14. Android开发-Android项目结构
  15. 图解数据库左连接、右连接、内连接、外连接、全连接的区别
  16. 价格便宜一半 这款智能座舱芯片实际性能却超越高通8155?
  17. Diligent将收购Steele Compliance Solutions
  18. 计算机应用能力考试ppt2003,[全国专业技术人员计算机应用能力考试PPT2003题库版.doc...
  19. 【Python】flask框架学习 flask框架的基本使用
  20. python能不能开发安卓app_Python能开发安卓APP吗?使用python开发网站的学习路线是怎样的?...

热门文章

  1. C++:实现量化有限差分SABR测试实例
  2. 联想+A916+原版官方稳定精简ROM
  3. echarts生成漂亮的地图
  4. day40-存储基本概念与SAN存储
  5. 2017 robotart x86_Robotart|国际机器人艺术大赛
  6. cad线性标注样式修改在哪里_如何设置CAD中线性标注?
  7. QQ空间触屏版说说模拟评论
  8. Springboot配置文件-spring.profiles.active
  9. u盘安装ubuntu出现:【error5】Input/output error的解决办法之一
  10. 图片在框内平行css,CSS行内垂直对齐