E - No Pain No Game 线段树
Life is a game,and you lose it,so you suicide.
But you can not kill yourself before you solve this problem:
Given you a sequence of number a 1, a 2, ..., a n.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.
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄
But you can not kill yourself before you solve this problem:
Given you a sequence of number a 1, a 2, ..., a n.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.
InputFirst line contains a number T(T <= 5),denote the number of test cases.
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 a 1, a 2, ..., a n.
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.OutputFor each test cases,for each query print the answer in one line.Sample Input
1
10
8 2 4 9 5 7 10 6 1 3
5
2 10
2 4
6 9
1 4
7 10
Sample Output
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。5 2 2 4 3
这个题目好难,不会写,看题解的。
这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u010033217/article/details/38156507
这个题目因为数据太大,所以如果直接每一个查询都直接查肯定会T的,所以就要用到线段树。
我直接说思路吧,
第一步,预处理所有可能的数据,就是把每一个数的因子全部求出来,用一个数组存下来。
第二步,输入数据,对于要查询的数据用结构体处理,然后对于结构体的右端点进行排序,从小到大。//这个是为了第三步,第四步
第三步,对于给定的数列进行处理,每次处理一个数,如果这个数的因子前面出现过,那么就更新这个数前面这个位置的最大的gcd,然后再更新这个数字出现的新位置。
//这个是关键,更新这个新位置是必要的,这个可以自己想明白吧。为什么是更新前面的位置呢?因为更新前面的位置之后,第四步就判断到目前位置的最大值就可以直接判断了,还不懂可以看第四步解析。
第四步,每次处理完一个数,都要判断查询里面是不是存在这个。
//因为本来应该是区间更新,现在可以用单点更新代替,这个是因为排序之后的r会越来越大,所以你只要更新区间的左端点就可以了,左端点被包含了,右端点肯定被包含进去了。
最后一步输出答案。
#include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <vector> #include <algorithm>
#define inf 0x3f3f3f3f
using namespace std; typedef long long ll; const int maxn = 5e4 + 10; int a[maxn], pre[maxn], n, m; int ans[maxn]; vector<int>vec[maxn]; struct node { int l, r, id; int Max; }tree[maxn*4]; node in[maxn]; void init() { for(int i=1;i<maxn;i++) { for(int j=i;j<maxn;j+=i) { vec[j].push_back(i); } } } bool cmp(node a,node b) { if (a.r == b.r) return a.l < b.l; return a.r < b.r; } void build(int id,int l,int r) { tree[id].l = l; tree[id].r = r; tree[id].Max = 0; if (l == r) return; int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); } void push_up(int id) { tree[id].Max = max(tree[id << 1].Max, tree[id << 1 | 1].Max); } void update(int pos,int val,int id) { if(tree[id].l==tree[id].r) { tree[id].Max = max(tree[id].Max, val); return; } int mid = (tree[id].l + tree[id].r) >> 1; if (pos <= mid) update(pos, val, id << 1); else update(pos, val, id << 1 | 1); push_up(id); } int query(int l,int r,int id) { if(l<=tree[id].l&&r>=tree[id].r) { return tree[id].Max; } int ans = 0; int mid = (tree[id].l + tree[id].r) >> 1; if (l <= mid) ans = max(ans, query(l, r, id << 1)); if (r > mid) ans = max(ans, query(l, r, id << 1 | 1)); return ans; } int main() { int t; init(); scanf("%d", &t); while(t--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); build(1, 1, n); scanf("%d", &m); for(int i=1;i<=m;i++) { scanf("%d%d", &in[i].l, &in[i].r); in[i].id = i; } memset(pre, -1, sizeof(pre)); sort(in + 1, in + 1 + m, cmp); for(int i=1,j=1;i<=n&&j<=m;i++) { for(int k=0;k<vec[a[i]].size();k++) { int tmp = vec[a[i]][k]; if(pre[tmp]!=-1) { update(pre[tmp], tmp, 1); } pre[tmp] = i; } while(in[j].r==i&&j<=m) { if(in[j].l==in[j].r) { ans[in[j].id] = 0; j++; continue; } ans[in[j].id] = query(in[j].l, in[j].r, 1); j++; } } for(int i=1;i<=m;i++) { printf("%d\n", ans[i]); } } return 0; }

更多精彩