#P9376. 「DROI」Round 2 进制与操作

    ID: 10373 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>莫队树状数组O2优化可持久化线段树随机化

「DROI」Round 2 进制与操作

Background

Instead of writing a weak background, it is better to make a higher-quality problem.

Problem Description

Define one operation on a number xx in base BB as either of the following:

  • Set xxBx \rightarrow \lfloor \dfrac{x}{B} \rfloor.

  • Set xx×B+tx \rightarrow x \times B + t,where t[0,B1]t \in [0,B-1].

You are given a sequence AA of length nn. There are mm queries, and each query is in the form:

  • l r B means: for the numbers in the sequence AA with indices in [l,r][l,r], when operating in base BB, what is the minimum number of operations needed to make all numbers equal (note: each operation is applied to one number).

Queries are independent, i.e. the operations are not actually performed.

Input Format

The first line contains two integers, which are n,mn, m.

The second line contains nn numbers, representing the sequence AA.

The next mm lines each contain three numbers, representing l,r,Bl, r, B for the query.

Output Format

Output mm lines in total. The ii-th line should contain the answer to the ii-th query.

5 5
7 6 5 8 9
1 3 2
2 5 2
4 4 6
3 5 4
1 5 3
5
8
0
5 
10
8 4
10 14 7 11 19 13 7 18 
1 7 4
3 8 2
1 4 4
1 4 2

15
18
8
11

Hint

Sample Explanation

For sample 1, for the five queries, making all numbers in the interval become 33, 44, 88, 44, and 66, respectively, is one optimal way.


Constraints

"This problem uses bundled testdata."

  • Subtask1(10%)\operatorname{Subtask} 1(10\%): n,m1000n,m \leq 1000.

  • Subtask2(20%)\operatorname{Subtask} 2(20\%): it is guaranteed that all queries have B=2B=2.

  • Subtask3(40%)\operatorname{Subtask} 3(40\%): n,m3×104n,m \leq 3 \times 10^4.

  • Subtask4(30%)\operatorname{Subtask} 4(30\%): no special restrictions.

For 100%100\% of the testdata: 1n,m1051 \leq n,m \leq 10^5, 2Ai,B1082 \leq A_i,B \leq 10^8.

Translated by ChatGPT 5