CF2161G.Bitwise And Equals

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

There is an array of integers a1,a2,…,ana_1, a_2, \ldots, a_n, and there is an integer XX.

You may perform the following operation zero or more times:

  • Select ii, and increase aia_i by one.

Let a′a' be the final state of the array. Your goal is to perform the operation above the smallest number of times such that a1′ & a2′ & … & an′=Xa'_1 \,\&\, a'_2 \,\&\, \ldots \,\&\, a'_n = X. &\& denotes the bitwise AND operation.

There are qq such query integers XX: X1,X2,…,XqX_1, X_2, \ldots, X_q. Compute the answer for each X=XiX = X_i. Note that all queries are processed separately and independently, from the same initial state aa.

给定一个整数数组 a1,a2,…,ana_1, a_2, \ldots, a_n 和一个整数 XX。

你可以执行以下操作零次或多次:

  • 选择下标 ii,并将 aia_i 的值加一。

设 a′a' 为数组的最终状态。你的目标是执行上述操作尽可能少的次数,使得 a1′ & a2′ & … & an′=Xa'_1 \,\&\, a'_2 \,\&\, \ldots \,\&\, a'_n = X。其中 &\& 表示按位与运算。

共有 qq 个这样的查询整数 XX:X1,X2,…,XqX_1, X_2, \ldots, X_q。请对每个 X=XiX = X_i 计算对应的答案。注意:所有查询均独立处理,且均从相同的初始数组 aa 出发。

输入格式

The first line contains integers nn and qq (2≤n≤200 0002 \le n \le 200\,000, 1≤q≤200 0001 \le q \le 200\,000) — the length of the array and the number of queries.

The second line contains integers a1,a2,…,ana_1, a_2, \ldots, a_n (for each ii, 0≤ai<2200 \le a_i \lt 2^{20}).

The next qq lines each contain a single integer XiX_i (0≤Xi<2200 \le X_i \lt 2^{20}).

第一行包含两个整数 nn 和 qq(2≤n≤200 0002 \le n \le 200\,000,1≤q≤200 0001 \le q \le 200\,000)—— 分别表示数组的长度和查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(对每个 ii,满足 0≤ai<2200 \le a_i \lt 2^{20})。

接下来的 qq 行每行包含一个整数 XiX_i(0≤Xi<2200 \le X_i \lt 2^{20})。

输出格式

For each query ii out of qq, print the smallest number of operations to get to the array a′a' such that a1′ & a2′ & … & an′=Xia'_1 \,\&\, a'_2 \,\&\, \ldots \,\&\, a'_n = X_i.

It's possible to show, that it's always possible to obtain such array a′a' in finite number of operations.

对于 qq 个查询中的每一个查询 ii,输出使得数组 a′a' 满足 a1′ & a2′ & … & an′=Xia'_1 \,\&\, a'_2 \,\&\, \ldots \,\&\, a'_n = X_i 所需的最少操作次数。

可以证明,总能在有限次操作内得到满足条件的数组 a′a'。

输入输出样例

  • 输入#1

    5 4
    6 4 7 5 4
    0
    2
    4
    6

    输出#1

    1
    8
    0
    5

说明/提示

For the first query, you can increase i=3i = 3 (ai=7a_i = 7) and get the array a=[6,4,8,5,4]a = [6, 4, 8, 5, 4], then 6 & 4 & 8 & 5 & 4=06 \,\&\, 4 \,\&\, 8 \,\&\, 5 \,\&\, 4 = 0.

For the third query, original array already matches condition: 6 & 4 & 7 & 5 & 4=46 \,\&\, 4 \,\&\, 7 \,\&\, 5 \,\&\, 4 = 4.

对于第一个查询,你可以将 i=3i = 3(即 ai=7a_i = 7)增加,得到数组 a=[6,4,8,5,4]a = [6, 4, 8, 5, 4],此时 6 & 4 & 8 & 5 & 4=06 \,\&\, 4 \,\&\, 8 \,\&\, 5 \,\&\, 4 = 0。

对于第三个查询,原数组已满足条件:6 & 4 & 7 & 5 & 4=46 \,\&\, 4 \,\&\, 7 \,\&\, 5 \,\&\, 4 = 4。

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

首页