【bzoj3155】【Preprefix sum】【树状数组】
来源:互联网 发布:初学者吉他知乎 编辑:程序博客网 时间:2024/06/07 01:30
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
1 2 3 4 5
Query 5
Modify 3 2
Query 5
Sample Output
35
32
32
HINT
1<=N,M<=100000,且在任意时刻0<=Ai<=100000
题解:树状数组的区间修改。。。或许直接用线段树吧。。
代码:
#include<iostream>#include<cstdio>#define N 100010using namespace std;long long c[N],c2[N],a[N];int n,q,v,x;char ch[10];long long read(){ long long x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}void updata(long long c[],int x,long long v){for (int i=x;i<=n;i+=(i&(-i))) c[i]+=v;}long long query(long long c[],int x){ long long ans(0); for (int i=x;i;i-=(i&(-i))) ans+=c[i]; return ans;}int main(){ n=read();q=read(); for (int i=1;i<=n;i++){ a[i]=read();updata(c,i,a[i]);updata(c2,i,(n-i+1)*a[i]); } for (int i=1;i<=q;i++){ scanf("%s",&ch); if (ch[0]=='Q'){x=read();printf("%lld\n",query(c2,x)-(n-x)*query(c,x));} if (ch[0]=='M'){ x=read();v=read();updata(c,x,v-a[x]);updata(c2,x,(n-x+1)*(v-a[x]));a[x]=v; } }}
0 0
- 【bzoj3155】【Preprefix sum】【树状数组】
- 【BZOJ3155】Preprefix sum【树状数组】
- bzoj3155: Preprefix sum 树状数组
- bzoj3155: Preprefix sum
- 【BZOJ3155】Preprefix sum
- 【bzoj3155】Preprefix Sum
- BZOJ3155 Preprefix sum
- bzoj3155 Preprefix sum(简单线段树)
- 【BZOJ】【P3155】【Preprefix sum】【题解】【树状数组】
- bzoj 3155 Preprefix sum 树状数组
- BZOJ 3155: Preprefix sum 树状数组
- [bzoj 3155] Preprefix sum(树状数组)
- BZOJ 3155: Preprefix sum 树状数组题解
- bzoj 3155: Preprefix sum 树状数组
- 【BZOJ】3155 Preprefix Sum
- BZOJ Preprefix sum
- bzoj 3155 Preprefix sum
- BZOJ 3155 Preprefix sum
- 二叉树已知先序序列(后序序列)、中序序列求解后序序列(先序序列)
- 【Python】求数组(list)中最大的X个数,不递归,尽可能减少代码量
- win7系统怎么用笔记本做wifi热点
- c++11线程管理,RAII方式等待异常环境下线程结束
- matlab:创建cell结构体
- 【bzoj3155】【Preprefix sum】【树状数组】
- 话说正确率、召回率和F值
- [笔记] Convex Optimization 2015.11.18
- Spring MVC框架整合
- Windows Server 2008 R2常规设置及基本安全策略
- Maven 3 + Hibernate 4 + Spring 3 + Ehcache + Spring Cache
- Java内存管理的Tips
- c++任意数字转换为字符串
- PLSQL Developer 完美支持Oracle64位问题