The Number of Products

The Number of Products

You are given a sequence a1,a2,…,ana1,a2,…,an consisting of nn non-zero integers (i.e. ai≠0ai≠0).

You have to calculate two following values:

  1. the number of pairs of indices (l,r)(l,r) (l≤r)(l≤r) such that al⋅al+1…ar−1⋅aral⋅al+1…ar−1⋅ar is negative;
  2. the number of pairs of indices (l,r)(l,r) (l≤r)(l≤r) such that al⋅al+1…ar−1⋅aral⋅al+1…ar−1⋅ar is positive;

Input

The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of elements in the sequence.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109;ai≠0)(−109≤ai≤109;ai≠0) — the elements of the sequence.

Output

Print two integers — the number of subsegments with negative product and the number of subsegments with positive product, respectively.

Examples

Input

5
5 -3 3 -1 1

Output

8 7

Input

10
4 2 -4 3 1 2 -4 3 2 3

Output

28 27

Input

5
-1 -2 -3 -4 -5

Output

9 6

题解及分析

AC代码:

#include<stdio.h>
#include<string.h>
//R[i]表示以i为结尾的正子列的个数,L[i]表示以i为结尾的负子列的个数
long long int n,a[200005],R[200005],L[200005],ansR=0,ansL=0;
int main()
{
    scanf("%lld",&n);
    for(long long int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        //将a[i]按照是否为负数分类,便于后面进行遍历
        if(a[i]<0) a[i]=1;
        else a[i]=0;
    }
    R[0]=0;
    L[0]=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i])//递推关系
        {
            L[i]=R[i-1]+1;
            R[i]=L[i-1];
        }
        else 
        {
            R[i]=R[i-1]+1;
            L[i]=L[i-1];
        }
        ansR+=R[i];//ansR表示前i个数组成的数列一共有ansR个正子列
        ansL+=L[i];//ansL表示前i个数组成的数列一共有ansL个负子列
    }
    printf("%lld %lld",ansL,ansR);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值