#P5654. 基础函数练习题

基础函数练习题

Description

YSGH has a permutation pp of 1n1 \sim n and an integer sequence ww of length nn.

Define:

$$F(l, r) = \begin{cases} \max(F(l, m - 1), F(m + 1, r)) + w_m & , l \le r \\ 0 & , l > r \end{cases}$$

where mm is the index of the maximum value of pp in the interval [l,r][l, r].

There are qq queries asking for the value of F(l,r)F(l, r).

Input Format

The first line contains two positive integers n,qn, q, with the same meaning as in the statement.

The second line contains nn positive integers. The ii-th integer is pip_i, with the same meaning as in the statement.

The third line contains nn integers. The ii-th integer is wiw_i, with the same meaning as in the statement.

The next qq lines each contain two positive integers l,rl, r (1lrn1 \le l \le r \le n), representing a query for F(l,r)F(l, r).

Output Format

Output qq lines. Each line contains one integer, which is the answer.

5 2
2 1 5 3 4
2 5 1 2 4
3 5
1 1
7
2

Hint

This problem uses bundled testdata.

  • Subtask 1 (10 points): n,q5×103n, q \le 5 \times {10}^3.
  • Subtask 2 (10 points): It is guaranteed that pp is random.
  • Subtask 3 (20 points): n,q5×104n, q \le 5 \times {10}^4.
  • Subtask 4 (20 points): n,q105n, q \le {10}^5.
  • Subtask 5 (20 points): wi0w_i \ge 0.
  • Subtask 6 (20 points): No special constraints.

For 100%100\% of the data, 1n,q5×1051 \le n, q \le 5 \times {10}^5, wi109|w_i| \le 10^9, 1pin1 \le p_i \le n. It is guaranteed that pp is a permutation of 1n1 \sim n.

Translated by ChatGPT 5