The Great God:
http://blog.csdn.net/u014800748/article/details/47680899
The sum of GCDTime
limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 526 Accepted Submission (s): 226
Problem Descriptionyou has an arrayThe length ofIs
Let
Inputthere is multiple test cases. The first line of input contains an integer T, indicating the number of the test cases. For each test case:
First line have one integers
Second Line hasIntegers
Third line have one integersThe number of questions
Next there is Q Lines,each line has both integers,
Outputfor each question,you need to print
Sample Input
251 2 3 4 531 32 31 444 2 6 931 32 42 3
Sample Output
9616182310
Authorsxyz
Source2015 multi-university Training Contest 8
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < vector>using namespace Std;const int maxn=10100;typedef long Long int ll;struct g{g () {} g (int _id,ll _g): ID (_id) , g (_g) {} int id; LL g;void toString () {printf ("ID:%d g:%lld\n", id,g);}; int n,a[maxn],q;vector<g> vl[maxn],vr[maxn];struct que{int l,r,id; BOOL operator< (const que& Que) Const {if (l!=que. L) return L<que. L Return R<que. R }}que[maxn];void PreInit () {///Get left point////with I as the right endpoint, preprocess the segment for (int i=1;i<=n;i++) {vl[i].cle AR (); if (i==1) {vl[i].push_back (G (I,a[i])); } else {LL curg=a[i];int l=i;for (auto &it:vl[i-1]) {int G=__GCD (It.g,curg), if (G!=curg) Vl[i].push_back ( G (L,curg)); curg=g; L=it.id;} Vl[i].push_back (G (L,curg)); }}///Get right point///with I as the left end, preprocess the segment for (int i=n;i>=1;i--) {vr[i].clear (), if (i==n) {Vr[i].push_Back (G (i,a[i));} Else{ll curg=a[i];int r=i;for (auto &it:vr[i+1]) {int G=__GCD (CURG,IT.G), if (G!=curg) Vr[i].push_back (g (R,curg)); curg=g; R=it.id;} Vr[i].push_back (G (R,curg));}}} Calculate the value between L,r ll calu (int type,int l,int R) {LL ret=0;if (type==0) {int tr=r;for (auto &it:vl[r]) {if (it.id>=l) {ret+= (tr-it.id+1) *it.g;tr=it.id-1;} else{ret+= (tr-l+1) *it.g;break;}} else if (type==1) {int tr=l;for (auto &it:vr[l]) {if (it.id<=r) {ret+= (it.id-tr+1) *it.g;tr=it.id+1;} else{ret+= (r-tr+1) *it.g;break;}} return ret;} LL Ans[maxn];int Main () {int t_t;scanf ("%d", &t_t); while (t_t--) {scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%d", a+i); PreInit (); scanf ("%d", &q); for (int i=0,l,r;i<q;i++) {scanf ("%d%d", &l,&r); Que[i]. L=l; Que[i]. R=r; Que[i].id=i; } sort (que,que+q); int l=1,r=0; LL ret=0;for (int i=0;i<q;i++) {while (R<que[i]. R) {R++;ret+=calu (0,l,r);} while (R>que[i]. R) {Ret-=calu (0,l,r); r--;} while (L<que[i]. L) {Ret-=calu (1,l,r); l++;} while (L>que[i]. L) {L--;ret+=calu (1,l,r);} Ans[que[i].id]=ret;} for (int i=0;i<q;i++) cout<<ans[i]<<endl; } return 0;}
Hdoj 5381 The sum of GCD-mo team algorithm