CodeForces 652C Foe Pairs
只要计算每个位置最多能到哪个位置,累加即可,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的更多相关文章
- codeforces 652C Foe Pairs 水题
题意:给你若干个数对,给你一个序列,保证数对中的数都在序列中 对于这个序列,询问有多少个区间,不包含这些数对 分析:然后把这些数对转化成区间,然后对于这些区间排序,然后扫一遍,记录最靠右的左端点就好 ...
- Code Forces 652C Foe Pairs
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- codeforces#572Div2 E---Count Pairs【数学】【同余】
题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...
- 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 ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
- 【CF652C】Foe Pairs(线性扫描)
题意:给你1-n的一个排列和m组数对,问有多少区间不包含任意一个数对. (1 ≤ n, m ≤ 3·105) 思路:数据范围过大,不能用容斥原理 f[i]表示以位置i上的数为左端点,右端点最小到哪里 ...
随机推荐
- OpenGL----绘制立方体,定点数组与顶点缓冲
,立方体是很简单,但是这里只是拿立方体做一个例子,来说明OpenGL在绘制方法上的改进.从原始一点的办法开始一个立方体有六个面,每个面是一个正方形,好,绘制六个正方形就可以了. glBegin(GL_ ...
- Hibernate 系列教程5-双向多对一
主要讲解inverse和cascade的用法 cascade定义的是关系两端对象到对象的级联关系: 而inverse定义的是关系和对象的级联关系(管理外键的值). inverse 属性默认是false ...
- linux ubuntu平台下安装Scrapy
1.安装Python sudo apt-get install python2.7 python2.7-dev 2.安装pip 下载get-pip.py 选中该文件所在路径,执行下面的命令 sudo ...
- C --> OC with RunTime
前言 本来打算写一篇关于runtime的学习总结,无奈长篇大论不是我的风格,就像写申论一样痛苦,加之网上关于tuntime的文章多如牛毛,应该也够童子们学习的了,今天就随便聊聊我的理解吧. runti ...
- 模版引擎Handlebars语法(1)
<script src="handlebars.js"></script></head><body> <div id=&quo ...
- Hadoop: LongWritable cannot be cast to org.apache.hadoop.io.IntWritable
写MR Job的时候遇到一个坑爹的异常: LongWritable cannot be cast to org.apache.hadoop.io.IntWritable 当写Map的时候,key的默认 ...
- android ScrollView嵌套EditText
editext.setOnTouchListener(new OnTouchListener() { @Override publi ...
- 转:selenium webdriver 执行javascript代码
在用selenium webdriver 编写web页面的自动化测试代码时,可能需要执行一些javascript代码,selenium本身就支持执行js,我们在代码中import org.openqa ...
- Centos-ip配置详解
1 搭建好Centos ,我这里是CentOS-6.7-x86_64-minimal 提供一个下载地址 链接:http://pan.baidu.com/s/1nvTUTh3 密码:xewk 2 我是 ...
- FFmpeg的Android平台移植—编译篇
摘要:本文主要介绍将FFmpeg音视频编解码库移植到Android平台上的编译和基本测试过程. 环境准备: ubuntu-12.04.5 android-ndk64-r10-linux-x86_64. ...