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;
}

最新文章

  1. Visual Studio:error MSB8020(搬运)
  2. 详解Maple中的基础工具栏
  3. 【hihoCoder】1033: 交错和
  4. EasyUI DataGrid getChecked/getSelections 获取不到数据
  5. JS正则表达式验证数字
  6. 关于ScrollView和listview的冲突关于的滑动和宽度
  7. supersocket+controller+action
  8. 使用out来返回多个值
  9. Hibernate学习笔记--映射配置文件详解
  10. mysql sql语句大全(转载)
  11. 每天学习点js
  12. icns图标的制作
  13. PHP迭代器:Iterator和IteratorAggregate
  14. IT这条路,适合什么人走。
  15. 自己用纯C++实现简单的QT中信号与槽机制
  16. [UE4]C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例
  17. JavaScript document和window属性及方法详解
  18. 微信授权,openid 分享
  19. iOS边练边学--通知机制和键盘处理
  20. Android中Application类的使用

热门文章

  1. Prometheus【node_exporter】+grafana监控云主机
  2. ArrayList扩容机制以及底层实现
  3. go gin框架和springboot框架WEB接口性能对比
  4. 病毒木马查杀实战第017篇:U盘病毒之专杀工具的编写
  5. LA2965侏罗纪(异或和为0的最大数字个数)
  6. POJ1698 最大流或者匈牙利
  7. Windows Pe 第三章 PE头文件(下)
  8. Win64 驱动内核编程-26.强制结束进程
  9. layui select 动态赋值
  10. ConcurrentHashMap源码解读一