大意:给一个数组,先求出SUM[I],然后动态的求出1-I的SUM[I]的和,

这题得化公式:

树状数组维护两个和:SUM(A[I])(1<=I<=X);

SUM(A[I]*(N-I+1)) (1<=I<=X);

答案就是:SUM(A[I]*(N-I+1))-SUM[A[I]]*(N-X) (1<=I<=X);

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<cmath>
  5. using namespace std;
  6. typedef long long ll;
  7. const int N=;
  8. ll s[N],t[N];
  9. int a[N];
  10. int n;
  11.  
  12. int lowbit(int x)
  13. {
  14. return x&(-x);
  15. }
  16. void update1(int x,ll v)
  17. {
  18. while (x<=n)
  19. {
  20. s[x]+=v;
  21. x+=lowbit(x);
  22. }
  23. }
  24.  
  25. void update2(int x,ll v)
  26. {
  27. while (x<=n)
  28. {
  29. t[x]+=v;
  30. x+=lowbit(x);
  31. }
  32. }
  33.  
  34. ll sum1(int x)
  35. {
  36. ll ans=;
  37. while (x)
  38. {
  39. ans+=s[x];
  40. x-=lowbit(x);
  41. }
  42. return ans;
  43. }
  44. ll sum2(int x)
  45. {
  46. ll ans=;
  47. while (x)
  48. {
  49. ans+=t[x];
  50. x-=lowbit(x);
  51. }
  52. return ans;
  53. }
  54.  
  55. int main()
  56. {
  57. int m;
  58. char s[];
  59. scanf("%d%d",&n,&m);
  60. for (int i=;i<=n;i++)
  61. {
  62. scanf("%d",&a[i]);
  63. update1(i,a[i]);
  64. update2(i,(ll)a[i]*(n-i+));
  65. }
  66.  
  67. while (m--)
  68. {
  69. int x,y;
  70. scanf("%s%d",s,&x);
  71. if (s[]=='Q')
  72. {
  73. printf("%lld\n",sum2(x)-sum1(x)*(n-x));
  74. }
  75. else
  76. {
  77. scanf("%d",&y);
  78. update1(x,y-a[x]);
  79. update2(x,(ll)(y-a[x])*(n-x+));
  80. a[x]=y;
  81. }
  82. }
  83. return ;
  84. }

BZOJ 3155: Preprefix sum的更多相关文章

  1. BZOJ 3155: Preprefix sum( 线段树 )

    刷刷水题... 前缀和的前缀和...显然树状数组可以写...然而我不会, 只能写线段树了 把改变成加, 然后线段树维护前缀和, 某点p加, 会影响前缀和pre(x)(p≤x≤n), 对[p, n]这段 ...

  2. 3155: Preprefix sum

    3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...

  3. [bzoj3155]Preprefix sum(树状数组)

    3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1183  Solved: 546[Submit][Status] ...

  4. 树状数组【bzoj3155】: Preprefix sum

    3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了 ...

  5. Preprefix sum BZOJ 3155 树状数组

    题目描述 前缀和(prefix sum)Si=∑k=1iaiS_i=\sum_{k=1}^i a_iSi​=∑k=1i​ai​. 前前缀和(preprefix sum) 则把SiS_iSi​作为原序列 ...

  6. 差分+树状数组【p4868】Preprefix sum

    Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前 ...

  7. 2021.08.09 P4868 Preprefix sum(树状数组)

    2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...

  8. BZOJ3155: Preprefix sum

    题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath& ...

  9. BZOJ3155:Preprefix sum——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3155 最朴素的想法是两棵树状数组,一个记录前缀和,一个记录前缀前缀和,但是第二个我们非常不好修改 ...

随机推荐

  1. Delphi For Android 开发笔记 2 NEXTGEN下的字符串类型

    delphi开发速度迅捷至少有30%(猜的,呵呵)的原因是因为其字符串(string.WideString.PChar.PAnsiChar等)处理能力. 而从delphi XE4开始,在system等 ...

  2. virtualenv python虚拟环境搭建

    python virtualenv.py flask

  3. oracle 查询今天哪个表增加的数据多

    一.创建一个表  create table A(  TABLE_NAME VARCHAR2(200),  COUNT_NUM  NUMBER) 二.创建一个存储过程create or replace  ...

  4. lucene .NET 搜索图片 功能实现

    关于搜索部分 1想建立索引.构建jpg图片解析器,在索引时将jpg图片的exif信息及其文本信息如名称,存放路径,大小,日期等等加入索引!具体实现代码如下: public void BulidInde ...

  5. Golang与MySQL

    1. 在golib下载go-sql-driver/mysql go get github.com/go-sql-driver/mysql 2. 代码引入 import ( "database ...

  6. openssl AES加密以及padding

    好习惯,先上代码再说事 加密 void AesEncrypt(unsigned char* pchIn, int nInLen, unsigned char *ciphertext, int & ...

  7. mvc4 http错误403.14 forbidden

    1. 检查服务器上是否安装了“HTTP重定向”功能和“静态内容压缩”功能(在添加/删除程序或增加角色处安装). 2. 应用程序池要被配置为“集成” 3. 把.net 4.0安装在iis上 4. 确保自 ...

  8. (转)前端构建工具gulp入门教程

    前端构建工具gulp入门教程 老婆婆 1.8k 2013年12月30日 发布 推荐 10 推荐 收藏 83 收藏,20k 浏览 本文假设你之前没有用过任何任务脚本(task runner)和命令行工具 ...

  9. 学习IOS需要知道的事

    什么是iOS iOS是一款由苹果公司开发的操作系统(OS是Operating System的简称),就像平时在电脑上用的Windows XP.Windows 7,都是操作系统 那什么是操作系统呢?操作 ...

  10. 55.ERROR:Place:1136 - This design contains a global buffer instance…… non-clock load pins off chip

    ISE在布局布线时,出现下图所示错误. 对于"clock_dedicated_route”错误原因有两种情况: 1.  就是有一个时钟你没有放到全局时钟或者局部时钟的引脚,布局的时候不能把它 ...