The Number of Products

给定一个非零整数序列,计算序列中子序列乘积为负和为正的指数对数量。通过分析正负序列的组合,使用前缀和和奇偶桶策略来高效解决问题。
摘要由CSDN通过智能技术生成

给定一个序列a1,a2,…,由n个非零整数组成(即ai≠0)。
请计算以下两个值:
指数对(l,r)(l≤r)的数目,使得al⋅al+1…ar−1⋅ar为负;
指数对(l,r)(l≤r)的数目,使得al⋅al+1…ar−1⋅ar为正;

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

The second line contains n integers a1,a2,…,an (−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

分析:先考虑全部为正的序列,则正积段为n+(n-1)+…+1种取法,所以正负积段总和为n * (n + 1) / 2,求出正积段即可求出负积段

在此基础上,我们只考虑正负,不考虑乘积的结果,所以令正数为0负数为1,用前缀和统计奇偶,维护两个桶前缀和分别为奇数和偶数的个数,

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=2e5+5;
int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值