AT_abc479_d.Back and Forth Distance

普及-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are NN squares arranged in a row, numbered 1,2,…,N1, 2, \ldots, N from left to right.

When you are on square ii, you can make one of the following moves.

  • Choose an integer jj such that 1≤j<i1 \leq j < i, and move to square jj. The cost of this move is LiL_i.

  • Choose an integer jj such that i<j≤Ni < j \leq N, and move to square jj. The cost of this move is RiR_i.

You are given QQ queries. Answer each of them. Each query is in the following format.

  • Integers ss and tt are given. Find the minimum total cost required to reach square tt from square ss by repeatedly making moves.

有 NN 个正方形从左到右依次排列,编号为 1,2,…,N1, 2, \ldots, N。

当你位于第 ii 个正方形时,你可以执行以下两种移动之一:

  • 选择一个整数 jj,满足 1≤j<i1 \leq j < i,然后移动到第 jj 个正方形。该移动的代价为 LiL_i。

  • 选择一个整数 jj,满足 i<j≤Ni < j \leq N,然后移动到第 jj 个正方形。该移动的代价为 RiR_i。

你将收到 QQ 个查询,需对每个查询作答。每个查询的格式如下:

  • 给定整数 ss 和 tt,求从第 ss 个正方形出发,通过若干次上述移动到达第 tt 个正方形所需的最小总代价。

输入格式

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

NN QQ
L2L_2 L3L_3 …\ldots LNL_N
R1R_1 R2R_2 …\ldots RN−1R_{N - 1}
query1\text{query}_1
query2\text{query}_2
⋮\vdots
queryQ\text{query}_Q

Here, queryi\text{query}_i is the ii-th query, given in the following format:

ss tt

输入从标准输入中按以下格式给出:

NN QQ
L2L_2 L3L_3 …\ldots LNL_N
R1R_1 R2R_2 …\ldots RN−1R_{N - 1}
query1\text{query}_1
query2\text{query}_2
⋮\vdots
queryQ\text{query}_Q

其中,queryi\text{query}_i 表示第 ii 个查询,其格式如下:

ss tt

输出格式

Output QQ lines.

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

输出 QQ 行。

第 ii 行应包含第 ii 个查询的答案。

输入输出样例

  • 输入#1

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

    输出#1

    3
    3
    1
    5
    3
    2
    9

说明/提示

Sample 1 Explanation:
We explain the first and second queries.

For the first query, if you move directly from square 11 to square 22, the total cost is R1=3R_1 = 3, which is the minimum possible total cost.

For the second query, if you move from square 22 to square 44 and then from square 44 to square 11, the total cost is R2+L4=3R_2 + L_4 = 3, which is the minimum possible total cost.

Constraints

  • 2≤N≤3×1052 \leq N \leq 3 \times 10^5
  • 1≤Q≤3×1051 \leq Q \leq 3 \times 10^5
  • 1≤Li≤1091 \leq L_i \leq 10^9 (2≤i≤N)(2 \leq i \leq N)
  • 1≤Ri≤1091 \leq R_i \leq 10^9 (1≤i≤N−1)(1 \leq i \leq N - 1)
  • 1≤s,t≤N1 \leq s, t \leq N for each query.
  • s≠ts \neq t for each query.
  • All input values are integers.

样例 1 解释:
我们解释第一个和第二个查询。

对于第一个查询,若直接从方格 11 移动到方格 22,总代价为 R1=3R_1 = 3,这是可能的最小总代价。

对于第二个查询,若从方格 22 移动到方格 44,再从方格 44 移动到方格 11,总代价为 R2+L4=3R_2 + L_4 = 3,这是可能的最小总代价。

约束条件

  • 2≤N≤3×1052 \leq N \leq 3 \times 10^5
  • 1≤Q≤3×1051 \leq Q \leq 3 \times 10^5
  • 1≤Li≤1091 \leq L_i \leq 10^9 (2≤i≤N)(2 \leq i \leq N)
  • 1≤Ri≤1091 \leq R_i \leq 10^9 (1≤i≤N−1)(1 \leq i \leq N - 1)
  • 每个查询中,1≤s,t≤N1 \leq s, t \leq N。
  • 每个查询中,s≠ts \neq t。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页