题库练习 Iva & Pav
← 上一题 下一题 →

A16200 | Iva & Pav

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Iva and Pav are a famous Serbian competitive programming couple. In Serbia, they call Pav "papuca" and that's why he will make all of Iva's wishes come true.

Iva gave Pav an array $a$ of $n$ elements.

Let's define $f(l, r) = a_l \ \& \ a_{l+1} \ \& \dots \& \ a_r$ (here $\&$ denotes the [bitwise AND operation](http://tiny.cc/bitwise_and)).

Note that $f(l, r)$ is not defined when $l>r$ .

Iva also gave Pav $q$ queries.

Each query consists of 2 numbers, $k$ and $l$ , and she wants Pav to find the largest index $r$ ( $l \le r \le n$ ), such that $f(l, r) \ge k$ .

Pav wants to solve this problem fast because he doesn't want to upset Iva. He needs your help.

输入格式

The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases.

The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the length of array $a$ .

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^9$ ) — the elements of array $a$ .

The third line of each test case contains a single integer $q$ ( $1 \le q \le 10^5$ ) — the number of queries Iva gave Pav.

The next $q$ lines of each test case contains two numbers, $l$ and $k$ ( $1 \le l \le n$ , $1 \le k \le 10^9$ ) — the left bound for the subsegment, and the integer $k$ described in statement.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ . Also, it is guaranteed that the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$ .

输出格式

For each query output maximal index $r$ ( $l \le r \le n$ ) such that $a_l \ \& \ a_{l+1} \ \& \dots \& \ a_r \ \ge \ k$ .

If such $r$ doesn't exist, output $-1$ .

输入输出样例

输入 #1
3
5
15 14 17 42 34
3
1 7
2 15
4 5
5
7 5 3 1 7
4
1 7
5 7
2 3
2 2
7
19 20 15 12 21 7 11
4
1 15
4 4
7 12
5 7
输出 #1
2 -1 5 
1 5 2 2 
2 6 -1 5
C++ 编辑器
输入
输出