Educational Codeforces Round 10 C. Foe Pairs 水题
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 水题的更多相关文章
- Educational Codeforces Round 7 B. The Time 水题
B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...
- Educational Codeforces Round 7 A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...
- Educational Codeforces Round 11 A. Co-prime Array 水题
A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...
- Educational Codeforces Round 10
A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...
- Educational Codeforces Round 24 CF 818 A-G 补题
6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...
- Educational Codeforces Round 5(A,B题)
虽然是水题但还是贴下代码把 A #include<cstring> #include<cstdio> using namespace std; ; char x[qq],y[q ...
- Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
随机推荐
- 2013-7-31hibernate二级缓存
难得闲 Fckeditor Fckconfig.js大部分配置都在这里面, 增加字体: 程序代码: FCKConfig.FontNames = 'Arial;Comic Sans MS ...
- i春秋第二届春秋欢乐赛RSA256writeup
i春秋第二届春秋欢乐赛writeup 下载之后进行解压 发现四个文件 0x01看到题目是RSA的 又看到public.key 所以直接用kali linux的openssl 0x02可以看到e就是E ...
- for 、forEach 、 forof、 forin遍历对比
一.遍历内容的异同 1.for 和 for...in 是针对数组下标的遍历 2.forEach 及 for...of 遍历的是数组中的元素 二.对非数字下标的处理 由于array在js中也是对象中的一 ...
- IE6-IE9不支持table.innerHTML的解决方法--终极解决办法
一.把要转译的内容放到其他属性中,例如“tt” <p class="feedback_answer_content" tt='${feedInfo.feedback_answ ...
- 浅谈Linux系统中如何查看进程 ——ps,pstree,top,w,全解
进程是一个其中运行着一个或多个线程的地址空间和这些线程所需要的系统资源.一般来说,Linux系统会在进程之间共享程序代码和系统函数库,所以在任何时刻内存中都只有代码的一份拷贝. 1,ps命令 作用:p ...
- JAVA 抽象类、接口
一.抽象类 1.语法定义 在类前面使用 abstract 修饰的类就是抽象类 2.应用场景: (1)在某些情况下,某个父类只是知道其子类应该包含怎样的方法,但无法准确知道这些子类如何实现这些方法. ( ...
- EF – 5.DbSet与DbContext,数据更新奥秘
5.6.4 <DbSet与DbContext> 介绍DbSet与DbContext中的核心属性及重要方法. 5.6.5 <数据更新的奥秘> 这一讲极为重要,因为它揭示出了En ...
- 面试题13:在O(1)时间删除链表节点
注意分情况讨论: 1. 要删除的不是尾节点 2. 链表只有一个节点 3. 链表中有多个节点,删除尾节点 void DeleteNode(ListNode** pListHead, ListNode* ...
- html5.2新特性【长期更新】
先来说几个新定义: 1.p标签里只能是文字内容,不能在里面使用浮动,定位这些特性了.语义化加强,p标签就是文字标签. 2.legend以前只能是纯文本,新版可以加标签了,很爽吧. <fields ...
- spring_150901_hibernate_transaction_xml
实体类: package com.spring.model; import javax.persistence.Entity; import javax.persistence.Id; import ...