(奇数区间)给定一个长度为 n 的数列:𝑎1, 𝑎2, ⋯ 𝑎𝑛,如果其中一段连续的子序列𝑎𝑖 , 𝑎𝑖+1, ⋯ 𝑎𝑗 (𝑖 ≤ 𝑗)中,奇数比偶数多,我们就称这个区间[i,j] 是奇数区间。求给定的 n个数中有多少个奇数区间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include<bits/stdc++.h> using namespace std; const int off = 1e6 + 1; const int maxn = 1e6 + 5; int a[maxn], s[maxn], c[2 * maxn]; int n; long long ans; int lowbit(int x){ return x & -x; } long long getSum(int x){ long long res = 0; while(x > 0){ res += c[x]; ① ; } return res; } void add(int x, int k){ while( ② ){ c[x] += k; ③ ; } } int main(){ cin >> n; add(off, 1); for(int i = 1; i <= n; i++){ cin >> a[i]; s[i] = ④ ; ans += getSum( ⑤ ); add(s[i] + off,1); } cout << ans << endl; return 0; } |