HDU 4630 No Pain No Game 树状数组+离线操作
来源:互联网 发布:红人装淘宝合作接口 编辑:程序博客网 时间:2024/05/23 16:54
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值。
解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数。那么每次查询[L,R]之间两数gcd的最大值即为查询[L,R]中值最大的线段,离线所有的查询数据,然后按右端点坐标从小到大排序,依次往右加入即可。
这里学到了树状数组维护最大值的写法。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;#define N 50007int c[N];struct node{ int l,r,v;}a[10*N],Q[N];int n,pos[N],num[N],ans[N];int cmp(node ka,node kb) { return ka.r < kb.r; }int lowbit(int x) { return x&-x; }void modify(int x,int val){ while(x > 0) { c[x] = max(c[x],val); x -= lowbit(x); }}int getmax(int x){ int res = 0; while(x <= n) { res = max(res,c[x]); x += lowbit(x); } return res;}int main(){ int t,i,j,x,q,tot; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&x); pos[x] = i; c[i] = 1; } tot = 0; for(i=2;i<=n/2;i++) { int k = 0; for(j=i;j<=n;j+=i) //i的倍数 num[k++] = pos[j]; sort(num,num+k); for(j=1;j<k;j++) { a[tot].l = num[j-1]; a[tot].r = num[j]; a[tot++].v = i; } } scanf("%d",&q); for(i=0;i<q;i++) { scanf("%d%d",&Q[i].l,&Q[i].r); Q[i].v = i; } sort(a,a+tot,cmp); sort(Q,Q+q,cmp); j = 0; for(i=0;i<q;i++) { if(Q[i].l == Q[i].r) { ans[Q[i].v] = 0; continue; } while(j < tot && a[j].r <= Q[i].r) modify(a[j].l,a[j].v),j++; ans[Q[i].v] = getmax(Q[i].l); } for(i=0;i<q;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- HDU 4630 No Pain No Game 树状数组+离线操作
- hdu 4630 no pain no game 树状数组+离线查询
- HDU 4630 No Pain No Game(离线 + 树状数组)
- hdu 4630 No Pain No Game(树状数组离线操作)
- hdu-4630-No Pain No Game-(树状数组,离线处理)
- hdu 4630 No Pain No Game(线段树+离线操作)
- hdu 4630 No Pain No Game【线段树 离线操作】
- hdu 4630 No Pain No Game 线段树 树状数组
- HDU 4630 No Pain No Game 树状数组
- hdu 4630 No Pain No Game 树状数组
- 树状数组no pain no game(hdu 2013多校contest3)
- No Pain No Game(树状数组)
- HDU 4630 No Pain No Game 离线+线段树
- HDU 4630 No Pain No Game (线段树离线查询)
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
- hdu 4630 No Pain No Game (区间gcd相关x线段树or树状数组)
- hdu 4630 No Pain No Game
- HDU 4630 No Pain No Game
- Codeforces Round #267 Div2 C George and Job --DP
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
- HDU 5025 Saving Tang Monk --BFS
- HDU 5033 Building --离线+单调栈
- java helloworld
- HDU 4630 No Pain No Game 树状数组+离线操作
- Codeforces 369E Valera and Queries --树状数组+离线操作
- HDU 5047 推公式+别样输出
- HDU 5056 Boring Count --统计
- Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
- HDU 5057 Argestes and Sequence --树状数组(卡内存)
- HDU 5029 Relief grain --树链剖分第一题
- SPOJ QTREE Query on a tree --树链剖分
- iOS开发- 获取安装的所有App