HDU 4630-no Pain No Game (segment tree + offline processing)

Source: Internet
Author: User
Tags greatest common divisor

Test instructions

Give you a sequence of n number a,q a query, each query to L,r, for the subscript i in [l,r] the interval of any two number of greatest common divisor in the maximum value

Analysis:

With hdu3333 experience, we sweep the sequence from left to right, and if the current number of approximations appears before, then this approximate may be the biggest answer. So we enumerate all the approximate numbers of the current number, maintain interval maximums with the segment tree, and query sequences are processed offline to ensure that queries are correct.

#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<1|1#defineAll 1,n,1#defineN 50010#defineRead Freopen ("In.txt", "R", stdin)Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;//bef the position of the current number in the previous occurrenceintmaxv[n*4],n,bef[n],e,a[n],maxn[n];structque{intL,r,id;} Q[n];BOOLcmp (que x,que y) {returnx.r<Y.R;}voidPushup (intRT) {Maxv[rt]=max (maxv[rt<<1],maxv[rt<<1|1]);}voidBuildintLintRintRT) {Maxv[rt]=1; if(L==R)return; intM= (l+r) >>1;    Build (Lson); Build (Rson);}voidUpdateintPintVintLintRintRT) {    if(l==R) {Maxv[rt]=Max (MAXV[RT],V); return; }    intM= (l+r) >>1; if(p<=m) update (P,v,lson); ElseUpdate (P,v,rson); Pushup (RT);}intQueryintLintRintLintRintRT) {    if(l<=l&&r>=R) {        returnMaxv[rt]; }    intM= (l+r) >>1; intnum=0; if(l<=m) num=Max (Num,query (L,r,lson)); if(r>m) num=Max (Num,query (L,r,rson)); returnnum;}voidsolve () {sort (q+1, q+e+1, CMP); memset (BEF,0,sizeof(BEF)); Build (1N1); intb=1;  for(intI=1; i<=n;++i) {         for(intj=1; j*j<=a[i];j++){            if(a[i]%j==0){                if(Bef[j]) {update (BEF[J],J,1N1); }                inttmp=a[i]/J; if(bef[tmp]&&tmp!=j) Update (BEF[TMP],TMP,1N1); BEF[J]=i; BEF[TMP]=i; }        }         while(b<=e&&q[b].r==i) {            if(q[b].l==Q[B].R) maxn[q[b].id]=0; Else{Maxn[q[b].id]=query (Q[B].L,Q[B].R,1N1); } b++; }    }     for(intI=1; i<=e;++i) printf ("%d\n", Maxn[i]);}intMain () {intT; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(intI=1; i<=n;++i) scanf ("%d",&A[i]); scanf ("%d",&e);  for(intI=1; i<=e;++i) {scanf ("%d%d",&q[i].l,&Q[I].R); Q[i].id=i;    } solve (); }return 0;}

HDU 4630-no Pain No Game (segment tree + offline processing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.