只要计算每个位置最多能到哪个位置,累加即可,DP从后往前预处理一下每个位置到达的最远位置。

有坑点:输入的时候如果同一个点出发的,需要保存最小值。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn=*+; int n,m;
int a[maxn],b[maxn];
int p[maxn]; int dp[maxn]; int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
p[a[i]]=i;
} for(int i=;i<=n;i++) b[i]=0x7FFFFFFF; for(int i=;i<=m;i++)
{
int li,ri;scanf("%d%d",&li,&ri);
int fx=p[li];
int fy=p[ri];
if(fx>fy) swap(fx,fy);
b[fx]=min(b[fx],fy);
} dp[n]=b[n];
for(int i=n-;i>=;i--)
{
if(b[i]==0x7FFFFFFF) dp[i]=min(dp[i+],b[i]);
else dp[i]=min(dp[i+],b[i]-);
} long long ans=; for(int i=;i<=n;i++)
{
if(dp[i]==0x7FFFFFFF) ans=ans+(long long)(n-i+);
else ans=ans+(long long)(dp[i]-i+);
}
printf("%lld\n",ans); return ;
}

CodeForces 652C Foe Pairs的更多相关文章

  1. codeforces 652C Foe Pairs 水题

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

  2. Code Forces 652C Foe Pairs

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  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. Educational Codeforces Round 10 C. Foe Pairs 水题

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

  5. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  6. codeforces#572Div2 E---Count Pairs【数学】【同余】

    题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...

  7. CodeForces - 1189E Count Pairs(平方差)

    Count Pairs You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Fi ...

  8. Codeforces 1188B - Count Pairs(思维题)

    Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...

  9. 【CF652C】Foe Pairs(线性扫描)

    题意:给你1-n的一个排列和m组数对,问有多少区间不包含任意一个数对. (1 ≤ n, m ≤ 3·105) 思路:数据范围过大,不能用容斥原理 f[i]表示以位置i上的数为左端点,右端点最小到哪里 ...

随机推荐

  1. Anton and School

    Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. Git学习 -- 冲突解决

    当连个分支对统一文件提交了不同修改时,可能会出现冲突,例如: $ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merg ...

  3. 解决 Oracle 11g 不能导出空表的问题

    --解决 Oracle 11g 不能导出空表的问题 --执行下面语句,查询数据库中的空表,同时产生分配空间.把生成的结果复制出来并执行. select 'alter table '||table_na ...

  4. AngularJs: Reload page

    <a ng-click="reloadRoute()" class="navbar-brand" title="home" data- ...

  5. Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? \Android Studio快捷键之代码提示

    问:Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? 答:有,as中的快捷键是Ctrl+Alt+V AndroidStudio和Eclipse常用快捷键对比 功能 ...

  6. java-两个jre目录和三个lib目录-转

    lib目录下放置着jar包.程序中的import语句找的就是这些文件!例如:import javax.servlet.RequestDispatcher; 问题在于,在cmd模式下编译,系统会提示:C ...

  7. 转 BAT CMD 批处理文件脚本总结(中文)

    1.               综述 1.”.bat”: 这是微软的第一个批处理文件的后缀名,在几乎所有的Windows 操作系统内都能运行. 2. “.cmd”: 是为Windows NT 设计的 ...

  8. 设置span 宽度的完美解决方案

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 我的android学习脚步----------- 的第一个应用

    刚刚开始学android开发,以前都是在别人调好的应用中修改JNI,现在需要自己一步步走 开发环境:Eclipse+ADT 配置不多讲了,引自:http://www.cnblogs.com/allen ...

  10. ViewController加载顺序与self.view

    转载自:http://blog.csdn.net/ishaoc/article/details/42172749   ViewController的加载顺序如下   从Stroyboard和xib中加 ...