zoukankan      html  css  js  c++  java
  • HDU 3074 Multiply game

    Multiply game

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 3074
    64-bit integer IO format: %I64d      Java class name: Main
    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

     
    解题:线段树
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 50010;
     5 const LL mod = 1000000007;
     6 LL tree[maxn<<2];
     7 void build(int L,int R,int v){
     8     if(L == R){
     9         scanf("%I64d",tree+v);
    10         return;
    11     }
    12     int mid = (L + R)>>1;
    13     build(L,mid,v<<1);
    14     build(mid+1,R,v<<1|1);
    15     tree[v] = tree[v<<1]*tree[v<<1|1]%mod;
    16 }
    17 void update(int L,int R,int lt,int rt,int val,int v){
    18     if(lt <= L && rt >= R) {
    19         tree[v] = val;
    20         return;
    21     }
    22     int mid = (L + R)>>1;
    23     if(lt <= mid) update(L,mid,lt,rt,val,v<<1);
    24     if(rt > mid) update(mid+1,R,lt,rt,val,v<<1|1);
    25     tree[v] = tree[v<<1]*tree[v<<1|1]%mod;
    26 }
    27 LL query(int L,int R,int lt,int rt,int v){
    28     if(lt <= L && rt >= R) return tree[v];
    29     int mid = (L + R)>>1;
    30     LL ans = 1;
    31     if(lt <= mid) ans *= query(L,mid,lt,rt,v<<1);
    32     if(rt > mid) ans *= query(mid+1,R,lt,rt,v<<1|1);
    33     return ans%mod;
    34 }
    35 int main(){
    36     int T,n,m,op,a,b;
    37     scanf("%d",&T);
    38     while(T--){
    39         scanf("%d",&n);
    40         build(1,n,1);
    41         scanf("%d",&m);
    42         while(m--){
    43             scanf("%d %d %d",&op,&a,&b);
    44             if(op) update(1,n,a,a,b,1);
    45             else printf("%I64d
    ",query(1,n,a,b,1));
    46         }
    47     }
    48     return 0;
    49 }
    View Code
  • 相关阅读:
    varnish缓存清理
    How to change default root@ email address linux / postfix / centos?
    crontab每小时运行一次
    awk 统计文件中按照某列统计某列的和(sum)
    Linux下查看压缩文件内容的 10 种方法
    python 脚本定时删除 elk索引
    python 如何让字符串的不具有转义的反斜杠具有转义功能
    Linux安装redis和部署
    redis requires Ruby version >= 2.2.2问题
    Redis持久化--Redis宕机或者出现意外删库导致数据丢失--解决方案
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4437583.html
Copyright © 2011-2022 走看看