<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 Solution {
public:
    vector<tuple<int,int,int>> sagment;
    void BuildSagment(int ind,int l,int r,string& s)
    {
        if(l == r)
        {
            auto t = make_tuple(1,1,1);
            sagment[ind] = t;
            return;
        }
        int mid = l + (r-l)/2;
        BuildSagment(2*ind+1,l,mid,s);
        BuildSagment(2*ind+2,mid+1,r,s);

        int fa,ma,la,pfa,pma,pla,fb,mb,lb;

        tie(fa,ma,la) = sagment[2*ind+1];
        tie(pfa,pma,pla) = sagment[2*ind+1];
        tie(fb,mb,lb) = sagment[2*ind+2];

        if(s[mid] == s[mid+1])
        {
            // all same
            if(fa == ma && ma == la && mid-l+1 == ma)
            {
                fa+=fb;
                ma+=fb;
                la+=fb;
            }
            else if(ma == la)
            {
                la+=fb;
                ma+=fb;
            }
            else
            {
                la+=fb;
                ma = max(la,ma);
            }

            if(fb == mb && mb == lb && r-mid == mb)
            {
                fb+=pla;
                mb+=pla;
                lb+=pla;
            }
            else if(mb == fb)
            {
                fb+=pla;
                mb+=pla;
            }
            else
            {
                fb+=pla;
                mb=max(mb,fb);
            }

            auto t = make_tuple(1,1,1);
            get<1>(t) = max(ma,mb);
            get<0>(t) = fa;
            get<2>(t) = lb;
            sagment[ind] = t;
        }
        else
        {

            auto t = make_tuple(1,1,1);
            get<1>(t) = max(ma,mb);
            get<0>(t) = fa;
            get<2>(t) = lb;
            sagment[ind] = t;
        }
        return;
    }

    void update(int ind,int l,int r,int keyInd,char val,string& s)
    {
        if(l == r && l == keyInd)
        {
            s[keyInd] = val;
            auto t = make_tuple(1,1,1);
            sagment[ind] = t;
            return;
        }
        int mid = l + (r-l)/2;
        if(mid >= keyInd)
        {
            update(2*ind+1,l,mid,keyInd,val,s);
        }
        else
        {
            update(2*ind+2,mid+1,r,keyInd,val,s);
        }
        int fa,ma,la,pfa,pma,pla,fb,mb,lb;
        tie(fa,ma,la) = sagment[2*ind+1];
        tie(pfa,pma,pla) = sagment[2*ind+1];
        tie(fb,mb,lb) = sagment[2*ind+2];

        if(s[mid] == s[mid+1])
        {
            if(fa == ma && ma == la && mid-l+1 == ma)
            {
                fa+=fb;
                ma+=fb;
                la+=fb;
            }
            else if(ma == la)
            {
                la+=fb;
                ma+=fb;
            }
            else
            {
                la+=fb;
                ma = max(la,ma);
            }

            if(fb == mb && mb == lb && r-mid == mb)
            {
                fb+=pla;
                mb+=pla;
                lb+=pla;
            }
            else if(mb == fb)
            {
                fb+=pla;
                mb+=pla;
            }
            else
            {
                fb+=pla;
                mb=max(mb,fb);
            }

            auto t = make_tuple(1,1,1);
            get<1>(t) = max(ma,mb);
            get<0>(t) = fa;
            get<2>(t) = lb;
            sagment[ind] = t;
        }
        else
        {

            auto t = make_tuple(1,1,1);
            get<1>(t) = max(ma,mb);
            get<0>(t) = fa;
            get<2>(t) = lb;
            sagment[ind] = t;
        }
        return;
    }

    void print()
    {
        for(int i=0;i<13;i++)
        {
            cout << "<" << get<0>(sagment[i]) << "," << get<1>(sagment[i]) << "," << get<2>(sagment[i]) << "> "; 
        }
        cout << endl;
    }
    
    vector<int> longestRepeating(string s, string queryCharacters, vector<int>& queryIndices) {
        int sLen = s.length();
        sagment.resize(4*sLen);
        BuildSagment(0,0,sLen-1,s);    
        vector<int> ans;
        int q = queryIndices.size();
        // print();
        for(int i=0;i<q;i++)
        {
            update(0,0,sLen-1,queryIndices[i],queryCharacters[i],s);
            // print();
            ans.push_back(get<1>(sagment[0]));
        }
        return ans;
    }
};
92400