<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>

void build(int si,int ss,int se,vector<int>&seg,vector<int>&arr){
    if(ss==se){
        seg[si]=arr[ss]; return ;
    }
    int mid = (ss+se)/2;
    build(2*si+1,ss,mid,seg,arr);
    build(2*si+2,mid+1,se,seg,arr);
    seg[si] = max(seg[2*si+1],seg[2*si+2]);
}

void update(int si,int ss,int se, int l,int r, int diff, vector<int>&tree,vector<int>&lazy){
    if(lazy[si]!=0){
        tree[si] += lazy[si];
        if(ss!=se){
            lazy[2*si+1] += lazy[si];
            lazy[2*si+2] += lazy[si];
        }
        lazy[si]=0;
    }

    if(ss>se || se<l || r<ss) return ; // out of range

    if(ss>=l && se<=r){
        tree[si] += diff;
        if(ss!=se){
            lazy[2*si+1] += diff;
            lazy[2*si+2] += diff;
        }
        return ;
    }

    int mid = (ss+se)/2; 
    update(2*si+1,ss,mid,l,r,diff,tree,lazy);
    update(2*si+2,mid+1,se,l,r,diff,tree,lazy);
    tree[si] = max(tree[2*si+1],tree[2*si+2]);
}

int query(int si,int ss,int se, int l, int r,vector<int>&tree,vector<int>&lazy){
    if(lazy[si]!=0){
        tree[si] += lazy[si];
        if(ss!=se){
            lazy[2*si+1] += lazy[si];
            lazy[2*si+2] += lazy[si];
        }
        lazy[si]=0;
    }

    if(ss>se || l>se || r<ss) return 0;

    if(ss>=l && se<=r){
        return tree[si];
    }
    int mid = (ss+se)/2;
    int t1 = query(2*si+1,ss,mid,l,r,tree,lazy);
    int t2 = query(2*si+2,mid+1,se,l,r,tree,lazy);
    return max(t1,t2);
}

class Solution {
public:
    vector<int> longestRepeating(string s, string q1, vector<int>& v1) {
        int n = s.length();
        int m = q1.size();

        vector<int>sm1(n); sm1[0]=1;
        vector<int>sm2(n); sm2[n-1]=1;

        for(int i=1;i<n;i++) {
            if(s[i]==s[i-1]) sm1[i]=sm1[i-1]+1; else sm1[i]=1;
        }
        for(int i=n-2;i>=0;i--){
            if(s[i]==s[i+1]) sm2[i]=sm2[i+1]+1; else sm2[i]=1;
        }

        vector<int>seg1(4*n+4);
        vector<int>seg2(4*n+4);
        vector<int>lz1(4*n+4);
        vector<int>lz2(4*n+4);
        build(0,0,n-1,seg1,sm1); build(0,0,n-1,seg2,sm2);

        vector<int>ans(m,1);
        if(n==1) return ans;

        for(int i=0;i<m;i++){
            int i2 = v1[i]; char ch = q1[i];

            int k1,k2,k3; 
            k1 = query(0,0,n-1,i2,i2,seg1,lz1);
            if(i2>0) k2 = query(0,0,n-1,i2-1,i2-1,seg1,lz1);
            if(i2+1<n) k3 = query(0,0,n-1,i2+1,i2+1,seg2,lz2);

            int i1 = i2-k2; int i3 = i2+k3; 

            if(i2==0){
                if(s[i2]==s[i2+1] && ch!=s[i2]){
                    update(0,0,n-1,i2+1,i3,-1,seg1,lz1);
                    update(0,0,n-1,i2,i2,-k3,seg2,lz2);
                }
                else if(s[i2]!=s[i2+1] && ch==s[i2+1]){
                    update(0,0,n-1,i2+1,i3,1,seg1,lz1);
                    update(0,0,n-1,i2,i2,k3,seg2,lz2);
                }
            }
            else if(i2==n-1){
                if(s[i2]==s[i2-1] && ch!=s[i2]){
                    update(0,0,n-1,i2,i2,-k2,seg1,lz1);
                    update(0,0,n-1,i1,i2-1,-1,seg2,lz2);
                }
                else if(s[i2]!=s[i2-1] && ch==s[i2-1]){
                    update(0,0,n-1,i2,i2,k2,seg1,lz1);
                    update(0,0,n-1,i1,i2-1,1,seg2,lz2);
                }
            }

            else if(s[i2-1]==s[i2+1] && s[i2]!=s[i2-1] && ch==s[i2-1]){
                update(0,0,n-1,i2+1,i3,k2+1,seg1,lz1);
                update(0,0,n-1,i2,i2,k2,seg1,lz1);
                update(0,0,n-1,i1,i2-1,k3+1,seg2,lz2);
                update(0,0,n-1,i2,i2,k3,seg2,lz2);
            }

            else if(s[i2-1]==s[i2+1] && s[i2]==s[i2-1] && ch!=s[i2-1]){
                update(0,0,n-1,i2+1,i3,-k2-1,seg1,lz1);
                update(0,0,n-1,i2,i2,-k2,seg1,lz1);
                update(0,0,n-1,i1,i2-1,-k3-1,seg2,lz2);
                update(0,0,n-1,i2,i2,-k3,seg2,lz2);
            }

            else if(s[i2-1]!=s[i2+1] && s[i2]!=s[i2-1] && s[i2]!=s[i2+1] && ch==s[i2+1]){
                update(0,0,n-1,i2+1,i3,1,seg1,lz1);
                update(0,0,n-1,i2,i2,k3,seg2,lz2);
            }

            else if(s[i2-1]!=s[i2+1] && s[i2]!=s[i2-1] && s[i2]!=s[i2+1] && ch==s[i2-1]){
                update(0,0,n-1,i2,i2,k2,seg1,lz1);
                update(0,0,n-1,i1,i2-1,1,seg2,lz2);
            }

            else if(s[i2-1]!=s[i2+1] && s[i2]==s[i2+1] && ch==s[i2-1]){
                update(0,0,n-1,i2+1,i3,-1,seg1,lz1);
                update(0,0,n-1,i2,i2,k2,seg1,lz1);
                update(0,0,n-1,i1,i2-1,1,seg2,lz2);
                update(0,0,n-1,i2,i2,-k3,seg2,lz2);
            }

            else if(s[i2-1]!=s[i2+1] && s[i2]==s[i2+1] && ch!=s[i2-1] && ch!=s[i2+1]){
                update(0,0,n-1,i2+1,i3,-1,seg1,lz1);
                update(0,0,n-1,i2,i2,-k3,seg2,lz2);
            }

            else if(s[i2-1]!=s[i2+1] && s[i2]==s[i2-1] && ch==s[i2+1]){
                update(0,0,n-1,i2+1,i3,1,seg1,lz1);
                update(0,0,n-1,i2,i2,-k2,seg1,lz1);
                update(0,0,n-1,i1,i2-1,-1,seg2,lz2);
                update(0,0,n-1,i2,i2,k3,seg2,lz2);
                
            }

            else if(s[i2-1]!=s[i2+1] && s[i2]==s[i2-1] && ch!=s[i2-1] && ch!=s[i2+1]){
                update(0,0,n-1,i2,i2,-k2,seg1,lz1);
                update(0,0,n-1,i1,i2-1,-1,seg2,lz2);
            }


            s[i2]=ch;
            ans[i] = query(0,0,n-1,0,n-1,seg1,lz1);
        }

        return ans;
    }
};
103800