3155: Preprefix sum

Time Limit: 1 Sec  Memory Limit: 512 MB
Submit: 1183  Solved: 546
[Submit][Status][Discuss]

Description

 

Input

第一行给出两个整数N,M。分别表示序列长度和操作个数
接下来一行有N个数,即给定的序列a1,a2,....an
接下来M行,每行对应一个操作,格式见题目描述

Output

对于每个询问操作,输出一行,表示所询问的SSi的值。

Sample Input

5 3
1 2 3 4 5
Query 5
Modify 3 2
Query 5

Sample Output

35
32

HINT

1<=N,M<=100000,且在任意时刻0<=Ai<=100000

Source

Katharon+#1

学过线段树都知道树状数组不能处理区间修改,无逆元的区间加法

但是树状数组其实用差分可以做区间修改单点查询

当然这道题和更强的区间修改求和关系不大,但形式确实很像

对于原数列a1,a2,a3,a4...

S为  1*a1, 1*a1+1*a2, 1*a1+1*a2+1*a3...

SS为1*a1, 2*a1+1*a2, 3*a1+2*a2+1*a3...

观察系数,发现从大到小变化,但序号却由小到大

比较一下,可以尝试把S乘一个i,消掉系数最大的

得到  1*a1, 2*a1+2*a2, 3*a1+3*a2+3*a3...

这样与SS作差,就可以又得到一个系数与序号正比的式子

0*a1, 0*a1+1*a2, 0*a1+1*a2+2*a3...

再观察,这就是个前缀和而已

所以维护一遍原前缀和,再维护(i-1)*a[i]的前缀和即可

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LL long long
int n,m;
LL a[],bit1[],bit2[];
int lb(int x){
return x&(-x);
}
LL q1(int x){
LL ans=;
while(x){
ans+=bit1[x];
x-=lb(x);
}
return ans;
}
LL q2(int x){
LL ans=;
while(x){
ans+=bit2[x];
x-=lb(x);
}
return ans;
}
int c1(int x,LL num){
while(x<=n){
bit1[x]+=num;
x+=lb(x);
}
return ;
}
int c2(int x,LL num){
while(x<=n){
bit2[x]+=num;
x+=lb(x);
}
return ;
}
int main(){
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
c1(i,a[i]);
c2(i,(i-)*a[i]);
}
for(int i=;i<=m;i++){
char in[];
scanf("%s",in);
if(in[]=='Q'){
int x;
scanf("%d",&x);
printf("%lld\n",x*q1(x)-q2(x));
}else{
int x;
LL y;
scanf("%d %lld",&x,&y);
LL tmp=y-a[x];
a[x]+=tmp;
c1(x,tmp);
c2(x,(x-)*tmp);
}
}
return ;
}

//p.s. 其实这道题提供了树状数组处理区间修改区间求和的一个方法

对于对一个数列进行区间修改区间求和,为了快速修改,需要进行差分,但求和就比较困难

对于区间求和,就相当于求区间差分前缀的前缀

应用上面的方法,就可以方便的解决这个问题

(完虐线段树oooooooooooooooooo)

//p.p.s.难道我讲的不清楚吗...

[bzoj3155]Preprefix sum(树状数组)的更多相关文章

  1. codeforces 703D D. Mishka and Interesting sum(树状数组)

    题目链接: D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megaby ...

  2. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  3. hdu3015,poj1990树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3015 题意:给定n组数,每组数有值x和值h,求n组数两两的val的总和.将所有x和所有h分别离散化(不 ...

  4. 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019

    题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...

  5. hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  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. Mishka and Interesting sum Codeforces Round #365 (树状数组)

    树状数组,与Turing Tree类似. xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l] 其中xr[r] ...

  9. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

随机推荐

  1. Sharepoint 2013 网站集的删除与还原

    一.可以通过三种方法删除网站集: 1.打开Sharepoint 2013 管理页面首页 ---> 单击‘应用程序管理(Application Management)’并进入该页面 ---> ...

  2. FastDFS connect timed out

    java.net.SocketTimeoutException: connect timed outUpload file "1003.png"fails:connect time ...

  3. A trip through the Graphics Pipeline 2011_06_(Triangle) rasterization and setup

    Welcome back. This time we’re actually gonna see triangles being rasterized – finally! But before we ...

  4. UIAlertController 的使用

    IAlertController 同时替代了 UIAlertView 和 UIActionSheet,从系统层级上统一了 alert 的概念 —— 即以 modal 方式或 popover 方式展示. ...

  5. cookie的作用

    Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术.Cookies是当你浏览某网站时,由Web服务器置于你硬盘上的一个非常小的文本文件,它可以 ...

  6. fbset 移植

    手头上的文件系统的fbset有问题,所以就自己从新移植一个到开发板上. 参考链接 http://blog.chinaunix.net/uid-20768928-id-5748009.html 下载地址 ...

  7. zabbix调用微信报警

    1.注册微信企业号,可以选团体号(针对小团队) 2.创建管理组 3.创建部门,记住部门id (使用了部门id,发消息会发送到所有部门成员,所以如果要单独发送给某个用户,""这样设置 ...

  8. [BS-29] 给UIView添加背景图片

    给UIView添加背景图片 //默认情况下只能设置UIView的背景颜色,不能给UIView设置背景图片,但通过绘图知识可以做到 - (void)drawRect:(CGRect)rect { [su ...

  9. codis配置

    codis集群配置 Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (不支持的命令列表) ...

  10. 多进程、协程、事件驱动及select poll epoll

    目录 -多线程使用场景 -多进程 --简单的一个多进程例子 --进程间数据的交互实现方法 ---通过Queues和Pipe可以实现进程间数据的传递,但是不能实现数据的共享 ---Queues ---P ...