ACM Learning process-hdu5475 An easy problem (line segment tree) (2015 Shanghai online game 08 Questions)

Source: Internet
Author: User

Problem Description

One day, a useless calculator is being built by Kuros. Let's assume that number X was showed on the screen of calculator. At first, X = 1. This calculator only supports and types of operation.
1. Multiply X with a number.
2. Divide X with a number which is multiplied before.
After all operation, please output the number X modulo M.

Input

The first line was an integer T (1≤t≤10), indicating the number of test cases.
For each test case, the first line is a integers q and M. Q is the number ofoperations and M is described above. (1≤q≤105,1≤m≤109)
The next Q lines, each line starts with an integer x indicating the type ofoperation.
If X is 1, a integer y is given, indicating the number to multiply. (0<y≤109)
If X is 2, an integer n is given. The calculator would divide the number whichis multiplied in the nth operation. (The nth operation must be a type 1operation.)

It's guaranteed that's in type 2 operation, there won ' t is the same n.

Output

For each test case, the first line, Pleaseoutput ' case #x: ' and x is the ID of the ' the ' test cases starting from 1.
Then Q lines follow, each of the line "output an" answer showed by Thecalculator.

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

The main problem is the division, which can not be division in the mode condition, unless there is an inverse to achieve division, but here the divisor is not necessarily with dividend coprime.

But if the process is not modeled, it is necessary to use a large number, will be t.

The divisor does not appear the same, considering the topic mentioned.

That is, if you multiply the 2, then the result is composed of 1 and 3, so that you do not have to consider the situation of each number, at this time each number is a whole, the results only and this number has not appeared related.

Therefore, we can consider using segment tree to maintain the product of segments. When a number is removed, all intervals associated with this number are recalculated, with a maximum of log (q) intervals.

Such efficiency is QLOGQ, is satisfies the condition.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Longusing namespacestd;Const intMAXN =100005;intQ, M;intOP[MAXN], top;//Segment Treestructnode{intLT, RT; LL Val;} tree[4*MAXN];//Update upvoidPushup (intID) {Tree[id].val= (tree[id<<1].val*tree[id<<1|1].val)%m;}//Create a line segment treevoidBuildintLtintRtintID) {tree[id].lt=lt; Tree[id].rt=RT; Tree[id].val=1;//the initial value of each paragraph, according to the topic requirements    if(LT = =RT) {        //Tree[id].add =??;        return; }    intMid = (LT+RT) >>1; Build (LT, Mid, id<<1); Build (Mid+1, RT, id<<1|1); Pushup (ID);}voidAddintLtintRtintIdintpls) {    if(LT <= tree[id].lt && RT >=tree[id].rt) {        if(pls) {tree[id].val*=pls; Tree[id].val%=m; }        ElseTree[id].val=1; return; }    intMid = (TREE[ID].LT+TREE[ID].RT) >>1; if(LT <=mid) Add (LT, RT, id<<1, pls); if(Rt >mid) Add (LT, RT, id<<1|1, pls); Pushup (ID);}voidWork () {Build (1Q1); Top=1; intd, y;  for(inti =0; i < Q; ++i) {scanf ("%d%d", &d, &y); if(d = =1) Add (top, Top,1, y); ElseAdd (y, y,1,0); Op[top++] =y; printf ("%i64d\n", tree[1].val); }}intMain () {//freopen ("test.in", "R", stdin);    intT; scanf ("%d", &T);  for(intTimes =1; Times <= T; ++Times ) {printf ("Case #%d:\n", times); scanf ("%d%d", &q, &m);    Work (); }    return 0;}

ACM Learning process-hdu5475 An easy problem (line segment tree) (2015 Shanghai online game 08 Questions)

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.