CF 1215 B The Number of Products (thinking title)

Links: https://codeforces.com/contest/1215/problem/B

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

You have to calculate two following values:

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

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

The second line contains nn integers a1,a2,,ana1,a2,…,an (109ai109;ai0)(−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.

 

The meaning of problems: seeking a product of the number of sub-segments of negative and positive numbers (not 0)

Solution: positive number of 0, a negative number, and statistical prefix (prefix is ​​required and parity), then swept from the beginning again, maintaining the tub 2 and is the number of prefixes and prefix number is odd and even.

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=2e5+5;
int n;
int a[maxn], sum[maxn], cnt[2];
long long pos, neg;
 
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        if(a[i]>0) A [I] = 0 ;
         the else A [I] = . 1 ; 
    } 
    for ( int I = . 1 ; I <= n-; I ++ ) 
        SUM [I] = SUM [I- . 1 ] + A [I];
     for ( int I = 1 ; I <= n-; I ++ ) 
        SUM [I] = SUM [I]% 2 ; 
    CNT [ 0 ] ++;                  // note cnt0 initialized to 1, indicating that the interval length of 1 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        IF (SUM [I]) { 
            POS+=cnt[1];
            neg+=cnt[0];
        }
        else{
            pos+=cnt[0];
            neg+=cnt[1];
        }
        cnt[sum[i]]++;
    }
    cout<<neg<<" "<<pos;
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Yokel062/p/11613839.html
Recommended