比较基础的线段树,1A。

线段树:

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 using namespace std;
  5
  6 typedef long long ll;
  7 const int N = 50001;
  8 const int MOD = 1000000007;
  9 int a[N];
 10
 11 struct Node
 12 {
 13     int l, r, s;
 14 } node[N << 2];
 15
 16 void pushup( int i )
 17 {
 18     node[i].s = ( ll ) node[i << 1].s * node[i << 1 | 1].s % MOD;
 19 }
 20
 21 void build( int i, int l, int r )
 22 {
 23     node[i].l = l, node[i].r = r;
 24     if ( l == r )
 25     {
 26         node[i].s = a[l];
 27         return ;
 28     }
 29     int mid = ( l + r ) >> 1;
 30     build( i << 1, l, mid );
 31     build( i << 1 | 1, mid + 1, r );
 32     pushup(i);
 33 }
 34
 35 void update( int i, int pos, int val )
 36 {
 37     if ( node[i].l == pos && node[i].r == pos )
 38     {
 39         node[i].s = val;
 40         return ;
 41     }
 42     int mid = ( node[i].l + node[i].r ) >> 1;
 43     if ( pos <= mid )
 44     {
 45         update( i << 1, pos, val );
 46     }
 47     else
 48     {
 49         update( i << 1 | 1, pos, val );
 50     }
 51     pushup(i);
 52 }
 53
 54 int query( int i, int l, int r )
 55 {
 56     if ( node[i].l == l && node[i].r == r ) return node[i].s;
 57     int mid = ( node[i].l + node[i].r ) >> 1;
 58     if ( r <= mid )
 59     {
 60         return query( i << 1, l, r );
 61     }
 62     else if ( l > mid )
 63     {
 64         return query( i << 1 | 1, l, r );
 65     }
 66     else
 67     {
 68         return ( ll ) query( i << 1, l, mid ) * query( i << 1 | 1, mid + 1, r ) % MOD;
 69     }
 70 }
 71
 72 int main ()
 73 {
 74     int t;
 75     scanf("%d", &t);
 76     while ( t-- )
 77     {
 78         int n, m;
 79         scanf("%d", &n);
 80         for ( int i = 1; i <= n; i++ )
 81         {
 82             scanf("%d", a + i);
 83         }
 84         build( 1, 1, n );
 85         scanf("%d", &m);
 86         while ( m-- )
 87         {
 88             int op, a, b;
 89             scanf("%d%d%d", &op, &a, &b );
 90             if ( op == 0 )
 91             {
 92                 printf("%d\n", query( 1, a, b ));
 93             }
 94             else
 95             {
 96                 update( 1, a, b );
 97             }
 98         }
 99     }
100     return 0;
101 }

当然,对于这种单点修改和查询区间“前缀和”的问题,我们自然也可以用树状数组来做,不过要用到乘法逆元。

对于这道题来说,树状数组反而难写,并且时间上也没有什么优势。

不过这是一种思想,将“加法”扩展到了“乘法”。

也可以考虑用log把乘法变成加法,不过可能会有精度问题。

树状数组:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5
 6 typedef long long ll;
 7 const int N = 50001;
 8 const int MOD = 1000000007;
 9 int a[N];
10 int c[N];
11
12 void extgcd( int a, int mod, int &x, int &y )
13 {
14     if ( !mod )
15     {
16         x = 1, y = 0;
17     }
18     else
19     {
20         extgcd( mod, a % mod, y, x );
21         y -= x * ( a / mod );
22     }
23 }
24
25 int lb( int i )
26 {
27     return i & -i;
28 }
29
30 void update( int i, int x )
31 {
32     while ( i < N )
33     {
34         c[i] = ( ll ) c[i] * x % MOD;
35         i += lb(i);
36     }
37 }
38
39 int get( int i )
40 {
41     int ans = 1;
42     while ( i )
43     {
44         ans = ( ll ) ans * c[i] % MOD;
45         i -= lb(i);
46     }
47     return ans;
48 }
49
50 int main ()
51 {
52     int t;
53     scanf("%d", &t);
54     while ( t-- )
55     {
56         int n, m;
57         scanf("%d", &n);
58         for ( int i = 1; i <= n; i++ )
59         {
60             c[i] = 1;
61         }
62         for ( int i = 1; i <= n; i++ )
63         {
64             scanf("%d", a + i);
65             update( i, a[i] );
66         }
67         scanf("%d", &m);
68         while ( m-- )
69         {
70             int op, x, y, inv, useless;
71             scanf("%d%d%d", &op, &x, &y );
72             if ( op == 0 )
73             {
74                 extgcd( get( x - 1 ), MOD, inv, useless );
75                 inv = ( inv + MOD ) % MOD;
76                 int tmp = ( ll ) inv * get(y) % MOD;
77                 printf("%d\n", tmp);
78             }
79             else
80             {
81                 extgcd( a[x], MOD, inv, useless );
82                 inv = ( inv + MOD ) % MOD;
83                 inv = ( ll ) inv * y % MOD;
84                 a[x] = ( ll ) a[x] * inv % MOD;
85                 update( x, inv );
86             }
87         }
88     }
89     return 0;
90 }

转载于:https://www.cnblogs.com/huoxiayu/p/4692270.html

hdu 3074 线段树 OR 树状数组相关推荐

  1. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  2. hdu 4417(线段树OR树状数组)

    题意:输入一个长度为n的序列,然后m个询问,询问区间[a,b]中比h小的数的个数. 思路:树状数组或线段树离线处理. 树状数组1 View Code 1 #include<cstdio> ...

  3. HDU 1556 前缀和 树状数组 线段树

    解法一: a[i]表示以 i作为起点,对 i-n的气球全部上色的次数  对(start,end)区间上色 ++a[start] --a[end+1]抵消掉 end+1-n的部分 问题转换为求 a的前缀 ...

  4. HDU - 6183 Color it(动态开点线段树/树状数组套动态开点线段树)

    题目链接:点击查看 题目大意:给出一个二维平面坐标系,需要完成四种操作: 0:删除所有点 1 xycx\ y\ cx y c:在点 (x,y)(x,y)(x,y) 处添加颜色 ccc 2 xy1y2x ...

  5. Minimum Inversion Number HDU - 1394(权值线段树/树状数组)

    The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that s ...

  6. HDU 1394 线段树or 树状数组~

    Minimum Inversion Number Description The inversion number of a given number sequence a1, a2, ..., an ...

  7. hdu 4836 The Query on the Tree(线段树or树状数组)

    The Query on the Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)

    Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...

  9. HDU 4417 Super Mario(离线线段树or树状数组)

    Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ...

最新文章

  1. 吃货告诉你IaaS、PaaS、SaaS之间的区别
  2. DLL延迟加载工程分析
  3. stl clocklist 查找元素_剑指信奥 C++ 之 STL 迭代器(上)
  4. 开学综合症有救了!17篇最新AI论文不容错过
  5. 中设置colorbar_【值得收藏】如何画出学术论文中需要的各种精美插图,看这一篇就够了!...
  6. java stream optional_java成神之——Stream和Optional
  7. 谷歌浏览器32位安装包_谷歌浏览器发布紧急安全更新修复Blink内核中的任意代码执行漏洞...
  8. 生死狙击服务器名字怎么修改器,生死狙击修改金币方法
  9. 使用idea导入远程git版本库项目
  10. Fanvas, 把swf文件转html5 canvas js软件工具程序
  11. 【工程源码】使用华邦的SPI FLASH作为EPCS时固化NIOS II软件报错及解决方案
  12. 6.插入脚注后,正文空一行
  13. linux中的man命令总结
  14. 实现支付宝AR扫描动画效果
  15. 火影忍者之~静音 (优先队列)
  16. 频率f,角频率Ω和数字频率w的物理含义
  17. Velocity模板引擎
  18. 游戏里的小哥哥小姐姐都是怎么来的?
  19. 温度控制直流电机转速
  20. Computing Trajectory Similarity in Linear Time: A Generic Seed-Guided Neural Metric Learning Appr...

热门文章

  1. AtomicStampedReference源码分析
  2. [LeetCode] 3. Longest Substring Without Repeating Characters 题解
  3. ZooKeeper典型应用场
  4. 在Windows上编译MongoDB C Driver
  5. 第8章 Service基础Activity与Service绑定
  6. 使用机器学习预测天气_如何使用机器学习预测着陆
  7. leetcode945. 使数组唯一的最小增量(排序)
  8. 如何使用React,TypeScript和React测试库创建出色的用户体验
  9. 如何在Python中建立和训练K最近邻和K-Means集群ML模型
  10. 33岁想从头学做网页设计_从头开始设计精美的移动应用