The Number of Products
You are given a sequence a1,a2,…,ana1,a2,…,an consisting of nn integers.
You have to calculate three following values:
- 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;
- 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 zero;
- 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)(−109≤ai≤109) — the elements of the sequence.
Output
Print three integers — the number of subsegments with negative product, the number of subsegments with product equal to zero and the number of subsegments with positive product, respectively.
Examples
Input
5 5 -3 3 -1 0
Output
6 5 4
Input
10 4 0 -4 3 1 2 -4 3 0 3
Output
12 32 11
Input
5 -1 -2 -3 -4 -5
Output
9 0 6
Note
In the first example there are six subsegments having negative products: (1,2)(1,2), (1,3)(1,3), (2,2)(2,2), (2,3)(2,3), (3,4)(3,4), (4,4)(4,4), five subsegments having products equal to zero: (1,5)(1,5), (2,5)(2,5), (3,5)(3,5), (4,5)(4,5), (5,5)(5,5), and four subsegments having positive products: (1,1)(1,1), (1,4)(1,4), (2,4)(2,4), (3,3)(3,3).
简单dp
#include<bits/stdc++.h>
using namespace std;
long long dp[200050][2],a[200050];
//dp[i][0]以当前数字为结尾的负数序列
//dp[i][1]以当前数字为结尾的正数序列
int main()
{
long long i,j,m,n,ans1,ans2,ans3;
scanf("%lld",&n);
for(i = 1; i <= n; i++)
{
scanf("%lld",&a[i]);
}
for(i = 1; i <= n; i++)
{
if(a[i]==0)
dp[i][0] = dp[i][1] = 0;
else if(a[i]<0)
{
dp[i][0] = dp[i-1][1]+1;
dp[i][1] = dp[i-1][0];
}
else
{
dp[i][0] = dp[i-1][0];
dp[i][1] = dp[i-1][1]+1;
}
}
ans1 = ans2 = ans3 = 0;
for(i = 1; i <=n; i++)
{
ans1 += dp[i][0];
ans2+= dp[i][1];
}
ans3 = n*(n+1)/2 -ans1-ans2;
printf("%lld %lld %lld\n",ans1,ans3,ans2);
return 0;
}