102545: [AtCoder]ABC254 F - Rectangle GCD

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

You are given a positive integer $N$ and sequences of $N$ positive integers each: $A=(A_1,A_2,\dots,A_N)$ and $B=(B_1,B_2,\dots,B_N)$.

We have an $N \times N$ grid. The square at the $i$-th row from the top and the $j$-th column from the left is called the square $(i,j)$. For each pair of integers $(i,j)$ such that $1 \le i,j \le N$, the square $(i,j)$ has the integer $A_i + B_j$ written on it. Process $Q$ queries of the following form.

  • You are given a quadruple of integers $h_1,h_2,w_1,w_2$ such that $1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N$. Find the greatest common divisor of the integers contained in the rectangle region whose top-left and bottom-right corners are $(h_1,w_1)$ and $(h_2,w_2)$, respectively.

Constraints

  • $1 \le N,Q \le 2 \times 10^5$
  • $1 \le A_i,B_i \le 10^9$
  • $1 \le h_1 \le h_2 \le N$
  • $1 \le w_1 \le w_2 \le N$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $Q$
$A_1$ $A_2$ $\dots$ $A_N$
$B_1$ $B_2$ $\dots$ $B_N$
$\mathrm{query}_1$
$\mathrm{query}_2$
$\vdots$
$\mathrm{query}_Q$

Each query is in the following format:

$h_1$ $h_2$ $w_1$ $w_2$

Output

Print $Q$ lines. The $i$-th line should contain the answer to $\mathrm{query}_i$.


Sample Input 1

3 5
3 5 2
8 1 3
1 2 2 3
1 3 1 3
1 1 1 1
2 2 2 2
3 3 1 1

Sample Output 1

2
1
11
6
10

Let $C_{i,j}$ denote the integer on the square $(i,j)$.

For the $1$-st query, we have $C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8$, so the answer is their greatest common divisor, which is $2$.


Sample Input 2

1 1
9
100
1 1 1 1

Sample Output 2

109

Input

题意翻译

给定序列 $ a_n, b_n $,存在 $ n \times n $ 的网格图,令图上 $ (i, j) $ 位置的值为 $ a_i + b_j $。$ q $ 次询问给定 $ h_1, h_2, w_1, w_2 $,查询左上角为 $ (h_1, w_1) $,右下角为 $ (h_2, w_2) $ 的矩形中所有数的 $ \gcd $。

Output

分数:500分

问题描述

你被给定一个正整数$N$和两个各有$N$个正整数的序列:$A=(A_1,A_2,\dots,A_N)$和$B=(B_1,B_2,\dots,B_N)$。

我们有一个$N \times N$的网格。从上数第$i$行和从左数第$j$列的方格叫做方格$(i,j)$。对于每对整数$(i,j)$,其中$1 \le i,j \le N$,方格$(i,j)$上写着整数$A_i + B_j$。处理$Q$个以下形式的查询。

  • 给你一个四元组整数$h_1,h_2,w_1,w_2$,其中$1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N$。找到矩形区域的最大的公约数,该矩形的左上角和右下角分别是$(h_1,w_1)$和$(h_2,w_2)$。

限制条件

  • $1 \le N,Q \le 2 \times 10^5$
  • $1 \le A_i,B_i \le 10^9$
  • $1 \le h_1 \le h_2 \le N$
  • $1 \le w_1 \le w_2 \le N$
  • 输入中的所有值都是整数。

输入

输入通过标准输入给出以下格式:

$N$ $Q$
$A_1$ $A_2$ $\dots$ $A_N$
$B_1$ $B_2$ $\dots$ $B_N$
$\mathrm{query}_1$
$\mathrm{query}_2$
$\vdots$
$\mathrm{query}_Q$

每个查询的格式如下:

$h_1$ $h_2$ $w_1$ $w_2$

输出

打印$Q$行。第$i$行应包含查询$\mathrm{query}_i$的答案。


样例输入1

3 5
3 5 2
8 1 3
1 2 2 3
1 3 1 3
1 1 1 1
2 2 2 2
3 3 1 1

样例输出1

2
1
11
6
10

令$C_{i,j}$表示方格$(i,j)$上的整数。

对于第1个查询,我们有$C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8$,所以答案是它们的最大公约数,即$2$。


样例输入2

1 1
9
100
1 1 1 1

样例输出2

109

加入题单

算法标签: