CF 498D Segment Tree

Source: Internet
Author: User

The main idea is that there are n sections of road, each section of the road has a value of a, through each end of the road needs 1s, if through this section of the road time t is a multiple, you need to wait for 1s to go, that is, 2s through.

The headache is the next two numbers will be different because of the number of constraints, a start to think of a range is 2-6, to deal with the relationship between the number of each other, or want to fork.

The positive solution should be to open 60 segment tree, because 2-6 of the LCM is 60, that is, all the digital modulus 2-6, the result of the loop length of 60. So if from I to J, the start time is 0, then the answer must be the same as the start time of 60.

x segment tree A node range if it is I and j, maintenance is the start of the time modulo 60 x, from I to j+1 the time required.

Write the time according to this processing node information merge and query can.

1#include <iostream>2#include <vector>3#include <algorithm>4#include <string>5#include <cstring>6#include <cstdio>7#include <cmath>8#include <cstdlib>9#include <queue>Ten#include <stack> One#include <map> A#include <Set> -  - using namespacestd; the  - Const intn=1e5+Ten; - Const intm= -; - intv[n<<2][m]; +  - voidUpintRT) { +      for(intI=0; i<m;i++) { Av[rt][i]=v[rt<<1][i]+v[rt<<1|1[(i+v[rt<<1][i])%M]; at     } - } - voidBuildintLintRintRT) { -     if(l==r) { -         intA; -scanf"%d",&a); in          for(intI=0; i<m;i++) -v[rt][i]=1+ (i%a==0); to         return; +     } -     intM= (l+r) >>1; theBuild (l,m,rt<<1); *Build (m+1,r,rt<<1|1); $ Up (RT);Panax Notoginseng } - voidUpdateintPintAintLintRintRT) { the     if(l==r) { +          for(intI=0; i<m;i++) Av[rt][i]=1+ (i%a==0); the         return; +     } -     intM= (l+r) >>1; $     if(p<=m) $Update (p,a,l,m,rt<<1); -     Else -Update (p,a,m+1,r,rt<<1|1); the Up (RT); - }Wuyi intAskintLintRintLintRintRtintp) { the     if(l<=l&&r<=R) { -         returnV[rt][p]; Wu     } -     intret=0; About     intM= (l+r) >>1; $     if(l<=m) Ret+=ask (l,r,l,m,rt<<1, p); -     if(r>m) Ret+=ask (l,r,m+1,r,rt<<1|1, (P+ret)%M); -     returnret; - } A intMain () { +     intN; thescanf"%d",&n); -Build1N1); $     intQ; thescanf"%d",&Q); the      while(q--) { the         Chars[5]; the         intx, y; -scanf"%s%d%d",s,&x,&y); in         if(s[0]=='C') theUpdate (x, Y,1N1); the         Else Aboutprintf"%d\n", Ask (x,y-1,1N1,0)); the     } the     return 0; the}

CF 498D Segment Tree

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.