Блог пользователя Cpp_Writer

Автор Cpp_Writer, 2 месяца назад, По-английски

You are given an integer $$$N$$$, and a string $$$S$$$ of length $$$N$$$ that only contains W and L. How many substrings $$$[l,r]$$$ in $$$S$$$ that satisfies:

  • $$$S_l=S_r=\texttt{W}$$$

  • The value of the count of W in $$$S_{[l,r]}$$$ minus the count of L in $$$S_{[l,r]}$$$ is greater than or equal to $$$K$$$ ($$$K$$$ is an integer and $$$K>0$$$).

  • Проголосовать: нравится
  • -7
  • Проголосовать: не нравится

»
2 месяца назад, # |
  Проголосовать: нравится -27 Проголосовать: не нравится

I think this can be solved in O(N) only using two pointers.

»
2 месяца назад, # |
  Проголосовать: нравится +30 Проголосовать: не нравится

you can do prefix sums , then fix the l , count the r with the prefix sum > prefix sum of l + k

this can be done with segment tree or ordered set in nlogn

»
2 месяца назад, # |
  Проголосовать: нравится -17 Проголосовать: не нравится

maintain two fenwick trees / segment trees/ ordered sets, holding prefix sums ending at W or L

»
2 месяца назад, # |
  Проголосовать: нравится -19 Проголосовать: не нравится

I guess you can do sliding window and solve it in O(N)

»
2 месяца назад, # |
  Проголосовать: нравится -19 Проголосовать: не нравится

not nlog but do a prefix sum where W: 1 and L: -1 then you just 2pointer it