From 6b4a50ae30bd4d3e8e68c9a567066616a403a7d2 Mon Sep 17 00:00:00 2001 From: Sovagya-mallick <163491392+Sovagya-mallick@users.noreply.github.com> Date: Thu, 25 Jun 2026 13:01:49 +0530 Subject: [PATCH] Python sol --- .../3700.Number of ZigZag Arrays II/README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/solution/3700-3799/3700.Number of ZigZag Arrays II/README.md b/solution/3700-3799/3700.Number of ZigZag Arrays II/README.md index 7560b51104e99..36c75f374d2b3 100644 --- a/solution/3700-3799/3700.Number of ZigZag Arrays II/README.md +++ b/solution/3700-3799/3700.Number of ZigZag Arrays II/README.md @@ -97,6 +97,39 @@ source: 第 469 场周赛 Q4 ```python +class BinaryIndexedTree: + __slots__ = "n", "c" + + def __init__(self, n: int): + self.n = n + self.c = [0] * (n + 1) + + def update(self, x: int, delta: int) -> None: + while x <= self.n: + self.c[x] += delta + x += x & -x + + def query(self, x: int) -> int: + s = 0 + while x: + s += self.c[x] + x -= x & -x + return s + + +class Solution: + def countMajoritySubarrays(self, nums: List[int], target: int) -> int: + n = len(nums) + tree = BinaryIndexedTree(n * 2 + 1) + s = n + 1 + tree.update(s, 1) + ans = 0 + for x in nums: + s += 1 if x == target else -1 + ans += tree.query(s - 1) + tree.update(s, 1) + return ans + ``` #### Java