poj 3468A Simple Problem with Integers(树状数组区间修改)
来源:互联网 发布:淘宝哪家多肉土好 编辑:程序博客网 时间:2024/05/18 02:54
题目链接:http://poj.org/problem?id=3468
思路:s[i]表示原来1到i的和 s1[i]表示区间修改后1到i的和 区间修改为[l,r]区间上加a
i<ls1[i]=s[i]l<=i<=rs1[i]=s[i]+a*i-a*(l-1)r<is1[i]=s[i]+a*(r-l+1)
sum[i]=c[i]*i+d[i]
则与上图表格对应的修改为
c在l处+a
c在r+1处-a
d在l处-a*(l-1)
d在r+1处+a*r
#include<cstdio>#include<cstring>using namespace std;const int num=100005;__int64 c[num],d[num];int n,m;int lowbit(int a){ return a&(-a);}void add(__int64 *a,int p,int ad){ while(p<=n) { a[p]+=ad; p+=lowbit(p); }}__int64 sum(__int64 *a,int p){ __int64 s=0; while(p>0) { s+=a[p]; p-=lowbit(p); } return s;}int main(){ int i,a,b,q; __int64 ans; char s[3]; //freopen("in.txt","r",stdin); scanf("%d%d",&n,&m); memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); for(i=1;i<=n;i++) { scanf("%d",&a); add(d,i,a); } while(m--) { scanf("%s",s); if(s[0]=='Q') { scanf("%d%d",&a,&b); ans=(b*sum(c,b)+sum(d,b))-((a-1)*sum(c,a-1)+sum(d,a-1)); //注意此处求[a,b]为sum[b]-sum[a-1] printf("%I64d\n",ans); } else { scanf("%d%d%d",&a,&b,&q); add(c,a,q); add(c,b+1,-q); add(d,a,-q*(a-1)); add(d,b+1,q*b); } } return 0;}
0 0
- poj 3468A Simple Problem with Integers(树状数组区间修改)
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- POJ 3468 A Simple Problem with Integers (树状数组) (区间修改+区间查询)
- POJ 3468 A Simple Problem with Integers(树状数组区间修改+区间查询)
- POJ 3468 A Simple Problem with Integers 树状数组(区间更新,区间查询)
- poj-3468-A Simple Problem with Integers(树状数组更新区间查区间)
- POJ 3468 A Simple Problem with Integers (树状数组解法 树状数组区间更新 区间查询)
- [POJ 2368 A Simple Problem with Integers] 树状数组区间修改、区间查询
- poj-3468 A Simple Problem with Integers(线段树,树状数组区间求和)
- POJ 3468 A Simple Problem with Integers (树状数组)
- POJ 3468 A Simple Problem with Integers(树状数组)
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- 20140719 「树状数组 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers (线段树区间更新 + 树状数组区间更新)
- poj 3468 A Simple Problem with Integers(线段树+树状数组区间更新区间查询)
- [POJ 3468]A Simple Problem with Integers[树状数组区间更新+求和]
- POJ 3468 A Simple Problem with Integers (树状数组区间更新)
- 最近这么不太平啊,出了这么大的事。
- 单反配件Miops,为什么敢这么贵
- HDU 1180:诡异的楼梯(BFS)
- ”舍得“大法:把自己的优点当缺点倒出去
- oracle 锁的机制
- poj 3468A Simple Problem with Integers(树状数组区间修改)
- Linux 平台 Oracle 单实例 从10.2.0.1 升级到10.2.0.5.4 步骤
- 使用FFMPEG类库分离出多媒体文件中的H.264码流
- Android 图片缓存、加载器
- 使用CSS修正一切:20多个常见Bug及其修正方法
- iOS自定义TableViewCell详解[两种方法]
- Android控件-GridView使用学习
- socket实现服务端与客户端的通讯
- debug正常,release有问题