#P4738. [CERC2017] Cumulative Code

[CERC2017] Cumulative Code

题目描述

As you probably know, a tree is a graph consisting of nn nodes and n1n - 1 undirected edges in which any two nodes are connected by exactly one path. In a labeled tree each node is labeled with a different integer between 11 and nn. The Prüfer code of a labeled tree is a unique sequence associated with the tree, generated by repeatedly removing nodes from the tree until only two nodes remain. More precisely, in each step we remove the leaf with the smallest label and append the label of its neighbour to the end of the code. Recall, a leaf is a node with exactly one neighbour. Therefore, the Prüfer code of a labeled tree is an integer sequence of length n2n - 2. It can be shown that the original tree can be easily reconstructed from its Prüfer code. The complete binary tree of depth kk, denoted with CkC_k, is a labeled tree with 2k12^k - 1 nodes where node jj is connected to nodes 2j2j and 2j+12j + 1 for all j<2k1j < 2^{k-1}. Denote the Prüfer code of CkC_k with p1,p2,...,p2k3p_1,p_2,..., p_{2^k-3}. Since the Prüfer code of CkC_k can be quite long, you do not have to print it out. Instead, you need to answer nn questions about the sums of certain elements on the code. Each question consists of three integers: a,da, d and mm. The answer is the sum of the of the CkC_k' s Prüfer code elements pa,pa+d,pa+2d,...,pa+(m1)dp_a, p_{a+d},p_{a+2d},...,p_{a+(m-1)d}.

输入格式

The first line contains two integers kk and q(2k30,1q300)q(2 \le k \le 30,1 \le q \le 300) — the depth of the complete binary tree and the number of questions. The jthj-th of the following qq lines contains the jthj-th question:three positive integers aj,dja_j,d_j and mjm_j such that aj,dja_j,d_j and aj+(mj1)dja_j + (m_j - 1)d_j are all at most 2k32^k - 3.

输出格式

Output 1 lines. The jthj-th line should contain a single integer — the answer to the jthj-th question.

题目大意

给一棵深度为 kk,点数为 2k12^k-1 的完全二叉树,其中根节点编号为 11,节点 xx 的儿子为 2x2x2x+12x+1

记它的 prufer 序列为 pp,下标从 11 开始。给定 qq 次询问,每次给出三个参数 aaddmm,求 i=0m1pa+id\sum\limits_{i=0}^{m-1}p_{a+id}

3 5
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
2
2
1
3
3
4 4
2 1 5
4 4 3
4 8 1
10 3 2
18
15
5
13
7 1
1 1 125
4031

提示

In the first example above, when constructing the Prüfer code for C3C_3 the nodes are removed in the following order: 4,5,2,1,64, 5, 2, 1, 6. Therefore, the Prüfer code of C3C_3 is 2,2,1,3,32, 2, 1, 3, 3.