You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.0 KiB
49 lines
1.0 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int N = 200010, M = 18;
|
|
/*
|
|
输入样例:
|
|
5 3
|
|
4 12 3 6 7
|
|
1 3
|
|
2 3
|
|
5 5
|
|
|
|
答案:
|
|
1
|
|
3
|
|
7
|
|
*/
|
|
int n, q;
|
|
int w[N];
|
|
int f[N][M];
|
|
// 预处理
|
|
void rmq() {
|
|
for (int j = 0; j < M; j++) // 注意是j在外层,类似于区间DP,长度在外层
|
|
for (int i = 1; i + (1 << j) - 1 <= n; i++) // i(行)在内层
|
|
if (j == 0) // base case 边界值
|
|
f[i][j] = w[i];
|
|
else
|
|
f[i][j] = __gcd(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
|
|
}
|
|
|
|
// 查询区间最大值
|
|
int query(int l, int r) {
|
|
int len = r - l + 1;
|
|
int k = log2(len);
|
|
return __gcd(f[l][k], f[r - (1 << k) + 1][k]);
|
|
}
|
|
int main() {
|
|
// 加快读入
|
|
ios::sync_with_stdio(false), cin.tie(0);
|
|
cin >> n >> q;
|
|
for (int i = 1; i <= n; i++) cin >> w[i];
|
|
rmq();
|
|
while (q--) {
|
|
int l, r;
|
|
cin >> l >> r;
|
|
printf("%d\n", query(l, r));
|
|
}
|
|
return 0;
|
|
} |