bzoj 3155: Preprefix sum 树状数组
来源:互联网 发布:淘宝店没生意怎么推广 编辑:程序博客网 时间:2024/06/15 22:54
题意
1<=N,M<=100000,且在任意时刻0<=Ai<=100000
分析
唉果然像我这种弱鸡就只能切切这种大家都会的sb题了。
把答案的式子展开后用树状数组维护一下对应项就好了。
代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int N=100005;int n,m,a[N];LL c1[N],c2[N];int read(){ int 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 ins1(int x,LL y){ while (x<=n) c1[x]+=y,x+=x&(-x);}void ins2(int x,LL y){ while (x<=n) c2[x]+=y,x+=x&(-x);}LL query1(int x){ LL ans=0; while (x) ans+=c1[x],x-=x&(-x); return ans;}LL query2(int x){ LL ans=0; while (x) ans+=c2[x],x-=x&(-x); return ans;}int main(){ n=read();m=read(); for (int i=1;i<=n;i++) a[i]=read(),ins1(i,a[i]),ins2(i,(LL)a[i]*i); while (m--) { char ch[10]; scanf("%s",ch); if (ch[0]=='M') { int x=read(),y=read(); ins1(x,-a[x]);ins2(x,(LL)-a[x]*x); a[x]=y; ins1(x,a[x]);ins2(x,(LL)a[x]*x); } else { int x=read(); LL s1=query1(x),s2=query2(x); printf("%lld\n",(LL)s1*(x+1)-s2); } } return 0;}
阅读全文
0 0
- bzoj 3155 Preprefix sum 树状数组
- BZOJ 3155: Preprefix sum 树状数组
- [bzoj 3155] Preprefix sum(树状数组)
- BZOJ 3155: Preprefix sum 树状数组题解
- bzoj 3155: Preprefix sum 树状数组
- 【BZOJ】【P3155】【Preprefix sum】【题解】【树状数组】
- 【BZOJ】3155 Preprefix Sum
- bzoj 3155 Preprefix sum
- BZOJ 3155 Preprefix sum
- 【bzoj3155】【Preprefix sum】【树状数组】
- 【BZOJ3155】Preprefix sum【树状数组】
- bzoj3155: Preprefix sum 树状数组
- [bzoj-3155]Preprefix sum 题解
- BZOJ Preprefix sum
- 3155: Preprefix sum
- 3155: Preprefix sum
- bzoj 3155 树状数组
- bzoj3155: Preprefix sum
- 通过 PackageManager 获得你想要的 App 信息
- 20171019
- Pro Spark Streaming笔记
- centos 安装mysql,更改初始密码
- CSS学习
- bzoj 3155: Preprefix sum 树状数组
- list集合中常用方法,和一些相关类的使用
- codeforces 652 E Pursuit For Artifacts
- CSP 201312-5 I’m stuck! 两次BFS
- c#线性空间求最长公共子序列
- 网络判断+Fragment+ViewPager+XListView+TabLayout横向滑动菜单 Z2
- Linux下查看进程及删除进程
- smarty模板引擎中的判断句式、循环、文件引用和对象的引用
- 高斯混合模型(GMM)