Under construction

Still being built. Things may change

Intelligence Exploration

Interview

Time limit4sMemory limit1024 MB

Summary
Given a 0/1 array, answer queries with the left-to-right implication value over each subarray from position l to r.
Level

Medium4 of 10

Topics
Array, Math, Implementation
Solved
No attempts yet

Problem

John is a researcher in artificial intelligence. He wants to analyze the reasoning ability of a new Limitless Logic Machine and check how well it understands when one statement implies another.

The experiment uses a long array aa of length nn consisting of zeroes and ones. Each query is given as a pair ll, rr, and the machine must compute the value of the implication over the subarray al→al+1→⋯→ara_l \to a_{l+1} \to \cdots \to a_r. You must compute the correct answers to these queries so that John can validate the machine.

The implication operator →\to is defined as follows.

xxyyx→yx \to y
000011
001111
110000
111111

The operations are evaluated from left to right, so the order is ((…((al→al+1)→al+2)→… )→ar)((\dots((a_l \to a_{l+1}) \to a_{l+2}) \to \dots) \to a_r).

Input

The first line contains an integer nn (2≤n≤1052 \leq n \leq 10^5), the size of the array.

The second line contains nn integers aa (a∈{0,1}a \in \{0, 1\}), the values in the array.

The third line contains an integer qq (1≤q≤1051 \leq q \leq 10^5), the number of queries.

Each of the next qq lines contains two integers ll and rr (1≤l<r≤n1 \leq l < r \leq n), describing a query.

Output

For each query, print the result of the corresponding implication on its own line.

Examples2

  1. Example 1

    Input
    5
    0 0 1 1 0
    5
    1 2
    2 3
    4 5
    3 4
    1 5
    
    Expected output
    1
    1
    0
    1
    0
    
  2. Example 2

    Input
    7
    1 0 0 0 1 0 0
    5
    1 3
    4 7
    2 4
    3 6
    1 7
    
    Expected output
    1
    1
    0
    0
    1