HDU 5475 Analogue Calculator multiplication (2015 Shanghai online race F segment tree)

Source: Internet
Author: User

Given how many operations and mod initial values are 1
Operation 1 y means multiply y
Operation 2 y means dividing by the number of times the y operation is multiplied.

The leaf node I of the segment tree indicates that the number of times I operation multiplied by 1 is replaced by Y.
When you encounter operation 2, replace the value of node I with 1.
Using the nature of the segment tree, the entire 1~n interval is maintained, the value of each output SUM[1] can be


Sample Input
1
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7

Sample Output
Case #1:
2
1
2
20
10
1
6
42
504
84

1# include <iostream>2# include <cstdio>3# include <cstring>4# include <algorithm>5# include <string>6# include <cmath>7# include <queue>8# include <list>9# define LLLong LongTen using namespacestd; One  A Const intMAXN =100005; - intMOD; -  theLL sum[maxn<<2] ;//4 times times the knot. -  - voidPushup (intRt//Update to parent node - { +SUM[RT] = (SUM[RT *2] * SUM[RT *2+1])% MOD;//RT is the current node. -  + } A  at voidBuildintLintRintRt//Building a segment tree - { -     if(L = =R) -     { -SUM[RT] =1 ; -         return ; in     } -     intm = (L + r)/2 ; toBuild (L, M, RT *2) ; +Build (M +1, R, RT *2+1) ; - pushup (RT); the } *  $ voidUpdata (intP, LL V,intLintRintRT)Panax Notoginseng { -     if(L = =R) the     { +SUM[RT] = v%MOD; A         return ; the     } +     intm = (L + r)/2 ; -     if(P <=m) $Updata (P, V, L, M, RT *2) ; $     Else -Updata (P, V, M +1, R, RT *2+1) ; - pushup (RT); the } - Wuyi  the intMain () - { Wu     //freopen ("In.txt", "R", stdin); -     intT; Aboutscanf"%d", &T); $     intCase =0 ; -      while(t--) -     { -case++ ; A         intN; +scanf"%d%d", &n, &MOD); theBuild1N1) ; -         intI, op; $ LL y; theprintf"Case #%d:\n", case); the          for(i =1; I <= N; i++) the         { thescanf"%d%i64d", &op, &y); -             if(OP = =1) inUpdata (i, Y,1N1) ; the             if(OP = =2) theUpdata (Y,1,1N1) ; Aboutprintf"%i64d\n", sum[1]); the         } the     } the     return 0; +}
View Code

HDU 5475 Analogue Calculator multiplication (2015 Shanghai online race F 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.