zoukankan      html  css  js  c++  java
  • 线段树 求区间连乘——hdu 3074 Multiply game

    Multiply game

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


    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
    #define mod 1000000007
    #include<iostream>
    using namespace std;
    #include<cstdio>
    #include<cstring>
    #define N 50010
    struct Tree{
        long long sum;
    }tree[N*4];/*数组要开到四倍,因为会有许多空节点使用不到*/
    int n,t,q,k1,k2,p,x;
    int a[N];
    void update(int k)
    {
        int lch=k<<1,rch=(k<<1)+1;/*不知道为甚,宏定义会出错误*/
        tree[k].sum=(tree[lch].sum*tree[rch].sum)%mod;
    }
    void build_tree(int k,int l,int r)
    {
        int lch=k<<1,rch=(k<<1)+1,mid=(l+r)>>1;
        if(l==r)
        {
            tree[k].sum=a[l]%mod;
            return ; 
        }
        build_tree(lch,l,mid);
        build_tree(rch,mid+1,r);
        update(k);
    }
    long long query(int k,int l,int r,int k1,int k2)
    {
        int lch=k<<1,rch=(k<<1)+1,mid=(l+r)>>1;
        if(k1<=l&&r<=k2)
        {
            return tree[k].sum%mod;
        }
        long long ans=1;
        if(k1<=mid)
          ans=(ans*query(lch,l,mid,k1,k2))%mod;
        if(k2>mid)
          ans=(ans*query(rch,mid+1,r,k1,k2))%mod;
        return ans;
    }
    void change(int k,int l,int r,int pos,int pl)
    {
        int lch=k<<1,rch=(k<<1)+1,mid=(l+r)>>1;
        if(l==r)/*一开始加了懒惰标记,结果下传的时候处理的和区间下传一样了,结果错了,改为直接找到单点,再往回更新*/
        {
            tree[k].sum=pl;
            return;
        }
        if(pos<=mid)
          change(lch,l,mid,pos,pl);
        else change(rch,mid+1,r,pos,pl);
        update(k);
    }
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            memset(tree,0,sizeof(tree));
            memset(a,0,sizeof(a));
            scanf("%d",&n);
            for(int i=1;i<=n;++i)
            {
                scanf("%d",&a[i]);
             } 
            build_tree(1,1,n);
            scanf("%d",&q);
            for(int i=1;i<=q;++i)
            {
                scanf("%d",&x);
                if(x==0)
                {
                    scanf("%d%d",&k1,&k2); 
                    cout<<query(1,1,n,k1,k2)<<endl;
                }
                else 
                {
                    scanf("%d%d",&k1,&p);
                    change(1,1,n,k1,p);
                    a[k1]=p; 
                }
            }
        
        }
        return 0;
    }
     
     
  • 相关阅读:
    VS2005中乱码问题
    android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
    android中的ellipsize设置(省略号的问题)
    手机web——自适应网页设计(html/css控制)
    Android横屏竖屏切换的问题
    android webview js alert对话框 不能弹出 解决办法
    WebView点击加载的页面中的按钮时不弹出新窗口以及在加载后执行javascript
    MVC学习笔记---MVC需要用的那些知识点
    CLR via C#学习笔记---类型
    C#学习笔记---协变和逆变
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5479802.html
Copyright © 2011-2022 走看看