Multiply game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2765 Accepted Submission(s): 977
Problem Description
Tired of playing computer games, alpc23 is planning to play a game on numbers. Because plus and subtraction is too easy for this gay, he wants to do some multiplication in a number sequence. After playing it a few times, he has found it is also too boring. So he plan to do a more challenge job: he wants to change several numbers in this sequence and also work out the multiplication of all the number in a subsequence of the whole sequence.
To be a friend of this gay, you have been invented by him to play this interesting game with him. Of course, you need to work out the answers faster than him to get a free lunch, He he…
To be a friend of this gay, you have been invented by him to play this interesting game with him. Of course, you need to work out the answers faster than him to get a free lunch, He he…
Input
The first line is 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, they are the initial n numbers of the sequence a1,a2, …,an,
Then the third line is the number of operation q (q<=50000), from the fourth line to the q+3 line are the description of the q operations. They are the one of the two forms:
0 k1 k2; you need to work out the multiplication of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
1 k p; the kth number of the sequence has been change to p. (1<=k<=n)
You can assume that all the numbers before and after the replacement are no larger than 1 million.
For each test case, the first line is the length of sequence n (n<=50000), the second line has n numbers, they are the initial n numbers of the sequence a1,a2, …,an,
Then the third line is the number of operation q (q<=50000), from the fourth line to the q+3 line are the description of the q operations. They are the one of the two forms:
0 k1 k2; you need to work out the multiplication of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
1 k p; the kth number of the sequence has been change to p. (1<=k<=n)
You can assume that all the numbers before and after the replacement are no larger than 1 million.
Output
For each of the first operation, you need to output the answer of multiplication in each line, because the answer can be 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
Source
题意:求区间积。
就是把线段树求和改成线段树求积就行了,然后要记得初始化.
在求和的过程中忘记取模了,一直没发现。。
在求和的过程中忘记取模了,一直没发现。。
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define LL long long
#define lx x<<1
#define rx x<<1|1
#define mid ((L+R)>>1)
const int N = 5e4 + 10;
const LL mod = 1000000007;
int T,n,q;
LL a[N],x,y,z,mul[N<<2];
LL build(int x,int L,int R){
if(L==R) return mul[x] = a[L];
return mul[x] = (build(lx, L, mid) * build(rx, mid+1, R)) % mod;
}
LL update(int x,LL pos,LL val,int L,int R){
if(L==R) return mul[x] = val % mod;
if(pos<=mid) update(lx, pos, val, L, mid);
else update(rx, pos, val, mid+1, R);
return mul[x] = (mul[lx] * mul[rx]) % mod;
}
LL Mul(int x,LL l,LL r,int L,int R){
if(l<=L&&R<=r) return mul[x] % mod;
LL ans = 1;
if(l<=mid) ans = (ans * Mul(lx, l, r, L, mid)) % mod;
if(mid<r) ans = (ans * Mul(rx, l, r, mid+1, R)) % mod;
return ans;
}
int main()
{
scanf("%d",&T);
while(T--){
memset(mul,0,sizeof mul);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1, 1, n);
scanf("%d",&q);
while(q--){
scanf("%lld%lld%lld",&x,&y,&z);
if(x==0){
printf("%lld\n",Mul(1, y, z, 1, n));
}else{
update(1, y, z, 1, n);
}
}
}
return 0;
}