标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 const int maxn=1e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int n,m,k,t,a[maxn],c[maxn],pr[maxn*10],ans[maxn]; vector<pii >b[maxn]; vector<pii >q[maxn]; void add(int x,int y) { for(int i=x;i<=n;i+=(i&(-i))) c[i]+=y; } int get(int x) { int ret=0; for(int i=x;i;i-=i&(-i)) ret+=c[i]; return ret; } void init() { for(int i=1;i<=n;i++) { int x=a[i],y=i; b[i].pb(mp(x,y)); for(int j=0;j<b[i-1].size();j++) { int now=gcd(b[i-1][j].fi,a[i]),pos=b[i-1][j].se; if(now!=x) { b[i].pb(mp(now,pos)); x=now; } } } } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { rep(i,1,n)scanf("%d",&a[i]),b[i].clear(),q[i].clear(); memset(c,0,sizeof c); memset(pr,0,sizeof pr); init(); rep(i,1,m) { int x,y; scanf("%d%d",&x,&y); q[y].pb(mp(x,i)); } rep(i,1,n) { for(j=0;j<b[i].size();j++) { int x=b[i][j].fi,pos=b[i][j].se; if(pr[x])add(pr[x],-1); add(pos,1); pr[x]=pos; } for(int j=0;j<q[i].size();j++) { int x=q[i][j].fi,y=q[i][j].se; ans[y]=get(i)-(x-1==0?0:get(x-1)); } } rep(i,1,m)printf("%d\n",ans[i]); } //system("Pause"); return 0; }
2016大连网络赛 Different GCD Subarray Query
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5860950.html