codeforces_652C. Foe Pairs
来源:互联网 发布:淘宝网皮草 编辑:程序博客网 时间:2024/05/22 13:16
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
用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 longusing 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;}
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
- C. Foe Pairs
- C. Foe Pairs
- 【Codeforces 652 C Foe Pairs 】
- cf#ecr10-C. Foe Pairs- two pointers
- Codeforences Educational Round10 C. Foe Pairs
- Code Forces 652C Foe Pairs
- Codeforces 652C Foe Pairs 【dp】
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Educational Codeforces Round 10 C. Foe Pairs
- Codeforces 652C Foe Pairs 【dp】
- Educational Codeforces Round 10 C. Foe Pairs
- CodeForces 652C Foe Pairs(思维)
- Educational Codeforces Round 10C Foe Pairs
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- 【Educational Codeforces Round 10C】【脑洞 SET】Foe Pairs 不含有敌对pair的区间数
- Codeforces 652C Foe Pairs【二分+RMQ】好像这题Dp做法很多啊
- posix线程使用详解
- Camera2.0新API下的摄像头预览、原始图像数据获取等
- Android 上拉面板 AndroidSlidingUpPanel
- 百度地图定位API,精度提高
- Android开发工具
- codeforces_652C. Foe Pairs
- 集合第八发练习之容器版V2.0
- 最小二乘法求AR模型
- 最小二乘支持向量机(LS-SVM)工具箱及使用说明
- Java 递归实现格雷码(Gray Code)
- vi基础配置
- 产品学习笔记6—MVP产品理念
- 【资源汇集.转】Python 学习资源
- JSp+javaBean开发模式---求和运算