3074 Multiply game(線段樹求區間乘積)
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…
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.
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
1
6
1 2 4 5 6 3
3
0 2 5
1 3 7
0 2 5
Sample Output
240
420
線段樹求區間乘積,把原來的加變成乘積就行了
#include<bits/stdc++.h>
using namespace std;
using LL =int64_t;
const int maxn=5e4+5;
const int mod=1e9+7;
vector<LL>A(maxn,0);
struct Node {
int l,r;
LL sum;
}E[4*maxn];
LL creat(int num,int l,int r) {
E[num].r=r,E[num].l=l;
int mid=(l+r)/2;
if(l==r) E[num].sum=A[r]%mod;
else E[num].sum=(creat(2*num,l,mid)*creat(2*num+1,mid+1,r))%mod;
return E[num].sum;
}
LL query(int num,int l,int r,int x,int y) {
if(x<=l&&y>=r) return E[num].sum;
int mid=(l+r)/2,ans=1;
if(x<=mid) ans=ans*query(2*num,l,mid,x,y)%mod;
if(y>mid) ans=(ans*query(2*num+1,mid+1,r,x,y)%mod);
return ans;
}
void update(int num,int l,int r,int x,int y) {
int mid=(l+r)/2;
if(l==r&&l==x) {
E[num].sum=y;
return ;
}
else {
if(x<=mid) update(2*num,l,mid,x,y);
else update(2*num+1,mid+1,r,x,y);
}
E[num].sum=(E[2*num].sum*E[2*num+1].sum)%mod;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T,n;
cin>>T;
for(int k=0;k<T;k++) {
cin>>n;
for(int i=1;i<=n;i++) {
cin>>A[i];
}
creat(1,1,n);
int m,x,y,flag;
cin>>m;
for(int i=0;i<m;i++) {
cin>>flag;
if(flag==0) {
cin>>x>>y;
cout<<(query(1,1,n,x,y)+mod)%mod<<endl;
}
else if(flag==1) {
cin>>x>>y;
update(1,1,n,x,y);
}
}
}
return 0;
}