hud-5475 an easy problem (segment tree)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5475

Test instructions

At the beginning of the original number is 1 in order to give you the Q operation, and then there are two types of operations.

1 means that the number after the last operation is multiplied by the number given to you, then the remainder, then the output, 2 represents the addition of the operation, dividing the current number by the specified number of previous

Then modulo the M.

Ideas:

This problem is written with a line segment tree, the breakpoint is updated, and the complexity is n (log (n));

Code:

1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <stdlib.h>6typedefLong LongLL;7 voidUp (LL k,ll l);8 voidBuild (LL l,ll r,ll k);9 ll que (ll l,ll r,ll k,ll aa,ll dd);Tentypedefstructpp One { A LL x; - LL y; - LL ID; the LL t; - } SS; -typedefstructtree1 - { + LL x; - LL y; + LL ID; A LL cou; at } SD; - LL m,n; -SS cnt[100005]; - intflag[4*100005]; -SD tree[4*100005]; - using namespacestd; in intMainvoid) - { to LL n,i,j,k,p,q; +scanf"%lld",&n); -      for(i=1; i<=n; i++) the     { *scanf"%lld%lld",&n,&M); $memset (Tree,0,sizeof(tree));Panax Notoginseng          for(j=1; j<=n; J + +) -         { thescanf"%lld%lld",&cnt[j].x,&cnt[j].y); +Cnt[j].id=J; A             if(cnt[j].x==1) the             { +CNT[J].T=CNT[J].Y;//the number to multiply by 1 o'clock operation.  -             } $             Elsecnt[j].t=1;//operation 2 o'clock multiplied by the number equivalent to 1; $         } -Build1N0);//achievements (because each operation has a corresponding operation, so the operation 2 is also put together, the equivalent of the number to multiply is 1) -printf"Case #%lld:\n", i); the          for(j=1; j<=n; J + +) -         {Wuyi             if(cnt[j].x==1) the             { -LL Dd=que (1J0,1, N);//when 1 o'clock ask for some Wuprintf"%lld\n", DD); -             } About             Else if(cnt[j].x==2) $             { -Up (FLAG[CNT[J].Y],J);//when the 2 o'clock breakpoint is updated -LL Dd=que (1J0,1, N);//ask for some -printf"%lld\n", DD); A             } +         } the     } -     return 0; $ } the voidBuild (LL l,ll r,ll k)//Achievements the { thetree[k].x=l; thetree[k].y=R; -     if(l==R) in     { thetree[k].cou=cnt[l].t%M; theflag[l]=K; About         return; the     } the     Else the     { +Build (L, (l+r)/2,2*k+1); -Build ((L+R)/2+1R2*k+2); theTree[k].cou= (tree[2*k+1].cou*tree[2*k+2].cou)%M;Bayi     } the } the voidUp (LL k,ll L)//Breakpoint Update - { -tree[k].cou=1;//the point to be deleted is the equivalent of a multiply thek= (K-1)/2; the      while(k>=0)//update up to root node the     { theTree[k].cou= (tree[2*k+1].cou*tree[2*k+2].cou)%M; -         if(k==0) the         { the              Break; the         }94k= (K-1)/2; the     } the } thell que (ll l,ll r,ll k,ll aa,ll dd)//Ask98 { About     if(l>dd| | r<aa) -     {101         return 1;102     }103     Else if(l<=aa&&r>=dd)104     { the         returnTree[k].cou;106     }107     Else108     {109LL Nx=que (L,r,2*k+1, AA, (AA+DD)/2); theLL Ny=que (L,r,2*k+2, (AA+DD)/2+1, DD);111         return(nx*ny)%M; the     }113  the}

hud-5475 an easy problem (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.