#ABC477E. 轮状图的距离 / Wheel Distance

轮状图的距离 / Wheel Distance

Problem Statement

You are given an edge-weighted undirected graph with N+1N+1 vertices numbered 11 to N+1N+1. For each integer ii satisfying 1≤i≤N1 \leq i \leq N, there is an edge of weight AiA_i connecting vertex ii and vertex (i mod N)+1(i \bmod N) + 1. Also, for each integer ii satisfying 1≤i≤N1 \leq i \leq N, there is an edge of weight BiB_i connecting vertex ii and vertex N+1N+1. There are no other edges.

Process QQ queries. In each query, you are given SS and TT; find the length of the shortest path from vertex SS to vertex TT.

Constraints

  • 3≤N≤2×1053 \leq N \leq 2 \times 10^5
  • 1≤Q≤2×1051 \leq Q \leq 2 \times 10^5
  • 1≤Ai,Bi≤1091 \leq A_i, B_i \leq 10^9
  • 1≤S<T≤N+11 \leq S \lt T \leq N+1
  • All input values are integers.

Input

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

  • NN QQ
  • A1A_1 A2A_2 …\ldots ANA_N
  • B1B_1 B2B_2 …\ldots BNB_N
  • query1\mathrm{query}_1
  • query2\mathrm{query}_2
  • ⋮\vdots
  • queryQ\mathrm{query}_Q

Each query queryq\mathrm{query}_q is given in the following format:

  • SS TT

Output

Output QQ lines. The qq-th line should contain the answer to the qq-th query.

5 3
1 3 4 2 5
5 2 4 3 7
2 5
5 6
1 4
6
5
6
  • For the first query, one shortest path from vertex 22 to vertex 55 is 2→1→52\to1\to5, and its length is A1+A5=1+5=6A_1+A_5=1+5=6.
10 12
95786828 55052989 7398452 90695030 73248844 120697567 77915140 68865651 15301338 68674005
690874817 21524935 244587369 529970100 736247510 757265588 993115119 576136368 21553212 219853538
5 6
5 8
2 6
3 7
1 3
3 4
8 11
1 10
2 6
6 8
8 9
5 8
73248844
265090269
226395315
244911927
150839817
7398452
90418863
68674005
226395315
198612707
68865651
265090269