You are given a string of size n consisting of 0s and/or 1s. You have to perform total k queries and there are two types of queries possible:
“1” (without quotes): Print length
of the longest sub-array which
consists of all ‘1’.
“2 X” (without quotes): where X is
an integer between 1 to n. In this
query, you will change character at
the Xth position to ‘1’ (It is
possible that the character at i-th
position was already ‘1’).
Input Format
First line of input contains n and k, where n is the length of the string, k is the number of queries.
Next line contains a string of 0’s and/or 1’s of length n.
Each of next k lines contains query of any one type(i.e 1 or 2).
Input Constraints
1≤n,k≤10^5
1≤X≤n
String contains only 0s and/or 1s.
Each query is of one of the mentioned
types.
Output Format
For each query of type 1, print in a new line the maximum size of the subarray with all 1’s
Sample Input
5 7
00000
1
2 3
1
2 5
1
2 4
1
Sample Output
0
1
1
3
Explanation
Initially there are no 1’s in the string, hence the answer is 0.
In second query of type ‘1’ state of string is 00100, hence answer is 1.
In Third query of type ‘1’ state of string is 00101, hence answer is 1.
In Fourth query of type ‘1’ state of string is 00111, hence answer is 3.
Can someone tell me how to approach this question?
I am not completely sure if this works or not, but we can simply edit all 0’s to -INF(-1e6) and apply maximum subarray sum using Segment Tree like in this problem(www.spoj.com/problems/GSS1)!
The idea is to maintain start and end of the block(the sub-array having all 1’s) and when any update comes just see the previous position and the next position. If any of them is 1 then update start and end accordingly.
@pk301 If i am not wrong your solution is n^2. (Correct me if I am wrong)
Test case :
n = 1e5; (string containing all zeroes)
q = 1e5;
for(int i = q; i > 0; --i) {
query of second type
2 i
}