C. Foe Pairs

题目连接:

http://www.codeforces.com/contest/652/problem/C

Description

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.

Sample Input

4 2
1 3 2 4
3 2
2 4

Sample Output

5

Hint

题意

给你一个n个数的排列

然后给你m组数

然后问你一共有多少个区间不包含这m组数。

题解:

对于每一个位置,我们直接暴力算出这个位置最多往左边延展多少。

但是显然这样子会有重复的,我们在扫的时候,维护一个当前最多往左边延展多少就好了

这样就不会有重复的了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;int n,m,p[maxn],t[maxn];
int c[maxn];
int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&p[i]);t[p[i]]=i;}for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);int a = 0,b = 0;a=max(t[x],t[y]),b=min(t[x],t[y]);c[a]=max(c[a],b);}long long ans = 0;int l = 0;for(int i=1;i<=n;i++){l=max(c[i],l);ans+=i-l;}cout<<ans<<endl;
}

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

  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 7 B. The Time 水题

    B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...

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

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

  6. Educational Codeforces Round 10 B. z-sort

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

  7. Codeforces Round #300 A. Cutting Banner 水题

    A. Cutting Banner Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pro ...

  8. Educational Codeforces Round 11 C hard process_补题——作为司老大的脑残粉

    司老大当时教了一种姿势枚举连续K个0,说实话当时比赛写这题完全蒙了 纵然后来知道思路还是写了一段时间 真的是.. 题目大意 n长度的序列,由0 1构成 我们可以改变 k个0为1 求可以得到的最长连续1 ...

  9. Educational Codeforces Round 106 (Rated for Div. 2)

    Educational Codeforces Round 106 (Rated for Div. 2) 题号 题目 知识点 A Domino on Windowsill B Binary Remova ...

最新文章

  1. 白话hash和数字签名,保证你看得懂
  2. DataGridView中实现点击单元格Cell动态添加自定义控件
  3. springboot activiti 配置项详解
  4. teamcity mysql 配置_CentOS 7 上 TeamCity 安装
  5. 03-13 微信小程序自动化测试
  6. visio业务流程图教学_用visio软件怎样画数据流程图和业务流程图?
  7. 有没有换发型的软件?一分钟智能更换发型
  8. 微机8088主板图绘制
  9. oracle用命令导入dmp,Oracle导出导入dmp文件(exp.imp命令行)
  10. 【韵律迁移】Robust and fine-grained prosody control of end-to-end speech synthesis
  11. puppeteer实现网页截图
  12. 了解更多全国各地浴室5×8装修图片
  13. Android下拉列表怎么做?(小白速成7)
  14. 安装软件总是遇到“Windows Installer 正在安装”,下载Windows Installer Clean Up安装不了
  15. 【杂谈】万字长文回顾深度学习的崛起背景,近10年在各行各业中的典型应用
  16. 完整英文版资产负债表、利润表及现金流量表
  17. Activity是什么
  18. 修复移动硬盘文件或目录损坏且无法读取
  19. 日系插画学习笔记(六):日系角色脸部画法-2头发
  20. X-Forwarded-For伪造

热门文章

  1. RuntimeError: Cannot re-initialize CUDA in forked subprocess
  2. 3D模型修补软件Magics的几个基本操作(设置中文、自定义工具页、缩放与拉伸)
  3. 迁移学习(Transfer Learning)概述及代码实现
  4. 可/不可剥夺内核 可/不可重入函数
  5. java基础多线程抢红包_高并发开发-微信抢红包实现
  6. HTTP协议思维导图
  7. 【Dear Imgui】组件的使用之Text
  8. 重庆三调工作中将mdb数据转换成vct格式的分析
  9. 【Unity】关于DrawMeshInstanced does not support the shader 的问题
  10. 我的大学生活回顾与反思