【POJ 3468】A Simple Problem with Integers
来源:互联网 发布:ai作图软件 编辑:程序博客网 时间:2024/06/05 05:41
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
Hint
Source
树状数组维护带修改的区间和~
首先这道题用线段树打标记很好做,但是树状数组可以更快的实现~
树状数组本身是不支持区间修改的,那么我们怎么办呢?
s[i]表示最开始的前缀和。
数组de[i],表示i-n的所有数都加上de[i]。
当把区间l-r都加上c的时候,de[l]+=c,de[r+1]-=c
我们看看怎么计算修改后的前缀和(前x个数)
原始和+修改和
=s[x]+del[1]*x+del[2]*(x-1)+...+del[x]*1
=s[x]+sigma(del[i]*(x-i+1))
=s[x]+sigma((1+x)*del[i]-i*del[i]) (1<=i<=x)
=s[x]+(1+x)*sigma(del[i])-sigma(i*del[i]) (1<=i<=x)
那么我们可以用两个树状数组:
d维护del[i]的前缀和;
d[i]维护del[i]*i的前缀和。
然后求l-r的区间和的时候,用前缀和相减就可以了~
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cstdlib>#define LL long long#define M 100000+5using namespace std;int n,T;LL d[M],di[M],s[M];int lowbit(int x){return x&(-x);}void Update1(int x,LL k){for (int i=x;i<=n;i+=lowbit(i))d[i]+=k;}void Update2(int x,LL k){for (int i=x;i<=n;i+=lowbit(i))di[i]+=k;}LL Getsum1(int x){LL ans=0;for (int i=x;i;i-=lowbit(i))ans+=d[i];return ans;}LL Getsum2(int x){LL ans=0;for (int i=x;i;i-=lowbit(i))ans+=di[i];return ans;}int main(){scanf("%d%d",&n,&T);s[0]=0;LL x;for (int i=1;i<=n;i++)scanf("%I64d",&x),s[i]=s[i-1]+x;while (T--){char ch[3];int l,r;LL c;scanf("%s",ch);scanf("%d%d",&l,&r);if (ch[0]=='C'){scanf("%I64d",&c);Update1(l,c);Update1(r+1,-c);Update2(l,c*(LL)l);Update2(r+1,(-c)*(LL)(r+1));}else{LL ans=0;ans=s[r]-s[l-1];ans+=(Getsum1(r)*(LL)(r+1)-Getsum2(r));ans-=(Getsum1(l-1)*(LL)l-Getsum2(l-1));printf("%I64d\n",ans);}}return 0;}
感悟:
1.WA是因为getsum1/2搞反。。POJ要用I64d
- 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 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 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
- Comparable与Comparator的差异
- 初识Linux下C语言编程
- Jquery easyUI dialog的close和destroy
- 已知两个1~30之间的数字,甲知道两数之和,乙知道两数之积。 。。。。。
- Eclipse Java注释模板设置详解
- 【POJ 3468】A Simple Problem with Integers
- 多进程还是多线程?
- 如何写出高质量程序
- 将已排好序的数组转换成高度平衡的二叉搜索树(BST)
- 黑马程序员——Java学习之类与多态
- 如何防止类被外界调用
- 二叉树
- 尊敬的用户:您访问的域名有误或网页不存在,您可以使用我们提供的一下服务
- 华硕笔记本,宽带连上,可以上网, 但收到不无线