HDU 3074 Multiply game(線段樹 單點更新)

Mr_Treeeee發表於2020-04-06

Multiply game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2687    Accepted Submission(s): 949


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
 

Source
 
題意:
求線段樹區間乘積,有更改操作。
POINT:
LL,別的其實和求和一樣。本以為要用什麼費馬小定理啥的,寫著寫著發現線段樹一次一次更新就行了,區間除以的時候可能適用。水題。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define rt x<<1|1
#define lt x<<1
const int N = 50000*8;
#define  LL long long
const LL p = 1000000007;
LL sum[N];
LL a[N/8];
void init()
{
    
}
void build(int x,int l,int r)
{
    if(l==r) (sum[x]=a[l])%=p;
    else
    {
        int mid=(l+r)>>1;
        build(lt,l,mid);
        build(rt,mid+1,r);
        (sum[x]=sum[lt]*sum[rt])%=p;
    }
}
void change(int x,int l,int r,int i,LL k)
{
    if(l==r&&r==i) (sum[x]=k)%=p;
    else
    {
        int mid=(l+r)>>1;
        if(i<=mid) change(lt,l,mid,i,k);
        if(mid<i) change(rt,mid+1,r,i,k);
        (sum[x]=sum[lt]*sum[rt])%=p;
    }
}
LL query(int x,int l,int r,int ll,int rr)
{
    LL ans=1;
    if(ll<=l&&rr>=r) (ans*=sum[x])%=p;
    else
    {
        int mid=(l+r)>>1;
        if(ll<=mid) (ans*=query(lt,l,mid,ll,rr))%=p;
        if(mid<rr) (ans*=query(rt,mid+1,r,ll,rr))%=p;
    }
    return ans;
    
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        build(1,1,n);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int k,k1,k2;
            scanf("%d %d %d",&k,&k1,&k2);
            if(k)
            {
                change(1,1,n,k1,(LL)k2);
            }
            else
            {
                LL ans=0;
                ans=query(1,1,n,k1,k2);
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}


相關文章