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 <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
#define MAX 3*100000
int a[MAX+5];
int tag[MAX+5];
int dp[MAX+5];
int n,m;
int f[MAX+5];
int x,y;
int main()
{
scanf("%d%d",&n,&m);
memset(tag,0,sizeof(tag));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
tag[a[i]]=i;
f[i]=n;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
int l=min(tag[x],tag[y]);
int r=max(tag[x],tag[y]);
f[l]=min(f[l],r-1);
} for(int i=n-1;i>=1;i--)
{
f[i]=min(f[i],f[i+1]);
}
__int64 num=0;
for(int i=1;i<=n;i++)
{
int right=f[i];
num+=right-i+1;
}
printf("%I64d\n",num);
return 0; }

Code Forces 652C Foe Pairs的更多相关文章

  1. codeforces 652C Foe Pairs 水题

    题意:给你若干个数对,给你一个序列,保证数对中的数都在序列中 对于这个序列,询问有多少个区间,不包含这些数对 分析:然后把这些数对转化成区间,然后对于这些区间排序,然后扫一遍,记录最靠右的左端点就好 ...

  2. CodeForces 652C Foe Pairs

    只要计算每个位置最多能到哪个位置,累加即可,DP从后往前预处理一下每个位置到达的最远位置. 有坑点:输入的时候如果同一个点出发的,需要保存最小值. #include<cstdio> #in ...

  3. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

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

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

  6. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  7. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  8. 图论:(Code Forces) Graph and String

    Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

随机推荐

  1. 第二百八十二节,MySQL数据库-MySQL视图

    MySQL数据库-MySQL视图 1.视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 2.也 ...

  2. 实现QQ第三方登录教程(php)

    参看地址:http://www.bcty365.com/content-10-2945-1.html

  3. Windows下基于eclipse的Storm应用开发与调试

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3971113.html 本文以一个简单的example来讲解如何开发storm应用程序 1.创建maven ...

  4. CString TCHAR互相转换

    CString->TCHAR*的转化可以用函数GetBuffer() // 原型:LPTSTR GetBuffer( int nMinBufLength ); CString str(_T(&q ...

  5. spring配置文件中bean标签

    <bean id="beanId"(1) name="beanName"(2) class="beanClass"(3) parent ...

  6. mybatis由浅入深day01_9动态sql(9.5sql片段_9.6foreach)

    9 动态sql 9.1 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 9.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个 ...

  7. POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)

    题目 Description One of the more popular activities in San Antonio is to enjoy margaritas in the park ...

  8. Leetcode: Best Time to Buy and Sell Stock I, II

    思路: 1. 算法导论讲 divide and conquer 时, 讲到过这个例子. 书中的做法是先让 price 数组减去一个值, 然后求解最大连续子数组的和. 分治算法的复杂度为 o(nlogn ...

  9. 原来javascript 自带 encodeURI 和 decodeURI文 方法了

    今天百度一下才知道js 自带 encodeURI 和 decodeURI 方法了,之前还找了其他代码来处理(笑哭了.jpg <script type="text/javascript& ...

  10. UITouch 的主要方法:

    1. UITouch 的主要方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesM ...