No Pain No Game

hdu4630:http://acm.hdu.edu.cn/showproblem.php?pid=4630

题意:给定一个排序,求区间最大GCD。

题解:离散树状数组。首先把查询按左端点从大到小排序。然后用树状数组来维护每个位置出现的最大的公约数。枚举每个数的约数,记录到当前位置为止,上一个x的倍数出现的位置b[x];

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<vector>
 6 using namespace std;
 7 const int N=5e4+19;
 8 int a[N],c[N],b[N],ans[N];
 9 int n,m;
10 int lowbit(int x){
11    return x&(-x);
12 }
13 void add(int x,int val){
14     while(x<=n){
15         c[x]=max(c[x],val);
16         x+=lowbit(x);
17     }
18 }
19 int getsum(int x){
20    int sum=0;
21    while(x>0){
22      sum=max(sum,c[x]);
23      x-=lowbit(x);
24    }
25    return sum;
26 }
27 struct Node{
28   int l,r;
29   int id;
30   bool operator<(const Node a)const{
31     return l>a.l;
32   }
33 }num[N];
34 int main(){
35     int T;
36     scanf("%d",&T);
37     while(T--){
38         scanf("%d",&n);
39         memset(a,0,sizeof(a));
40         memset(c,0,sizeof(c));
41         memset(b,0,sizeof(b));
42         for(int i=1;i<=n;i++)
43           scanf("%d",&a[i]);
44           scanf("%d",&m);
45           for(int i=1;i<=m;i++){
46             scanf("%d%d",&num[i].l,&num[i].r);
47             num[i].id=i;
48           }
49         sort(num+1,num+m+1);
50         int j=n;
51         for(int i=1;i<=m;i++){
52             for(;j>=num[i].l;j--){
53                for(int k=1;k*k<=a[j];k++){
54                    if(a[j]%k==0){
55                        if(b[k]){
56                         add(b[k],k);
57                        }
58                         b[k]=j;
59                       if(k*k!=a[j]){
60                          if(b[a[j]/k]){
61                             add(b[a[j]/k],a[j]/k);
62                          }
63                          b[a[j]/k]=j;
64                       }
65                    }
66                }
67              }
68            ans[num[i].id]=getsum(num[i].r);
69          }
70      for(int i=1;i<=m;i++)
71         printf("%d\n",ans[i]);
72     }
73 }

时间: 2024-10-27 10:02:03

No Pain No Game的相关文章

swust oj 1026--Egg pain&#39;s hzf

题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf is crazy about reading math recently,and he is thinking about a boring problem. Now there are n integers Arranged in a line.For each integer,he wants to know

Pain for friend

For a guy who has experienced his fair share of mysteries,on mystery,I still can't figure out is why some people come into our lives.Why some people go,and others become a part of you.Some friendships feel like they'll last forever.And others end far

HDU 4630 No Pain No Game

No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1465    Accepted Submission(s): 631 Problem Description Life is a game,and you lose it,so you suicide.But you can not kill yoursel

Your pain

Your pain is the breaking of the shell that encloses your understanding. 你的痛苦是你那包裹知识的皮壳的破裂.

hdu 4630 No Pain No Game【线段树 离线操作】

欢迎参加--每周六晚的BestCoder(有米!) No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1820    Accepted Submission(s): 778 链接:hdu 4630 Problem Description Life is a game,and you lose it,so y

hdu 4630 No Pain No Game(线段树+离线操作)

No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1769    Accepted Submission(s): 748 Problem Description Life is a game,and you lose it,so you suicide. But you can not kill yours

Miss leads pain ?

I have been 27 years old , very strong . I first go to XinJiang university for a master's degree in september,2017. I come with excitement and dream,then everything changed after meeting a very very beautiful and special girl unexpectedly. she is so

HDU - 4630 No Pain No Game(离线线段树)

No Pain No Game 题意:给出一个长度为n的1到n的排列 求区间两点gcd最大 思路: 因为题目没有更新 我们可以离线求解 对于每个查询按r排序 因为两点gcd一定会是两个数的约数 那么可以暴力插入a[i]的约数(当a[x]含有这个约数时 我们就能插入这个约数(x<i)) 我们使用last数组维护上一次出现这个约数的位置即可 查询:我们就只需要查询l~r出现过最大的约数就行 #include<bits/stdc++.h> using namespace std; #defin

HDU4630:No Pain No Game(线段树)

Problem Description 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 a1, a2, ..., an.They are also a permutation of 1...n. You need to answer some queries,each

&quot;this whole ARM thing is a f*cking pain in the ass&quot;