CF652 C ma~ 对我来说只要是牵扯到方法的题目都是好题
来源:互联网 发布:linux ssh配置 编辑:程序博客网 时间:2024/05/29 18:57
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).
其实这道题目我想了好久。。。感觉果然有点笨呐,竟然没有想到。。。(可能是因为一直想着别人的题解忘了自己应该怎么去思考了吧。呜~多思考哟)(好吧,其实是自己理解错误了所以最后半天没有写出来TAT)
恩,题目大意就是给你一些数字,然后让你找到符合条件的区间数有几个。这个区间中不能有敌人的pair。
思路:枚举,从右开始枚举,右端枚举该数组到左端即可。
用pos数组表示当前坐标所在的位置。(注意,这个区间是题目所给出的数列的区间)
然后就是用r数组来保存这些位置的右端,其中r[i]中,i是表示当前的左端点(然后要从右边开始枚举,这样就不需要考虑左端点了,只需要知道右端点的最小值即可)。然后r数组的值就是敌人的位置的(也就是有区间)。然后这个时候只需要用rb储存又断点就可以了。
- CF652 C ma~ 对我来说只要是牵扯到方法的题目都是好题
- 一个目前对我来说较难的题目
- 2008年,对我来说是非同寻常的一年!
- 美好的生活对我来说是那么奢侈
- 2015,对我来说,注定是不平凡的一年
- 虽然对各位大神来说微不足道 但是对于我来说是一点一点的成就
- 很久以前,我以为,只要我对别人好,能忍让,就会换来同样的回报。
- 百家争鸣的自媒体时代对你来说是好还是坏
- poj2159 对我来说不是水题的水题
- 只要你过的比我好
- 对字符串进行加解密的常用方法-对初学者来说是个很好的入门
- 《不要做一个浮躁的程序员》——对我来说是一篇很好的文章
- 对我来说由衷解脱的一天
- 对我来说很重要的网站
- VR对我来说
- c++训练题(牵扯到大数据的保存)
- 牵扯较多属性和方法的类题目,很简单的题目本来不想发的,如果有同学学到这个题目感觉太长不愿敲代码,copy走我的即可~不过还是建议自己打一打
- 好的,不好的,都是我
- JSON和模型之间最快捷方便的转换iOS框架:MJExtension
- 计算机网络基础之网络层
- xcode instruments 学习
- Unity加载本地文件夹中的所有文件
- Linux设备驱动之——I2C总线
- CF652 C ma~ 对我来说只要是牵扯到方法的题目都是好题
- 反射+多态实现0判断更具不同类型排序
- 网关高可用协议:HSRP、VRRP、GLBP、CASP
- Javascript 严格模式详解
- Android入门之理解Task
- python中 classmethod、staticmethod和普通方法的区别
- Visual Studio——设置缩进
- RecycleView 实现瀑布流 添加分隔
- JVM--内存调优