HDU 5869 Different GCD Subarray Query [区间gcd预处理]【数据
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 ———————————————————————————————————. Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description Given an array a of N positive integers a1,a2,?aN?1,aN; a subarray of a is defined as a continuous interval between a1 and aN. In other words,ai,ai+1,?,aj?1,aj is a subarray of a,for 1≤i≤j≤N. For a query in the form (L,R),tell the number of different GCDs contributed by all subarrays of the interval [L,R]. Input For each test,the first line consists of two integers N and Q,denoting the length of the array and the number of queries,respectively. N positive integers are listed in the second line,followed by Q lines each containing two integers L,R for a query. You can assume that 1≤N,Q≤100000 1≤ai≤1000000 Output Sample Input Sample Output Source ———————————————————————————————————. 解题思路: 然后vis【1e6】居然RE。。。。最后改了map才过、 时间复杂度
附本题代码 #include <bits/stdc++.h>
using namespace std;
#define INF (~(1<<31))
#define INFLL (~(1ll<<63))
#define pb push_back
#define mp make_pair
#define abs(a) ((a)>0?(a):-(a))
#define lalal puts("*******");
#define s1(x) scanf("%d",&x)
#define Rep(a,b,c) for(int a=(b);a<=(c);a++)
#define Per(a,c) for(int a=(b);a>=(c);a--)
#define no puts("NO")
typedef long long int LL ;
typedef unsigned long long int uLL ;
const int N = 100000+7;
const int MOD = 1e9+7;
const double eps = 1e-6;
const double PI = acos(-1.0);
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void fre(){
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
inline int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);}
/***********************************************************************/
int sum[N];
#define lowbit(x) (x&-x)
void update(int index,int val){
for(int i=index;i<N;i+=lowbit(i)) sum[i]+=val;
}
int getSum(int index){
int ans = 0;
for(int i=index;i;i-=lowbit(i)) ans += sum[i];
return ans;
}
vector<pair<int,int> >E[N];
int a[N],ans[N];
struct node {
int l,r,id;
bool operator <(const node &p) const{
//if(r==p.r) return l<p.l;
return r<p.r;
}
}q[N];
map<int,int>vis;
int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=1;i<=m;++i) scanf("%d %d",&q[i].l,&q[i].r),q[i].id=i,ans[i]=0;
sort(q+1,q+m+1);
for(int i=0;i<=n;++i)E[i].clear();
for(int i=1;i<=n;++i){
int x=a[i];
int y=i;
for(int j=0;j<E[i-1].size();++j){
int res=gcd(x,E[i-1][j].first);
if(x!=res){
E[i].pb(mp(x,y));
x=res;
y=E[i-1][j].second;
}
}
E[i].pb(mp(x,y));
}
vis.clear();
for(int R=0,i=1;i<=m;++i){
while(R < q[i].r){
R++;
for(int j=0;j<E[R].size();++j) {
int res=E[R][j].first;
int ids=E[R][j].second;
if(vis[res]) update(vis[res],-1);
vis[res] = ids;
update(vis[res],1);
}
}
ans[q[i].id] = getSum(R) - getSum(q[i].l-1);
}
for(int i=1;i<=m;++i) printf("%dn",ans[i]);
}
return 0;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |