BZOJ 3155: Preprefix sum
大意:给一个数组,先求出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);
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- typedef long long ll;
- const int N=;
- ll s[N],t[N];
- int a[N];
- int n;
- int lowbit(int x)
- {
- return x&(-x);
- }
- void update1(int x,ll v)
- {
- while (x<=n)
- {
- s[x]+=v;
- x+=lowbit(x);
- }
- }
- void update2(int x,ll v)
- {
- while (x<=n)
- {
- t[x]+=v;
- x+=lowbit(x);
- }
- }
- ll sum1(int x)
- {
- ll ans=;
- while (x)
- {
- ans+=s[x];
- x-=lowbit(x);
- }
- return ans;
- }
- ll sum2(int x)
- {
- ll ans=;
- while (x)
- {
- ans+=t[x];
- x-=lowbit(x);
- }
- return ans;
- }
- int main()
- {
- int m;
- char s[];
- scanf("%d%d",&n,&m);
- for (int i=;i<=n;i++)
- {
- scanf("%d",&a[i]);
- update1(i,a[i]);
- update2(i,(ll)a[i]*(n-i+));
- }
- while (m--)
- {
- int x,y;
- scanf("%s%d",s,&x);
- if (s[]=='Q')
- {
- printf("%lld\n",sum2(x)-sum1(x)*(n-x));
- }
- else
- {
- scanf("%d",&y);
- update1(x,y-a[x]);
- update2(x,(ll)(y-a[x])*(n-x+));
- a[x]=y;
- }
- }
- return ;
- }
BZOJ 3155: Preprefix sum的更多相关文章
- BZOJ 3155: Preprefix sum( 线段树 )
刷刷水题... 前缀和的前缀和...显然树状数组可以写...然而我不会, 只能写线段树了 把改变成加, 然后线段树维护前缀和, 某点p加, 会影响前缀和pre(x)(p≤x≤n), 对[p, n]这段 ...
- 3155: Preprefix sum
3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...
- [bzoj3155]Preprefix sum(树状数组)
3155: Preprefix sum Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 1183 Solved: 546[Submit][Status] ...
- 树状数组【bzoj3155】: Preprefix sum
3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了 ...
- Preprefix sum BZOJ 3155 树状数组
题目描述 前缀和(prefix sum)Si=∑k=1iaiS_i=\sum_{k=1}^i a_iSi=∑k=1iai. 前前缀和(preprefix sum) 则把SiS_iSi作为原序列 ...
- 差分+树状数组【p4868】Preprefix sum
Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前 ...
- 2021.08.09 P4868 Preprefix sum(树状数组)
2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...
- BZOJ3155: Preprefix sum
题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath& ...
- BZOJ3155:Preprefix sum——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3155 最朴素的想法是两棵树状数组,一个记录前缀和,一个记录前缀前缀和,但是第二个我们非常不好修改 ...
随机推荐
- Delphi For Android 开发笔记 2 NEXTGEN下的字符串类型
delphi开发速度迅捷至少有30%(猜的,呵呵)的原因是因为其字符串(string.WideString.PChar.PAnsiChar等)处理能力. 而从delphi XE4开始,在system等 ...
- virtualenv python虚拟环境搭建
python virtualenv.py flask
- oracle 查询今天哪个表增加的数据多
一.创建一个表 create table A( TABLE_NAME VARCHAR2(200), COUNT_NUM NUMBER) 二.创建一个存储过程create or replace ...
- lucene .NET 搜索图片 功能实现
关于搜索部分 1想建立索引.构建jpg图片解析器,在索引时将jpg图片的exif信息及其文本信息如名称,存放路径,大小,日期等等加入索引!具体实现代码如下: public void BulidInde ...
- Golang与MySQL
1. 在golib下载go-sql-driver/mysql go get github.com/go-sql-driver/mysql 2. 代码引入 import ( "database ...
- openssl AES加密以及padding
好习惯,先上代码再说事 加密 void AesEncrypt(unsigned char* pchIn, int nInLen, unsigned char *ciphertext, int & ...
- mvc4 http错误403.14 forbidden
1. 检查服务器上是否安装了“HTTP重定向”功能和“静态内容压缩”功能(在添加/删除程序或增加角色处安装). 2. 应用程序池要被配置为“集成” 3. 把.net 4.0安装在iis上 4. 确保自 ...
- (转)前端构建工具gulp入门教程
前端构建工具gulp入门教程 老婆婆 1.8k 2013年12月30日 发布 推荐 10 推荐 收藏 83 收藏,20k 浏览 本文假设你之前没有用过任何任务脚本(task runner)和命令行工具 ...
- 学习IOS需要知道的事
什么是iOS iOS是一款由苹果公司开发的操作系统(OS是Operating System的简称),就像平时在电脑上用的Windows XP.Windows 7,都是操作系统 那什么是操作系统呢?操作 ...
- 55.ERROR:Place:1136 - This design contains a global buffer instance…… non-clock load pins off chip
ISE在布局布线时,出现下图所示错误. 对于"clock_dedicated_route”错误原因有两种情况: 1. 就是有一个时钟你没有放到全局时钟或者局部时钟的引脚,布局的时候不能把它 ...