Good Subarrays Count Algorithm

बनाया गया: 17 जनवरी 2025

का उपयोग करके उत्तर दिया गया O1 द्वारा Chat01

प्रश्न

An array b
of m
integers is called good if, when it is sorted, b⌊m+12⌋=b⌈m+12⌉
. In other words, b
is good if both of its medians are equal. In particular, ⌊m+12⌋=⌈m+12⌉
when m
is odd, so b
is guaranteed to be good if it has an odd length.

You are given an array a
of n
integers. Calculate the number of good subarrays∗
in a
.


An array x
is a subarray of an array y
if x
can be obtained from y
by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Input
Each test contains multiple test cases. The first line contains the number of test cases t
(1≤t≤104
). The description of the test cases follows.

The first line of each test case contains a single integer n
(1≤n≤105
) — the length of the array.

The second line of each test case contains n
integers a1,a2,…,an
(1≤ai≤10
) — the given array.

It is guaranteed that the sum of n
over all test cases does not exceed 105
.

Output
For each test case, output a single integer representing the number of good subarrays in a
.

Example
InputCopy
3
4
1 1 1 1
5
1 10 2 3 3
10
6 3 2 3 5 3 4 2 3 5
OutputCopy
10
11
42
Note
In the first case, every subarray is good since all its elements are equal to 1
.

In the second case, an example of a good subarray is b=[10,2,3,3]
. When it is sorted, b=[2,3,3,10]
, so b⌊4+12⌋=b⌈4+12⌉=b2=b3=3
. Another example would be b=[1,10,2]
. On the other hand, b=[1,10]
is not good as its two medians are 1
and 10
, which are not equal.


solve it by dinary serch in c++ , or BIT

विचार

Analyzing subarray properties

I’m working through subarray characteristics to identify "good" subarrays. For odd-length subarrays, they automatically qualify as good. With even-length subarrays, the two medians must match.

Analyzing even-length subarrays

For subarrays with even length, ensuring the two medians match requires that half of the elements are ≤ X and half are ≥ X. This is the key characteristic to identify good subarrays.

Ensuring consistent medians

By defining a pivot "x" with at least half the elements being ≤ x and half ≥ x, I ensure the subarray's medians coincide, making it good.

Identifying the key element

For an even-length subarray to be good, at least one value must appear at least k+1 times, ensuring the medians coincide. This simplifies the process significantly.

Tracking frequency patterns

I’m thinking carefully about using a sliding window to count elements with frequencies ≥ (window_length//2 + 1). This technique helps identify potential median-matching subarrays effectively.

Calculating subarray counts

I’m thinking through the total subarray count, then subtracting the odd-length ones to focus on even-length subarrays. The goal is to identify those with high-frequency elements.

Calculating subarray frequencies

First, total subarrays: n(n+1)2\frac{n(n+1)}{2}. Then, odd-length subarrays: sum of (nk+1)(n-k+1) for odd kk. Leave even-length subarrays for next.

Considering subarray patterns

I'm exploring subarrays by their start and end indices, focusing on those with the same parity. This method might provide a fresh perspective on the problem.

Transforming the array

I’m planning to modify the original array to isolate subarrays with a majority element. This approach should simplify counting and identifying the desired subarrays effectively.

Transforming the array

I’m marking each element as +1 or -1 to distinguish them, then using a partial sum array to find even-length subarrays with a sum greater than half their length.

Transforming subarray sums

I’m refining the condition by transforming the array into c[i]=partialSum(i)×2ic[i] = \text{partialSum}(i) \times 2 - i. This adjustment helps to ensure sums are greater than half the subarray length.

Mapping out subarray sums

I’m setting up b[i]=1b[i] = 1 if a[i]=ea[i] = e and -1 otherwise. Then, calculating the partial sum prefix array p[i]p[i] simplifies identifying valid subarrays.

Mapping the strategy

I’m calculating subarrays using b[i]b[i] and p[i]p[i], ensuring the sum condition is satisfied for even lengths. This approach helps to track progress efficiently.

Adjusting my approach

I’m simplifying the condition by defining r[i]=2p[i]ir[i] = 2p[i] - i, ensuring the sum of specific subarray elements is greater than half the subarray's length for even lengths.

Refining the criteria

I’m focusing on transforming the array to ensure subarrays of even lengths have a sum greater than half their length, effectively identifying the desired properties for good subarrays.

Counting occurrences

I’m digging into media by defining b[i]b[i] for range 1 to 10, calculating prefix sums p[i]p[i], and verifying if their frequency meets the criteria.

Tracking progress

I'm piecing together subarray frequencies and making sure sums meet criteria for even lengths. This helps identify subarrays that are good, simplifying the overall approach.

Assessing subarray patterns

Hmm, I'm thinking about counting odd-length subarrays and subtracting even-length ones. This makes me consider the overall strategy to tackle the problem more effectively.

Breaking down the formula

I’m thinking through the formula for counting subarrays by focusing on odd and even lengths. This leads me to a more efficient method for working with large values of n.

Refining the approach

I’m working out the formula for odd and even-length subarrays. Then, I’ll use prefix sums to pinpoint even-length subarrays with a majority element, ensuring no double counting.

Ensuring unique counts

I’m confirming that each majority element subarray is counted just once, avoiding overlaps by summing distinct sets of even-length subarrays. This simplifies the process and enhances accuracy.

Transforming and categorizing

I’m mapping out the transformation of subarray sums and using Fenwick trees to categorize the count of even-length subarrays with a majority element, ensuring efficient processing.

Thought about good subarrays problem for 2m 17s

उत्तर

Analysis

We are given an integer array aa of length nn, and we want to count the number of good subarrays of aa. A subarray bb of length mm is called good if, after sorting bb, its two medians coincide. Formally,

b(m+1)/2  =  b(m+1)/2.b_{\lfloor (m+1)/2 \rfloor} \;=\; b_{\lceil (m+1)/2 \rceil}.

Equivalently:

  1. If mm is odd, then (m+1)/2=(m+1)/2,\lfloor (m+1)/2 \rfloor = \lceil (m+1)/2 \rceil, so any odd-length subarray is automatically good.
  2. If mm is even, say m=2k,m = 2k, then we need the m2\tfrac{m}{2}-th and (m2+1)\bigl(\tfrac{m}{2} + 1\bigr)-th elements (in sorted order) to be equal. In more intuitive terms, there must be some value xx in that subarray that appears at least k+1k+1 times (i.e.\ m2+1\ge \tfrac{m}{2}+1 times).
    • Why? Because for a length-2k2k subarray to have the two medians the same, the “middle two” elements in sorted order must both be xx. That forces freq(x)k+1\text{freq}(x) \ge k+1.

Hence the subarray is good if:

  • it has odd length, or
  • it has even length and contains some “majority” element whose frequency is length2+1\ge \tfrac{\text{length}}{2} + 1.

Since each test can have up to n=105n = 10^5 (and many test cases, but with total n105\sum n \le 10^5), an O(n2)O(n^2) solution is too slow.

Below is a common, efficient strategy once we notice that:

  1. Counting all subarrays of odd length is easy with a direct formula.
  2. For the even-length subarrays, we only need to check if there is a majority element.
  3. Because the array elements aia_i are bounded (1ai101 \le a_i \le 10), a subarray cannot have two distinct majority elements simultaneously.
    • Indeed, in an even-length subarray of length 2k2k, to have freq(x)k+1\text{freq}(x) \ge k+1 and also freq(y)k+1\text{freq}(y) \ge k+1 (for xyx \neq y) would exceed 2k2k total.

Therefore we can “sum over each candidate x{1,,10}x\in \{1,\dots,10\}” the number of even-length subarrays in which xx is a majority. Because no even subarray can be majority for two different values at once, we will not double-count.

Finally, we add

  • all odd-length subarrays (which are automatically good),
  • plus all even-length subarrays that have some majority element x{1,,10}x\in\{1,\dots,10\}.

Step 1. Count all odd-length subarrays

The total number of subarrays of an nn-element array is

n(n+1)2.\frac{n\,(n+1)}{2}.

Among these, the number of odd-length subarrays is given by a known closed-form:

oddCount  =  n+12  ×  n+22.\text{oddCount} \;=\; \left\lfloor \frac{n+1}{2} \right\rfloor \;\times\; \left\lfloor \frac{n+2}{2} \right\rfloor.

You can verify this formula by small cases or by combinatorial arguments.


Step 2. Count the even-length subarrays that have a majority element

Let us focus on one candidate value x{1,,10}x \in \{1,\dots,10\}. Define a new array

b[i]  =  {1if a[i]=x,0otherwise.b[i] \;=\; \begin{cases} 1 & \text{if } a[i] = x,\\ 0 & \text{otherwise}. \end{cases}

Let p[i]p[i] be the prefix sum of b[i]b[i], i.e.\

p[0]=0,p[i]=b[1]+b[2]++b[i].p[0] = 0,\quad p[i] = b[1] + b[2] + \cdots + b[i].

Then for a subarray i..ji..j (1-based indices) of length L=ji+1L = j - i + 1,
freqx(a[i..j])=p[j]p[i1].\text{freq}_x\bigl(a[i..j]\bigr) = p[j] - p[i-1].

An even-length subarray i..ji..j (so LL is even, L=2kL=2k) has xx as a majority element if

freqx    k+1p[j]p[i1]    L2+1  =  k+1.\text{freq}_x \;\ge\; k + 1 \quad\Longleftrightarrow\quad p[j] - p[i-1] \;\ge\; \frac{L}{2} + 1 \;=\; k+1.

Since L=ji+1L = j - i + 1, the condition is

p[j]p[i1]    (ji+1)2+1.p[j] - p[i-1] \;\ge\; \frac{(j - i + 1)}{2} + 1.

We must also enforce that (ji+1)(j - i + 1) is even.

How to count these subarrays efficiently

A known trick is to rewrite the inequality in a form suitable for Fenwick/BIT or balanced-binary-search (such as std::map in C++ or similar). The usual approach:

  1. We only care about subarrays of even length, i.e.\ ji+10 (mod 2)j - i + 1\equiv 0\ (\text{mod }2).
    • Equivalently, ii and jj have the same parity (both even indices or both odd indices).
  2. Rewrite p[j]p[i1]  >  ji+121p[j] - p[i-1] \;>\; \frac{j - i + 1}{2} - 1 or something quite close. One can then do a prefix transformation (often something like 2p[i]i2\,p[i] - i) and keep track of partial sums, performing lookups in a Fenwick tree or an ordered map.

A simpler “two-scan” method (often used in problems with {1,+1}\{-1,+1\} arrays) might also work here, but one must handle the “length is even” part carefully.

However, because 1a[i]101 \le a[i] \le 10, we only have 10 candidate values. That means we can do the following for each x{1,,10}x\in\{1,\dots,10\}:

  • Build the prefix sum array p[i]p[i].
  • We want to count the number of pairs (i,j)(i,j) with iji \le j, same parity, and p[j]p[i1]    (ji+1)2+1.p[j] - p[i-1] \;\ge\; \frac{(j - i + 1)}{2} + 1.
  • Use a Fenwick (BIT) or an ordered container to do it in O(nlogn)O(n \log n) for each xx.
  • Since there are only 10 different xx-values, the total is 10×O(nlogn)10 \times O(n \log n), which is fine for n105n \le 10^5.

Collect the counts for each xx. Since no even subarray can have two distinct majorities, we can sum those counts without double-counting.


Putting it all together

  1. Compute oddCount\text{oddCount} = number of odd-length subarrays =

    n+12  ×  n+22. \left\lfloor \frac{n+1}{2} \right\rfloor \;\times\; \left\lfloor \frac{n+2}{2} \right\rfloor.
  2. For each candidate x{1,,10}x \in \{1,\dots,10\}:

    1. Build b[i]b[i] and prefix sums p[i]p[i].
    2. Count (using Fenwick or ordered structure) the number of pairs (i,j)(i,j) with
      • 1ijn1 \le i \le j \le n,
      • ji+1j - i + 1 is even (so ii and jj have the same parity),
      • p[j]p[i1](ji+1)2+1p[j] - p[i-1] \ge \tfrac{(j - i + 1)}{2} + 1.
    3. Let that count be cntx\text{cnt}_x.
  3. Sum up cntx\text{cnt}_{x} over all xx. Denote this total by evenMajorityCount\text{evenMajorityCount}.

  4. The final answer (number of good subarrays) is

    oddCount  +  evenMajorityCount. \text{oddCount} \;+\; \text{evenMajorityCount}.

You repeat the above per test case.


C++ Implementation Sketch (Fenwick / BIT)

Below is a (somewhat abbreviated) sketch. The key is to implement the counting step for each xx. There are various ways to handle “only even-length subarrays”; one common trick is:

  • Notice that “ji+1j - i + 1 is even”     \iffii and jj have the same parity.”
  • So, you can maintain two Fenwick trees: one for indices ii that are odd, and one for indices ii that are even. Or, you can separate them in your prefix array approach.

One straightforward pattern (among several) is:

  1. Define d[i]  =  2p[i]i,d[i] \;=\; 2 \cdot p[i] - i, and similarly d[0]=0d[0] = 0.
  2. You want subarray (i,j)(i,j) with “same parity” and p[j]p[i1]    (ji+1)2+1        2(p[j]p[i1])    (ji+1)+2        (2p[j]j)(2p[i1](i1))    2.p[j] - p[i-1] \;\ge\; \frac{(j - i + 1)}{2} + 1 \;\;\Longleftrightarrow\;\; 2(p[j] - p[i-1]) \;\ge\; (j - i + 1) + 2 \;\;\Longleftrightarrow\;\; \bigl(2p[j] - j\bigr) - \bigl(2p[i-1] - (i-1)\bigr) \;\ge\; 2. That is d[j]d[i1]    2+((i1)j). d[j] - d[i-1] \;\ge\; 2 + \bigl((i-1) - j\bigr). …and then you handle the “same parity” by ensuring either both ii and jj are even or both odd.

Because there are multiple ways to code the details, below is a more “direct” counting approach, using a Fenwick tree keyed by the values of d[i]\,d[i]. (If you find the offset arithmetic too fiddly, you can store d[i]\,d[i] in an std::map<long long,int> as you iterate from left to right, and do an order-statistic or cumulative-frequency approach.)

#include <bits/stdc++.h> using namespace std; // Fenwick (BIT) for frequencies of indices/values. struct Fenwick { int N; vector<int> fenw; Fenwick(int n) : N(n), fenw(n+1,0) {} void update(int i,int v){ for(; i<=N; i+=(i & -i)) fenw[i]+=v; } int query(int i){ int s=0; for(; i>0; i-=(i & -i)) s+=fenw[i]; return s; } // query range [1..i] }; // You will also need coordinate compression of d[i] values // if they can be large, so you can store them in Fenwicks. int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ int n; cin >> n; vector<int> a(n+1); for(int i=1; i<=n; i++){ cin >> a[i]; } // 1) Count all odd-length subarrays long long totalSub = 1LL*n*(n+1)/2; // # of odd subarrays = floor((n+1)/2)*floor((n+2)/2) long long oddCount = 1LL*((n+1)/2)*((n+2)/2); long long ans = oddCount; // all odd-length are already good // 2) For each x in [1..10], count even-length subarrays that have freq(x)>= (L/2 +1) long long evenMajorityCount = 0; for(int x=1; x<=10; x++){ // Build b[i], prefix p[i] vector<int> b(n+1), p(n+1,0); for(int i=1; i<=n; i++){ b[i] = (a[i]==x ? 1 : 0); p[i] = p[i-1] + b[i]; } // We want to count number of pairs (i,j), i<=j, same parity, s.t. // p[j] - p[i-1] >= ( (j - i + 1)/2 ) + 1. // We'll implement a Fenwicks-based or map-based counting. // [Pseudocode or actual detailed code to do the counting...] // You can store d[i] = 2*p[i] - i in two separate maps: one for i even, one for i odd, // or use a single structure but watch the parity constraints carefully. long long countForX = 0; // ... implement the counting logic ... evenMajorityCount += countForX; } ans += evenMajorityCount; cout << ans << "\n"; } return 0; }

The precise details of the Fenwick or binary-search logic can vary. One simpler approach, since n105n\le10^5 and we only do it 10 times, is to store partial sums in an std::vector<long long>, do coordinate-compression for the d[i]d[i]-values, and then use two Fenwicks (one for odd indices, one for even). As you iterate jj from 0 to nn, you look up how many ii-indices in the Fenwicks satisfy

d[i]    d[j]2(because you want d[j]d[i]2), d[i] \;\le\; d[j] - 2 \quad\text{(because you want }d[j] - d[i]\ge2\text{),}

with the additional constraint that ii and jj have same parity. Then update Fenwicks with d[j]d[j].

However you implement it, the core ideas remain:

  1. Count odd-length subarrays right away.
  2. For each possible majority candidate xx (only 10 of them!), count how many even-length subarrays make xx appear L2+1\ge \frac{L}{2}+1 times.
  3. Sum those counts, add to the odd-length subarray count.

That solves the problem in about O(10×nlogn)O(10 \times n \log n) time overall, which is acceptable for n105\sum n \le 10^5.


Key Takeaways

  • Odd-length subarrays are automatically good (both medians collapse into one index).
  • Even-length subarrays must have a value whose frequency is strictly more than half of the length (i.e.\ L2+1\ge \frac{L}{2}+1).
  • Because ai10a_i\le 10, no two distinct values can both be “strict majority” in the same subarray. So we can count each candidate separately and sum up.
  • Prefix-sum transformations plus Fenwicks (or balanced-BST / binary-search) are a common technique to handle “count subarrays whose prefix differences exceed some threshold.”

This yields a clean O(10×nlogn)O(10 \times n \log n) solution in C++.

इस प्रश्नोत्तर को साझा करें