HDU 5875 Function (2016 Dalian network race H segment tree +GCD)

Source: Internet
Author: User
Tags gcd mul

Very simple one problem, the results of the background data is wrong, I was too stupid card for 3 hours ...

Test instructions: give you a string of number A and give you some interval (Lef,rig), find out A[lef]%a[lef+1]...%a[rig]

The puzzle: We can find that the number A to the number B modulo: If a<b, then equals the original number, otherwise a will become smaller at least half. That is, a is the most successful modulo (log2 a) time, so we only need to find the first one in the interval of a value less than equals a, and then update A and the interval left end, until no value is smaller than a or interval modulo completion.

We can use the line segment tree to find the first position in the interval less than a certain value, the method is: The parent node record interval minimum, and then when the minimum value of the whole paragraph is less than the given value, recursion into this subtree (the other subtrees tree may be recursive, because the previous subtree may contain more than the interval), So we know that the first recursion to the leaf node must be the last position that is less than or equal to this value (if any).

#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<stdlib.h>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1E-8/*Note that there may be output -0.000*/#defineSGN (x) (X<-eps -1:x<eps? 0:1)//X is a comparison of two floating-point numbers, note the return integer#defineCvs (x) (x > 0.0 x+eps:x-eps)//floating point Conversion#defineZero (x) (((x) >0? ( x):-(x)) <eps)//determine if it equals 0#defineMul (A, B) (a<<b)#defineDir (A, B) (a>>b)typedefLong Longll;typedef unsignedLong Longull;Const intinf=1<< -;Const DoublePi=acos (-1.0);Const intmod=1e9+7;Const intmax=200010<<2;intNnum[max],cnt,flag;intSegtr[max];structnode{intMmin,mpos;} res;voidUpnow (intNowintnext) {Segtr[now]=min (segtr[next],segtr[next|1]); return;}voidCreate (intStaintEnnintNow ) {    if(sta==enn) {scanf ("%d",&Segtr[now]); Nnum[cnt++]=segtr[now];//record values for each location        return; }    intMid=dir (Sta+enn,1); intNext=mul (now,1);    Create (Sta,mid,next); Create (Mid+1, enn,next|1);    Upnow (Now,next); return;}voidQuery (intStaintEnnintNowintXintYintz) {    if(sta>=x&&enn<=y) {if(Sta==enn)//to the leaf node{flag=1;//indicates a leaf node can be reached only once            if(segtr[now]<=z)//Find{res.mmin=Segtr[now]; Res.mpos=STA; }            return; }        if(segtr[now]>z)//This section doesn't have to be recursive .        return; }    intMid=dir (Sta+enn,1); intNext=mul (now,1); if(mid>=x&&!flag&&segtr[next]<=z)//before the leaves, the sub-tree interval minimum is less than or equal to the given valueQuery (sta,mid,next,x,y,z); if(mid<y&&!flag&&segtr[next|1]<=z) Query (Mid+1, enn,next|1, x, Y, z); return;}intMain () {intt,n,m; intLef,rig; scanf ("%d",&t);  while(t--) {CNT=1; scanf ("%d",&N); Create (1N1); scanf ("%d",&m);  for(intI=1; i<=m;++i) {scanf ("%d%d",&lef,&rig); if(Lef==rig)//only one value{printf ("%d\n", Nnum[lef]); Continue; }            intans=Nnum[lef]; Lef++;  while(1) {res.mmin=inf,res.mpos=-1; Flag=0; Query (1N1, Lef,rig,ans); if(ans>=res.mmin)//Successful mold-taking{ans=ans%res.mmin; Lef=res.mpos+1; }                Else                     Break; if(Lef>rig| | ans==0)//End Condition                     Break; } printf ("%d\n", ans); }    }    return 0;}

HDU 5875 Function (2016 Dalian network race H segment tree +GCD)

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.