题意:http://acm.hdu.edu.cn/showproblem.php?pid=5400

思路:预处理出每个点向左和向右的最远边界,从左向右枚举中间点,把区间答案加到总答案里面。由与可能与前面的区间重叠,需要减去重复的答案,由于左边界非降,所以重叠的区间长度很容易得到。

#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-12; /* -------------------------------------------------------------------------------- */ const int maxn = 1e5 + ; int a[maxn], R[maxn], L[maxn]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, d1, d2;
while (cin >> n >> d1 >> d2) {
for (int i = ; i <= n; i ++) {
scanf("%d", a + i);
}
for (int i = n; i >= ; i --) {
a[i] = a[i] - a[i - ];
}
a[n + ] = INF * ;
R[n + ] = INF;
for (int i = ; i <= n; i ++) {
L[i] = max(, a[i] == d1? L[i - ] : i);
}
for (int i = n; i; i --) {
R[i] = min(n, a[i + ] == d2? R[i + ] : i);
}
int lastl = , lastr =;
ll ans = ;
for (int i = ; i <= n; i ++) {
ll c = R[i] - L[i] + ;
ans += c * (c + ) / ;
ll cc = min(lastr, R[i]) - L[i] + ;
if (cc > ) ans -= cc * (cc + ) / ;
lastl = L[i];
umax(lastr, R[i]);
}
cout << ans << endl;
}
return ;
}

[hdu5400 Arithmetic Sequence]预处理,容斥的更多相关文章

  1. HDU5400 Arithmetic Sequence

    解题思路:这题看懂题目是很关键的,这个区间是等差数列,且公差为d1或d2, 特别注意单个数字也为等差数列.每次求出等差数列序列长度,然后   求出对应这种长度对应有多少种组合方式,累加起来就是结果. ...

  2. HDU 4675 GCD of Sequence(容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4675 题意:给出n,m,K,一个长度为n的数列A(1<=A[i]<=m).对于d(1< ...

  3. HDU 5297 Y sequence 容斥 迭代

    Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...

  4. HDU - 5297:Y sequence (迭代&容斥)

    Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hat ...

  5. hdu4390-Number Sequence(容斥计算)

    题意:给定b数列.计算有多少种数列 a1,a2,...,an 满足条件 a1*a2*...*an=b1*b2*-*bn (ai>1). 解法:处理出b数列中出现的全部质因子的数量记录在map中, ...

  6. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  7. XTU 1242 Yada Number 容斥

    Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime ...

  8. 青云的机房组网方案(简单+普通+困难)(虚树+树形DP+容斥)

    题目链接 1.对于简单的版本n<=500, ai<=50 直接暴力枚举两个点x,y,dfs求x与y的距离. 2.对于普通难度n<=10000,ai<=500 普通难度解法挺多 ...

  9. hdu 5400 Arithmetic Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...

随机推荐

  1. gdb 调试中No symbol “***” in current context解决方法

    主要是因为GCC/G++版本和GDB不匹配造成的,网上也有说是因为O2优化问题,具体啥原因需要自己尝试一下. 解决: 放狗搜索,解决办法是在编译是加-gdwarf-3即可,出现这样的原因是gcc,gd ...

  2. java对象头信息和三种锁的性能对比

    java头的信息分析 首先为什么我要去研究java的对象头呢? 这里截取一张hotspot的源码当中的注释 这张图换成可读的表格如下 |-------------------------------- ...

  3. Python中关于第三方库的补充

    Python语言的强大之处在于它的开源.正是因为它的开源,产生了成百上千的第三方库,涵盖了计算机的几乎所有的方向.第三方库的安装也并不是特别的复杂,通过在cmd中使用pip命令可以安装几乎所有的库,但 ...

  4. Java类的使用

    在一个Java文件中写两个类:一个基本的类,一个测试类.注意:文件名称和测试类名称一致. 如何使用呢?创建对象使用.如何创建对象呢?格式:类名 对象名 = new 类名(); Student s = ...

  5. python数据结构之栈的构建

    class Stack(object): def __init__(self): self.stack=[] def pop(self): return self.stack.pop() def pu ...

  6. MySQL为某字段加前缀、后缀

    在开发过程中,可能会遇到加前缀或者后缀的情况.比如为视频添加路径时,如果手动加起来肯定慢,而且比较不符合程序员的特点,我们就应该能让程序跑就不会手动加. 使用UPDATE sql 语句:update ...

  7. Zookeepe的安装和集群的配置

    Zookeepe的安装和集群的配置 一.Zookeeper的简介 Zookeeper是个框架 二.Zookeeper在Linux上安装 官方提供的下载服务器 地址1:http://mirror.bit ...

  8. Python数据分析入门与实践 学习

    pandas是一个Python语言的软件包,在我们使用Python语言进行机器学习编程的时候,这是一个非常常用的基础编程库.本文是对它的一个入门教程.pandas提供了快速,灵活和富有表现力的数据结构 ...

  9. 浅谈 PHP 与手机 APP 开发

    来源:http://www.thinkphp.cn/topic/5023.html 一.先简单回答两个问题: 1.PHP 可以开发客户端?答:不可以,因为PHP是脚本语言,是负责完成 B/S架构 或 ...

  10. dns的抓包分析

    dns: 域名系统(服务)协议 dns的解析全过程: 1. 浏览器先检查自身缓存中有没有被解析过的这个域名对应的ip地址,如果有,解析结束.同时域名被缓存的时间也可通过TTL属性来设置. 2. 如果浏 ...