No Pain No Game
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 474 Accepted Submission(s): 187
But you can not kill yourself before you solve this problem:
Given you a sequence of number a1, a2, ..., an.They are also a permutation of 1...n.
You need to answer some queries,each with the following format:
If we chose two number a,b (shouldn't be the same) from interval [l, r],what is the maximum gcd(a, b)? If there's no way to choose two distinct number(l=r) then the answer is zero.
Then follow T test cases.
For each test cases,the first line contains a number n(1 <= n <= 50000).
The second line contains n number a1, a2, ..., an.
The third line contains a number Q(1 <= Q <= 50000) denoting the number of queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n),denote a query.
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
//線段樹 + 離線處理
const int V = 50000 + 5;
typedef struct node {
int l, r, index;
};
node nod[V];
int sum[V][99], sum_top[V], last[V];
int p[V * 3], first_number_index, ans[V];
int n, m, T;
bool cmp(node a, node b) {
if(a.r < b.r)
return true;
return false;
}
void build(int N) {
int nn = 1;
int r = 2;
while(r < 2 * N) {
nn++;
r *= 2;
}
first_number_index = 1 << (nn - 1);
memset(p, 0, sizeof(p));
}
void Update(int index, int num) {
p[index] = num;
while(index != 0) {
index >>= 1;
p[index] = max(p[index << 1], p[(index << 1) | 1]);
}
}
int Query(int a, int b) {
int Max = 0;
while(a <= b) {
int temp = 1;
int index = first_number_index + a - 1;
while(index % (2 * temp) == 0 && a + (2 * temp) - 1 <= b)
temp *= 2;
Max = max(Max, p[index / temp]);
a += temp;
}
return Max;
}
int main() {
int i, j, k;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
build(n);
memset(sum_top, 0, sizeof(sum_top));
memset(last, 0, sizeof(last));
for(i = 1; i <= n; ++i) {
int temp;
scanf("%d", &temp);
for(j = 1; j <= (int) sqrt(temp * 1.0); ++j)
if(temp % j == 0) {
sum[i][sum_top[i]++] = temp / j;
if(j * j != temp)
sum[i][sum_top[i]++] = j;
}
}
scanf("%d", &m);
for(i = 0; i < m; ++i) {
scanf("%d%d", &nod[i].l, &nod[i].r);
nod[i].index = i;
}
int k = 0;
sort(nod, nod + m, cmp);
for(i = 1; i <= n && k < m; ++i) {
for(j = 0; j < sum_top[i]; ++j) {
int pre = sum[i][j];
if(last[pre] > 0) {
int a = last[pre];
int index = first_number_index + a - 1;
if(p[index] < pre)
Update(index, pre);
}
last[pre] = i;
}
while(nod[k].r == i) {
ans[nod[k].index] = Query(nod[k].l, nod[k].r);
k++;
}
}
for(i = 0; i < m; ++i)
printf("%d\n", ans[i]);
}
}