QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 1024 MB Total points: 100

#8366. Train Travel

Statistics

A railway line (not a circular one) has $n$ stations, numbered $1, \ldots, n$ in order. There are $n$ types of trains running on this line, numbered $1, \ldots, n$. Each type of train operates in both directions.

Each station on this railway line has a passenger traffic volume, which is a positive integer $\le n$. The passenger traffic volume of station $i$ ($1 \le i \le n$) is $a_i$.

A train of type $j$ ($1 \le j \le n$) stops only at stations where the passenger traffic volume is $\ge j$. A passenger can depart from station $x$, board any train that stops at $x$, and travel along the direction of the train to the next station $y$ where that train stops. If the train is traveling to the left, i.e., $y < x$, the passenger must pay $l_x$ ddtt coins; otherwise, if the train is traveling to the right, i.e., $y > x$, the passenger must pay $r_x$ ddtt coins.

It is guaranteed that for $1 \le i \le n-1$, $l_i \le l_{i+1}$ and $r_i \ge r_{i+1}$.

There are $q$ passengers, numbered $1, \ldots, q$. Passenger $k$ ($1 \le k \le q$) has a starting station $s_k$ and a destination station $t_k$ ($1 \le s_k, t_k \le n$). Assume these passengers can only move using this railway line.

For each passenger, calculate the minimum number of ddtt coins required to reach their destination. It is guaranteed that the starting station and destination station for each passenger are different. Moving back and forth is allowed. There are multiple test cases.

Input

The first line contains a positive integer $T$ ($1 \le T \le 3 \cdot 10^4$), representing the number of test cases.

For each test case, the first line contains two integers $n, q$ ($1 \le n, q \le 3 \cdot 10^5$), representing the number of stations and the number of passengers.

The second line contains $n$ integers $a_1, \ldots, a_n$, representing the passenger traffic volume of the stations.

The next $n$ lines each contain two positive integers $l_i, r_i$ ($1 \le l_i, r_i \le 10^9, l_i \le l_{i+1}, r_i \ge r_{i+1}$).

The next $q$ lines each contain two positive integers $s_k, t_k$ ($1 \le s_k, t_k \le n$).

Output

For each passenger, output the minimum number of ddtt coins required to reach the destination.

Examples

Input 1

1
9 6
1 7 3 4 9 9 1 2 2
1 11
1 11
5 11
7 10
8 6
8 4
8 3
9 1
10 1
1 9
5 1
3 1
7 6
2 6
1 1

Output 1

33
9
6
8
17
0

Constraints

It is guaranteed that $\sum n, \sum q \le 3 \cdot 10^5$, $1 \le l_i, r_i \le 10^9$, and $1 \le s_k, t_k \le n$.

It is guaranteed that $l_i \le l_{i+1}$ and $r_i \ge r_{i+1}$.

Subtask 1 (3 pts): $\sum n \le 400$.

Subtask 2 (14 pts): $\sum n, \sum q \le 5000$.

Subtask 3 (21 pts): $\sum n \le 5000$.

Subtask 4 (20 pts): $l_i = r_i = 1$ ($1 \le i \le n$).

Subtask 5 (42 pts): No additional restrictions.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.