CF1878E.Iva & Pav

普及/提高-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 aa of nn elements.

Let's define f(l,r)=al & al+1 &…& arf(l, r) = a_l \ \& \ a_{l+1} \ \& \dots \& \ a_r (here &\& denotes the bitwise AND operation).

Note that f(l,r)f(l, r) is not defined when l>rl \gt r.

Iva also gave Pav qq queries.

Each query consists of 2 numbers, kk and ll, and she wants Pav to find the largest index rr (l≤r≤nl \le r \le n), such that f(l,r)≥kf(l, r) \ge k.

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

伊娃和帕夫是一对著名的塞尔维亚竞赛编程情侣。在塞尔维亚,人们称帕夫为“papuca”,因此他将实现伊娃的所有愿望。

伊娃给了帕夫一个包含 nn 个元素的数组 aa。

我们定义 f(l,r)=al & al+1 &…& arf(l, r) = a_l \ \& \ a_{l+1} \ \& \dots \& \ a_r(此处 &\& 表示按位与运算)。

注意:当 l>rl \gt r 时,f(l,r)f(l, r) 未定义。

伊娃还给了帕夫 qq 个查询。

每个查询包含两个数 kk 和 ll,她希望帕夫找出满足 f(l,r)≥kf(l, r) \ge k 的最大下标 rr(其中 l≤r≤nl \le r \le n)。

帕夫希望快速解决这个问题,以免让伊娃不高兴。他需要你的帮助。

输入格式

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

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

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of array aa.

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

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

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5. Also, it is guaranteed that the sum of qq over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 数组 aa 的元素。

每个测试用例的第三行包含一个整数 qq(1≤q≤1051 \le q \le 10^5)—— Iva 给 Pav 的查询数量。

每个测试用例接下来的 qq 行,每行包含两个数 ll 和 kk(1≤l≤n1 \le l \le n,1≤k≤1091 \le k \le 10^9)—— 子段的左边界,以及题目描述中所述的整数 kk。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。同时,保证所有测试用例的 qq 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each query output maximal index rr (l≤r≤nl \le r \le n) such that al & al+1 &…& ar ≥ ka_l \ \& \ a_{l+1} \ \& \dots \& \ a_r \ \ge \ k.

If such rr doesn't exist, output −1-1.

对于每个查询,输出最大的索引 rr(满足 l≤r≤nl \le r \le n),使得 al & al+1 &…& ar ≥ ka_l \ \& \ a_{l+1} \ \& \dots \& \ a_r \ \ge \ k。

如果这样的 rr 不存在,则输出 −1-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

说明/提示

In the first test case n=5n=5, and the array a=[15,14,17,42,34]a = [15, 14, 17, 42, 34]

The first query asks for the biggest index rr such that the f(1,r)≥7f(1, r) \ge 7.

f(1,1)=15, f(1,2)=14, f(1,3)=0 f(1,4)=0 f(1,5)=0f(1,1) = 15, \ f(1, 2) = 14, \ f(1,3)=0 \ f(1, 4)=0 \ f(1, 5)=0, so r=2r=2 is the answer.

The second query asks for f(2,r)≥15f(2, r) \ge 15. Since such rr doesn't exist, the answer is −1-1.

The third query asks for f(4,r)≥5f(4, r) \ge 5. f(4,4)=42, f(4,5)=34f(4, 4) = 42, \ f(4, 5) = 34, so r=5r=5 is the answer.

In the second test case n=5n=5, and the array a=[7,5,3,1,7]a= [7, 5, 3, 1, 7].

For the first query, f(1,r)≥7f(1, r) \ge 7.

f(1,1)=7, f(1,2)=5, f(1,3)=1, f(1,4)=1, f(1,5)=1f(1, 1)=7, \ f(1, 2)=5, \ f(1, 3) = 1, \ f(1,4) = 1, \ f(1, 5)=1, so the answer to this query is 11.

For the second query, f(5,r)≥7f(5, r) \ge 7.

f(5,5)=7f(5, 5) = 7, so the answer is 55.

For the third query, f(2,r)≥3f(2, r) \ge 3.

f(2,2)=5, f(2,3)=1, f(2,4)=1, f(2,5)=1f(2, 2) = 5, \ f(2, 3) = 1, \ f(2, 4) = 1, \ f(2, 5) = 1, so the answer is 22.

第一个测试用例中,n=5n=5,数组 a=[15,14,17,42,34]a = [15, 14, 17, 42, 34]。

第一个查询要求找出最大的下标 rr,使得 f(1,r)≥7f(1, r) \ge 7。

f(1,1)=15, f(1,2)=14, f(1,3)=0, f(1,4)=0, f(1,5)=0f(1,1) = 15,\ f(1, 2) = 14,\ f(1,3)=0,\ f(1, 4)=0,\ f(1, 5)=0,因此答案为 r=2r=2。

第二个查询要求找出满足 f(2,r)≥15f(2, r) \ge 15 的 rr。由于不存在这样的 rr,答案为 −1-1。

第三个查询要求找出满足 f(4,r)≥5f(4, r) \ge 5 的 rr。f(4,4)=42, f(4,5)=34f(4, 4) = 42,\ f(4, 5) = 34,因此答案为 r=5r=5。

第二个测试用例中,n=5n=5,数组 a=[7,5,3,1,7]a= [7, 5, 3, 1, 7]。

对于第一个查询,要求满足 f(1,r)≥7f(1, r) \ge 7。

f(1,1)=7, f(1,2)=5, f(1,3)=1, f(1,4)=1, f(1,5)=1f(1, 1)=7,\ f(1, 2)=5,\ f(1, 3) = 1,\ f(1,4) = 1,\ f(1, 5)=1,因此该查询的答案为 11。

对于第二个查询,要求满足 f(5,r)≥7f(5, r) \ge 7。

f(5,5)=7f(5, 5) = 7,因此答案为 55。

对于第三个查询,要求满足 f(2,r)≥3f(2, r) \ge 3。

f(2,2)=5, f(2,3)=1, f(2,4)=1, f(2,5)=1f(2, 2) = 5,\ f(2, 3) = 1,\ f(2, 4) = 1,\ f(2, 5) = 1,因此答案为 22。

输入解题思路,AI测评打分。不知道怎么写?

首页