Code Forces 652C Foe Pairs
1 second
256 megabytes
standard input
standard output
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 2
1 3 2 4
3 2
2 4
5
9 5
9 7 2 3 1 4 6 5 8
1 6
4 5
2 7
7 2
2 7
20
In the first example the intervals from the answer are (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).
用一个数组表示每个数字可以向右延生的最大长度,也就是右边哪些点可以和这个数字形成一个区间。注意:
在给定完敌对点,更新数组之后,要从后往前再更新一次。相同左边端点的敌对点应该选择右端点较小的。
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
#define MAX 3*100000
int a[MAX+5];
int tag[MAX+5];
int dp[MAX+5];
int n,m;
int f[MAX+5];
int x,y;
int main()
{
scanf("%d%d",&n,&m);
memset(tag,0,sizeof(tag));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
tag[a[i]]=i;
f[i]=n;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
int l=min(tag[x],tag[y]);
int r=max(tag[x],tag[y]);
f[l]=min(f[l],r-1);
} for(int i=n-1;i>=1;i--)
{
f[i]=min(f[i],f[i+1]);
}
__int64 num=0;
for(int i=1;i<=n;i++)
{
int right=f[i];
num+=right-i+1;
}
printf("%I64d\n",num);
return 0; }
Code Forces 652C Foe Pairs的更多相关文章
- codeforces 652C Foe Pairs 水题
题意:给你若干个数对,给你一个序列,保证数对中的数都在序列中 对于这个序列,询问有多少个区间,不包含这些数对 分析:然后把这些数对转化成区间,然后对于这些区间排序,然后扫一遍,记录最靠右的左端点就好 ...
- CodeForces 652C Foe Pairs
只要计算每个位置最多能到哪个位置,累加即可,DP从后往前预处理一下每个位置到达的最远位置. 有坑点:输入的时候如果同一个点出发的,需要保存最小值. #include<cstdio> #in ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- 图论:(Code Forces) Graph and String
Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
随机推荐
- i2c驱动笔记
基于bcm5300x芯片 注册平台总线设备,设备名bcm5300x_i2c,通过名称与驱动进行匹配. 注册平台总线驱动.驱动名称"bcm5300x_i2c",与设备进行匹配. dr ...
- C中入栈顺序和运算顺序有关系吗?
如下代码会怎么执行? printf( "%c,%c,%c\n", getchar(), getchar(), getchar() ); 实际测试,是倒序执行,感觉上符合“C函数的形 ...
- css -- 背景图片自适应屏幕大小
由于<body>标签的图片不能够拉伸, 解决办法: 1.图片不够大,又background属性不能拉伸图片: 2.只能用个div,把其z-index值设为负,并使这个div大小为整个bod ...
- Ubuntu adb devices : no permissions 解决方法
ntun下USB连接Android手机后,使用adb devices 出现如下: List of devices attached ???????????? no permissions 同时在DDM ...
- 【案例分析】Linux下怎样查看port占用情况
作者:zhanhailiang 日期:2014-11-08 基于express写一个測试server代码例如以下,可是执行失败,报"listen EADDRINUSE".字面上理解 ...
- win7cmd静态绑定arp
netsh -c "172.16.3.1" "f4-ea-67-8b-91-cc"
- Linux下的CPU性能瓶颈分析案例
问题描述: 在对notify执行性能测试时发现cpu负载突然飙高,cpu利用率高达95%.这时候就要排查是哪些线程消耗了cpu,并从代码层找到占用cpu的“罪魁祸首”. 步骤: 1. 先用ps+gre ...
- 【转】如何读懂Oracle文档中的语法图
转自:http://blog.itpub.net/22990797/viewspace-750157/ Oracle文档中用到了两种表达语法的方法,语法图和BNF. BNF, Backus-Naur ...
- XLua系统学习
官方网站:https://github.com/Tencent/xLua 学习手册:http://manual.luaer.cn/ 技术博客: http://blog.csdn.net/column/ ...
- CSS样式中” 大于号”
CSS样式中” 大于号” 在一段CSS代码中见到一个大于号(>),代码如下: BODY#css-zen-garden > DIV#extraDiv2 { BACKGROUND-IMAGE: ...