Multiply Game
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1820 Accepted Submission (s): 611
Problem descriptiontired of playing computer games, Alpc23 are planning to play a game on numbers. Because Plus and subtraction are too easy for this gay, the he wants to does some multiplication in a number sequence. After playing it a few times, he had found it is also too boring. So he plan to does a more challenge Job:he wants to change several numbers in this sequence and also work out the Multiplic ation of all the number in a subsequence of the whole sequence.
To is a friend of this gay, you had been invented by him to play this interesting game with him. Of course, you need-to-work-out the answers faster than him-get a free lunch, he he ...
Inputthe first line was the number of case T (t<=10).
For each test case, the first line is the length of sequence n (n<=50000), the second line has n numbers, and they is the Initial n numbers of the sequence a1,a2, ..., an,
Then the third line was the number of operation Q (q<=50000), from the fourth line to the Q+3 line was the description o f The Q operations. They is the one of the forms:
0 K1 K2; You need the multiplication of the subsequence from K1 to K2, inclusive. (1<=k1<=k2<=n)
1 k p; The kth number of the sequence have been change to P. (1<=k<=n)
You can assume this all the numbers before and after the replacement is no larger than 1 million.
Outputfor each of the first operation, your need to output the answer of multiplication on each line, because the answer CA N is very large, so can only output the answer after MoD 1000000007.
Sample Input
161 2 4 5 6 330 2 51 3 70 2 5
Sample Output
240420
Source2009 multi-university Training Contest 17-host by Nudt
The main idea: to give some number, according to the different requirements of the point update or interval calculation. So it's a simple line-up tree template problem.
#include <stdio.h> #include <string.h> #define N 500005#define mod 1000000007struct tree{int l,r;__int64 sum, add;} Tree[n<<2];void pushup (int root) {if (TREE[ROOT].L==TREE[ROOT].R) return; Tree[root].sum=tree[root<<1]. Sum*tree[root<<1|1].sum%mod;return;} void pushdown (int root) {if (TREE[ROOT].L==TREE[ROOT].R) return, if (tree[root].add==-1) return; Tree[root<<1]. Add=tree[root<<1|1].add=tree[root].add;tree[root<<1].sum= (tree[root<<1].r-tree[root<<1 ].l+1) *tree[root].add;tree[root<<1|1].sum= (tree[root<<1|1].r-tree[root<<1|1].l+1) *tree[root] . Add;tree[root].add=-1;return;} void update (int l,int r,int z,int root) {if (L==TREE[ROOT].L&&R==TREE[ROOT].R) {tree[root].sum= (Tree[root]. r-tree[root].l+1) *z;tree[root].add=z;return;} Pushdown (root), int mid=tree[root].l+tree[root].r>>1;if (r<=mid) Update (l,r,z,root<<1), else if (l> MID) Update (L,R,Z,ROOT<<1|1), else {update (l,mid,z,root<<1); Update (Mid+1,r,z,rooT<<1|1);} Pushup (root); return;} void build (int l,int r,int root) {tree[root].l=l;tree[root].r=r;tree[root].sum=0;tree[root].add=-1;if (l==r) {tree[ Root].sum=1;return;} int mid= (L+R) >>1;build (l,mid,root<<1); build (mid+1,r,root<<1|1);p ushup (root); return;} __int64 query (int l,int r,int root) {if (L==TREE[ROOT].L&&R==TREE[ROOT].R) return Tree[root].sum;pushdown (root ); int mid=tree[root].l+tree[root].r>>1;if (R<=MID) return query (l,r,root<<1); else if (L>mid) return Query (l,r,root<<1|1), Else return query (l,mid,root<<1) *query (mid+1,r,root<<1|1)%mod;} int main () {int t,i,j,k,n,q,k1,k2,a[50005],caozuo;scanf ("%d", &t), while (t--) {scanf ("%d", &n); build (1,n,1); for (i=1;i<=n;i++) {scanf ("%d", &a[i]); update (i,i,a[i],1);} scanf ("%d", &q), while (q--) {scanf ("%d%d%d", &caozuo,&k1,&k2), if (caozuo==0) {printf ("%i64d\n", query (k1,k2,1));} if (caozuo==1) update (k1,k1,k2,1);}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 3074 Multiply Game (segment tree)