https://vjudge.net/contest/365698#problem/J
l[i]表示以a[i]为右端点的为负数的情况,r[i]表示为整数。
转移就是这样:
if(a[i]<0){
l[i]=r[i-1]+1;
r[i]=l[i-1];
}
else{
l[i]=l[i-1];
r[i]=r[i-1]+1;
}
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) using namespace std; long long T; long long n,ans; long long a[N],l[N],r[N],x,y; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } signed main(){ in(n); For(i,1,n) in(a[i]); For(i,1,n){ if(a[i]<0){ l[i]=r[i-1]+1; r[i]=l[i-1]; } else{ l[i]=l[i-1]; r[i]=r[i-1]+1; } } For(i,1,n){ x+=l[i]; y+=r[i]; } o(x);p(' ');o(y); return 0; }