## Editorial for TLE '16 Contest 7 P3 - NOR

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: d

It is not necessary to use a segment tree or a sparse table to solve this problem. These approaches are overkill and run quite slowly. Binary search is also not necessary, and it increases execution time.

The first observation is that , where can be any expression. In particular, if , then the answer to the query is always .

To solve a query, it is only necessary to get the largest that satisfies both and . In case are all 0's, let . This information can be preprocessed and stored in an array in time (linear time). Now there are cases to consider:

• : In this case, all integers are equal to .
• : In this case, only the first integer is equal to . The rest are equal to .
• : In this case, , because and .

A query can be solved in (constant time). Make sure to print the correct answer for each case.

Time Complexity: