Code Forces 652C Foe Pairs
来源:互联网 发布:淘宝少女前线初始号 编辑:程序博客网 时间:2024/05/05 01:32
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.
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.
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.
4 21 3 2 43 22 4
5
9 59 7 2 3 1 4 6 5 81 64 52 77 22 7
20
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*100000int 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
- 【Codeforces 652 C Foe Pairs 】
- Codeforces 652C Foe Pairs 【dp】
- Codeforces 652C Foe Pairs 【dp】
- CodeForces 652C Foe Pairs(思维)
- C. Foe Pairs
- C. Foe Pairs
- cf#ecr10-C. Foe Pairs- two pointers
- Codeforences Educational Round10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10C Foe Pairs
- Code Forces #5C
- Code forces 612C
- Codeforces 652C Foe Pairs【二分+RMQ】好像这题Dp做法很多啊
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- 产品学习笔记7—如何与程序员高效沟通
- 设计模式之迭代器模式
- JAVA 异常类
- 泛型--Generic
- 【codevs2516】【BZOJ2461】符环,DP
- Code Forces 652C Foe Pairs
- Bzoj4443:[Scoi2015]小凸玩矩阵:网络流
- 项目中代码健壮性感言
- oracle创建数据库sql脚本
- ubuntu下搭建nfs服务器
- Code Forces 652D Nested Segments(离散化+树状数组)
- PHP常用正则表达式汇总
- 干货分享:让你分分钟学会 javascript 闭包
- 闪回归档: