POJ 3468 A Simple Problem with Integers
来源:互联网 发布:java打印所有ASCII码 编辑:程序博客网 时间:2024/06/07 05:33
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
线段树区间更新
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#define N 100010using namespace std;struct num{ int l,r; __int64 val,add,sum;}a[4*N];__int64 b[N],res;bool status[10*N];char s1[5];int main(){ //freopen("data.in","r",stdin); void pre_build(int k,int l,int r); void find(int k,int l,int r); void update(int k,int l,int r,__int64 val); int n,m; while(scanf("%d %d",&n,&m)!=EOF) { memset(status,false,sizeof(status)); for(int i=1;i<=n;i++) { scanf("%I64d",&b[i]); } pre_build(1,1,n); while(m--) { scanf("%s",s1); int x,y; __int64 val; if(s1[0]=='Q') { scanf("%d %d",&x,&y); res=0; find(1,x,y); printf("%I64d\n",res); }else { scanf("%d %d %I64d",&x,&y,&val); update(1,x,y,val); } } } return 0;}void pushup(int k){ a[k].sum=a[k<<1].sum+a[k<<1|1].sum;}void pre_build(int k,int l,int r){ a[k].l = l; a[k].r = r; a[k].val=0; a[k].add=0; status[k]=true; if(l==r) { a[k].sum=b[l]; return ; } int mid=(l+r)>>1; pre_build(k<<1,l,mid); pre_build(k<<1|1,mid+1,r); pushup(k);}void update(int k,int l,int r,__int64 val){ a[k].sum+=(__int64)(r-l+1)*val; if(a[k].l==l&&a[k].r==r) { a[k].val+=val; return ; } int mid=(a[k].l+a[k].r)>>1; if(r<=mid) { update(k<<1,l,r,val); }else if(l>mid) { update(k<<1|1,l,r,val); }else { update(k<<1,l,mid,val); update(k<<1|1,mid+1,r,val); }}void find(int k,int l,int r){ a[k].sum+=(a[k].add*(__int64)(a[k].r-a[k].l+1)); if(status[k<<1]) { a[k<<1].add+=(a[k].add+a[k].val); } if(status[k<<1|1]) { a[k<<1|1].add+=(a[k].add+a[k].val); } a[k].add=0; a[k].val=0; if(a[k].l==l&&a[k].r==r) { res+=a[k].sum; return ; } int mid=(a[k].l+a[k].r)>>1; if(r<=mid) { find(k<<1,l,r); }else if(l>mid) { find(k<<1|1,l,r); }else { find(k<<1,l,mid); find(k<<1|1,mid+1,r); }}
- 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
- C#隐藏启动窗口方法总结
- 最长等差数列分析
- CF 342A(Xenia and Divisors-考虑有限情况)
- 【记录】system v ipc(3) 信号量
- qsort函数
- POJ 3468 A Simple Problem with Integers
- Android之场景桌面(二)----模拟时钟实现
- 数组A中,除了某一个数字x之外,其他数字都出现了三次,而x出现了一次。请给出最快的方法,找到x。
- MFC为何使用消息映射表而不用虚函数?
- 机房收费系统Number One
- C#窗口切换问题的解决
- CF 342B(Xenia and Spies-模拟)
- HP450 MAC
- 百万级PHP网站架构工具箱