<p>You are given a <strong>0-indexed</strong> string <code>s</code>. You are also given a <strong>0-indexed</strong> string <code>queryCharacters</code> of length <code>k</code> and a <strong>0-indexed</strong> array of integer <strong>indices</strong> <code>queryIndices</code> of length <code>k</code>, both of which are used to describe <code>k</code> queries.</p>

<p>The <code>i<sup>th</sup></code> query updates the character in <code>s</code> at index <code>queryIndices[i]</code> to the character <code>queryCharacters[i]</code>.</p>

<p>Return <em>an array</em> <code>lengths</code> <em>of length </em><code>k</code><em> where</em> <code>lengths[i]</code> <em>is the <strong>length</strong> of the <strong>longest substring</strong> of </em><code>s</code><em> consisting of <strong>only one repeating</strong> character <strong>after</strong> the</em> <code>i<sup>th</sup></code> <em>query</em><em> is performed.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;babacc&quot;, queryCharacters = &quot;bcb&quot;, queryIndices = [1,3,3]
<strong>Output:</strong> [3,3,4]
<strong>Explanation:</strong> 
- 1<sup>st</sup> query updates s = &quot;<u>b<strong>b</strong>b</u>acc&quot;. The longest substring consisting of one repeating character is &quot;bbb&quot; with length 3.
- 2<sup>nd</sup> query updates s = &quot;bbb<u><strong>c</strong>cc</u>&quot;. 
  The longest substring consisting of one repeating character can be &quot;bbb&quot; or &quot;ccc&quot; with length 3.
- 3<sup>rd</sup> query updates s = &quot;<u>bbb<strong>b</strong></u>cc&quot;. The longest substring consisting of one repeating character is &quot;bbbb&quot; with length 4.
Thus, we return [3,3,4].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abyzz&quot;, queryCharacters = &quot;aa&quot;, queryIndices = [2,1]
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong>
- 1<sup>st</sup> query updates s = &quot;ab<strong>a</strong><u>zz</u>&quot;. The longest substring consisting of one repeating character is &quot;zz&quot; with length 2.
- 2<sup>nd</sup> query updates s = &quot;<u>a<strong>a</strong>a</u>zz&quot;. The longest substring consisting of one repeating character is &quot;aaa&quot; with length 3.
Thus, we return [2,3].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
	<li><code>k == queryCharacters.length == queryIndices.length</code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
	<li><code>queryCharacters</code> consists of lowercase English letters.</li>
	<li><code>0 &lt;= queryIndices[i] &lt; s.length</code></li>
</ul>

class SegmentTree:
    def __init__(self, data):
        self.nodes = [None] * 4 * len(data) #node: (value, left_node_index, right_node_index)
        self.data = data
        self._build_tree(0, 0, len(data)-1)

    def _left(self, index):
        return (index + 1) * 2 - 1

    def _right(self, index):
        return (index + 1) * 2

    def _build_tree(self, node_index, left_data_index, right_data_index):
        value = None
        if left_data_index == right_data_index:
            # (longest c, longest len, prefix c, prefix len, suffix c, suffix len)
            value = (
                self.data[left_data_index],
                1,
                self.data[left_data_index],
                1,
                self.data[left_data_index],
                1)
        else:
            left_node_index = self._left(node_index)
            right_node_index = self._right(node_index)

            mid_data_index = (left_data_index + right_data_index) // 2
            (left_lc, left_ll, left_pc, left_pl, left_sc, left_sl) = self._build_tree(left_node_index, left_data_index, mid_data_index)
            (right_lc, right_ll, right_pc, right_pl, right_sc, right_sl) = self._build_tree(right_node_index, mid_data_index + 1, right_data_index)
            
            lc = ll = None
            if left_ll > right_ll:
                lc = left_lc
                ll = left_ll
            else:
                lc = right_lc
                ll = right_ll
            if left_sc == right_pc and left_sl + right_pl > ll:
                ll = left_sl + right_pl
                lc = left_sc

            pc = left_pc
            pl = left_pl
            if left_pl == (mid_data_index - left_data_index + 1) and left_pc == right_pc:
                pl = left_pl + right_pl

            sc = right_sc
            sl = right_sl
            if right_sl == (right_data_index - mid_data_index) and left_sc == right_sc:
                sl = left_sl + right_sl
    
            value = (lc, ll, pc, pl, sc, sl)

        self.nodes[node_index] = value
        return value

    def _update(self, node_index, data_index, update_value, left_data_index, right_data_index):
        value = self.nodes[node_index]
        mid_data_index = (left_data_index + right_data_index) // 2
        left_node_index = self._left(node_index)
        right_node_index = self._right(node_index)

        new_value = None
        if left_data_index == right_data_index:
            new_value = (
                update_value,
                1,
                update_value,
                1,
                update_value,
                1)
        else:
            left_value = right_value = None
            if data_index <= mid_data_index:
                left_value = self._update(left_node_index, data_index, update_value, left_data_index, mid_data_index)
                right_value = self.nodes[right_node_index]
            else:
                left_value = self.nodes[left_node_index]
                right_value = self._update(right_node_index, data_index, update_value, mid_data_index + 1, right_data_index)
            (left_lc, left_ll, left_pc, left_pl, left_sc, left_sl) = left_value
            (right_lc, right_ll, right_pc, right_pl, right_sc, right_sl) = right_value
            
            lc = ll = None
            if left_ll > right_ll:
                lc = left_lc
                ll = left_ll
            else:
                lc = right_lc
                ll = right_ll
            if left_sc == right_pc and left_sl + right_pl > ll:
                ll = left_sl + right_pl
                lc = left_sc

            pc = left_pc
            pl = left_pl
            if left_pl == (mid_data_index - left_data_index + 1) and left_pc == right_pc:
                pl = left_pl + right_pl

            sc = right_sc
            sl = right_sl
            if right_sl == (right_data_index - mid_data_index) and left_sc == right_sc:
                sl = left_sl + right_sl
    
            new_value = (lc, ll, pc, pl, sc, sl)
        self.nodes[node_index] = new_value
        return new_value

    def update(self, data_index, update_value):
        self.data[data_index] = update_value
        return self._update(0, data_index, update_value, 0, len(self.data)-1)

class Solution:
    def longestRepeating(self, s: str, queryCharacters: str, queryIndices: List[int]) -> List[int]:
        st = SegmentTree(list(s))
        M = len(queryCharacters)
        ans = [0] * M
        for i in range(M):
            c = queryCharacters[i]
            p = queryIndices[i]
            st.update(p, c)
            ans[i] = st.nodes[0][1]
        return ans
3582