Problem Statement

There are NN buildings along the AtCoder Street, numbered 11 through NN from west to east. Initially, Buildings 1,2,…,N1,2,…,N have the heights of A1,A2,…,ANA1,A2,…,AN, respectively.

Takahashi, the president of ARC Wrecker, Inc., plans to choose integers ll and rr (1≤l<r≤N)(1≤l<r≤N) and make the heights of Buildings l,l+1,…,rl,l+1,…,r all zero.
To do so, he can use the following two kinds of operations any number of times in any order:

  • Set an integer xx (l≤x≤r−1)(l≤x≤r−1) and increase the heights of Buildings xx and x+1x+1 by 11 each.
  • Set an integer xx (l≤x≤r−1)(l≤x≤r−1) and decrease the heights of Buildings xx and x+1x+1 by 11 each. This operation can only be done when both of those buildings have heights of 11 or greater.

Note that the range of xx depends on (l,r)(l,r).

How many choices of (l,r)(l,r) are there where Takahashi can realize his plan?

Constraints

  • 2≤N≤3000002≤N≤300000
  • 1≤Ai≤1091≤Ai≤109 (1≤i≤N)(1≤i≤N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A1A1 A2A2 ⋯⋯ ANAN

Output

Print the answer.


Sample Input 1

5
5 8 8 6 6

Sample Output 1

3

Takahashi can realize his plan for (l,r)=(2,3),(4,5),(2,5)(l,r)=(2,3),(4,5),(2,5).

For example, for (l,r)=(2,5)(l,r)=(2,5), the following sequence of operations make the heights of Buildings 2,3,4,52,3,4,5 all zero.

  • Decrease the heights of Buildings 44 and 55 by 11 each, six times in a row.
  • Decrease the heights of Buildings 22 and 33 by 11 each, eight times in a row.

For the remaining seven choices of (l,r)(l,r), there is no sequence of operations that can realize his plan.


Sample Input 2

7
12 8 11 3 3 13 2

Sample Output 2

3

Takahashi can realize his plan for (l,r)=(2,4),(3,7),(4,5)(l,r)=(2,4),(3,7),(4,5).

For example, for (l,r)=(3,7)(l,r)=(3,7), the following figure shows one possible solution.


Sample Input 3

10
8 6 3 9 5 4 7 2 1 10

Sample Output 3

1

Takahashi can realize his plan for (l,r)=(3,8)(l,r)=(3,8) only.


Sample Input 4

14
630551244 683685976 249199599 863395255 667330388 617766025 564631293 614195656 944865979 277535591 390222868 527065404 136842536 971731491

Sample Output 4

8

题意:

有 n 个数,每次可以将 a[x],a[x+1] 同时 +1/-1 ,问存在多少区间同时减为 0

题解:

直觉告诉我用差分来做,然后模拟出来的结果时间复杂度都是 O(n^2)

直到看到了 hu_tao 大佬的讨论,一语惊醒,每次操作一定是将一个奇数位和偶数位同时操作,所以无论怎么变一个区间想要同时变为 0,那么这个区间上奇数位置和偶数位置的加和是相同的;

所以将奇数位置处的值置为负数,利用同余定理,就可以找到任意一个连续的子段加和为 0

const int N=1e6+5;

    int n, m, _;
int i, j, k;
ll a[N];
map<ll,int> mp; signed main()
{
//IOS;
while(~sd(n)){
for(int i=1;i<=n;i++){
sll(a[i]);
if(i%2==0) a[i]=-a[i];
}
ll ans=0;
mp[0]++;
for(int i=1;i<=n;i++){
a[i]+=a[i-1];
if(mp[a[i]]) ans+=mp[a[i]];
mp[a[i]]++;
}
pll(ans);
mp.clear();
}
//PAUSE;
return 0;
}

AtCoder Regular Contest 119 C - ARC Wrecker 2(同余定理+思维)的更多相关文章

  1. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  2. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  3. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  4. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  5. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

  6. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

  7. AtCoder Regular Contest 102

    AtCoder Regular Contest 102 C - Triangular Relationship 题意: 给出n,k求有多少个不大于n的三元组,使其中两两数字的和都是k的倍数,数字可以重 ...

  8. AtCoder Regular Contest 096

    AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...

  9. AtCoder Regular Contest 097

    AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...

随机推荐

  1. python进阶(17)协程

    协程 协程(Coroutine),又称微线程,纤程.(协程是一种用户态的轻量级线程)   作用:在执行 A 函数的时候,可以随时中断,去执行 B 函数,然后中断B函数,继续执行 A 函数 (可以自动切 ...

  2. Java | 使用OpenFeign管理多个第三方服务调用

    背景 最近开发了一个统一调度类的项目,需要依赖多个第三方服务,这些服务都提供了HTTP接口供我调用. 服务多.接口多,如何进行第三方服务管理和调用就成了问题. 常用的服务间调用往往采用zk.Eurek ...

  3. BLE链路层空中包格式

    空中包格式 BLE链路层的空中包格式非常简单,它所有的空中包都遵循下图所示的格式: 有上图可见,BLE空中包由4个部分组成,他们分别是: 前导码(Preamble) 访问地址(Access Addre ...

  4. 从苏宁电器到卡巴斯基第25篇:难忘的三年硕士时光 III

    阴差阳错,走上了讲师的道路 时间已经来到了2015年的1月,我的找工作之路也是屡败屡战,屡战屡败.金山.百度以及腾讯不约而同地不要我,使得我对于自己的未来充满了迷茫.尽管才研二而已,可是对于我这种没有 ...

  5. ZOJ3715 竞选班长求最小花费

    题意:       有n个小朋友竞选班长,一号想当班长,每个人都必须选择一个人当班长,并且不可以选择自己,并且每个人都有一个权值ai,这个权值就是如果1想让这个人改变主意选择自己当班长就得给他ai个糖 ...

  6. 【JavaScript】Leetcode每日一题-矩形区域不超过K的最大值和

    [JavaScript]Leetcode每日一题-矩形区域不超过K的最大值和 [题目描述] 给你一个 m x n 的矩阵 matrix 和一个整数 k ,找出并返回矩阵内部矩形区域的不超过 k 的最大 ...

  7. 6 JDBC

    JDBC 理解图 需要mysql包 下载官网:https://downloads.mysql.com/archives/c-j/ 第一个JDBC项目 创建一个java项目,一路next 导入jar包 ...

  8. Codeforces Round #688 (Div. 2)

    A. Cancel the Trains 题意:给定两个数组,找出这两个数组中有多少重复元素,然后输出 思路:直接找 代码: 1 #include<iostream> 2 #include ...

  9. 射线与空间内三角形的相交检测算法(Möller-Trumbore)的推导与实践

    背景介绍(学习算法之前需要先了解) 射线与空间内三角形的相交检测是游戏程序设计中一个常见的问题,最典型的应用就是拾取(Picking),本文介绍一个最常见的方法,这个方法也是DirectX中采用的方法 ...

  10. phpstudy2018 开启目录浏览

    废话不多说直接开始 一.打开 vhosts-ini 配置文件 二.加入以下内容  注意填写自己的网站根目录 <Directory "你自己的网站根目录"> Option ...