#abc339g. G - Smaller Sum

G - Smaller Sum

Score: 600600 points

问题描述

给定一个长度为 NN 的序列 A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N)

需要回答接下来的 QQ 个查询请求。第 ii 个查询如下:

  • 找出在 ALi,ALi+1,,ARiA_{L_i},A_{L_i+1},\dots,A_{R_i} 中不超过 XiX_i 的元素之和。

请注意,你需要在线回答这些查询,即只有在回答完当前查询后,下一个查询才会显现。

因此,对于第 ii 个查询,你得到的是加密后的输入 αi,βi,γi\alpha_i, \beta_i, \gamma_i。请按照以下步骤还原原始的第 ii 个查询并作答:

  • 定义 B0=0B_0=0,且令 BiB_i 为(第 ii 个查询的答案)。
  • 查询可以通过以下方式解密:
    • Li=αiBi1L_i = \alpha_i \oplus B_{i-1}
    • Ri=βiBi1R_i = \beta_i \oplus B_{i-1}
    • Xi=γiBi1X_i = \gamma_i \oplus B_{i-1}

此处,xyx \oplus y 表示 xxyy 的按位异或操作。

什么是按位异或?非负整数 AABB 的按位异或运算 ABA \oplus B 定义如下:

  • AABB 在二进制下对应位置上的数字恰好有一个为 11,则 ABA \oplus B2k2^k 位(k0k \geq 0)上的数字为 11,否则为 00

例如:35=63 \oplus 5 = 6(二进制表示:011101=110011 \oplus 101 = 110)。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a sequence A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N) of length NN.

Answer the following QQ queries. The ii-th query is as follows:

  • Find the sum of the elements among ALi,ALi+1,,ARiA_{L_i},A_{L_i+1},\dots,A_{R_i} that are not greater than XiX_i.

Here, you need to answer these queries online.
That is, only after you answer the current query is the next query revealed.

For this reason, instead of the ii-th query itself, you are given encrypted inputs αi,βi,γi\alpha_i, \beta_i, \gamma_i for the query. Restore the original ii-th query using the following steps and then answer it.

  • Let B0=0B_0=0 and Bi=B_i = (the answer to the ii-th query).
  • Then, the query can be decrypted as follows:
    • Li=αiBi1L_i = \alpha_i \oplus B_{i-1}
    • Ri=βiBi1R_i = \beta_i \oplus B_{i-1}
    • Xi=γiBi1X_i = \gamma_i \oplus B_{i-1}

Here, xyx \oplus y denotes the bitwise XOR of xx and yy.

What is bitwise XOR? The bitwise XOR of non-negative integers AA and BB, ABA \oplus B, is defined as follows:

  • The digit in the 2k2^k place (k0k \geq 0) of ABA \oplus B in binary is 11 if exactly one of the corresponding digits of AA and BB in binary is 11, and 00 otherwise.

For example, 35=63 \oplus 5 = 6 (in binary: 011101=110011 \oplus 101 = 110).

Constraints

  • All input values are integers.
  • 1N2×1051 \le N \le 2 \times 10^5
  • 0Ai1090 \le A_i \le 10^9
  • 1Q2×1051 \le Q \le 2 \times 10^5
  • For the encrypted inputs, the following holds:
    • 0αi,βi,γi10180 \le \alpha_i, \beta_i, \gamma_i \le 10^{18}
  • For the decrypted queries, the following holds:
    • 1LiRiN1 \le L_i \le R_i \le N
    • 0Xi1090 \le X_i \le 10^9

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

QQ

α1\alpha_1 β1\beta_1 γ1\gamma_1

α2\alpha_2 β2\beta_2 γ2\gamma_2

\vdots

αQ\alpha_Q βQ\beta_Q γQ\gamma_Q

Output

Print QQ lines.
The ii-th line should contain the answer to the ii-th query.

Sample Input 1

8
2 0 2 4 0 2 0 3
5
1 8 3
10 12 11
3 3 2
3 6 5
12 0 11

Sample Output 1

9
2
0
8
5

The given sequence is A=(2,0,2,4,0,2,0,3)A=(2,0,2,4,0,2,0,3).
This input contains five queries.

  • Initially, B0=0B_0=0.
  • The first query is α=1,β=8,γ=3\alpha = 1, \beta = 8, \gamma = 3.
    • After decryption, we get $L_i = \alpha \oplus B_0 = 1, R_i = \beta \oplus B_0 = 8, X_i = \gamma \oplus B_0 = 3$.
    • The answer to this query is 99. This becomes B1B_1.
  • The next query is α=10,β=12,γ=11\alpha = 10, \beta = 12, \gamma = 11.
    • After decryption, we get $L_i = \alpha \oplus B_1 = 3, R_i = \beta \oplus B_1 = 5, X_i = \gamma \oplus B_1 = 2$.
    • The answer to this query is 22. This becomes B2B_2.
  • The next query is α=3,β=3,γ=2\alpha = 3, \beta = 3, \gamma = 2.
    • After decryption, we get $L_i = \alpha \oplus B_2 = 1, R_i = \beta \oplus B_2 = 1, X_i = \gamma \oplus B_2 = 0$.
    • The answer to this query is 00. This becomes B3B_3.
  • The next query is α=3,β=6,γ=5\alpha = 3, \beta = 6, \gamma = 5.
    • After decryption, we get $L_i = \alpha \oplus B_3 = 3, R_i = \beta \oplus B_3 = 6, X_i = \gamma \oplus B_3 = 5$.
    • The answer to this query is 88. This becomes B4B_4.
  • The next query is α=12,β=0,γ=11\alpha = 12, \beta = 0, \gamma = 11.
    • After decryption, we get $L_i = \alpha \oplus B_4 = 4, R_i = \beta \oplus B_4 = 8, X_i = \gamma \oplus B_4 = 3$.
    • The answer to this query is 55. This becomes B5B_5.

update @ 2024/3/10 01:32:16