POJ 3468 A Simple Problem with Integers(线段树区间求和)
来源:互联网 发布:网络摄像机编码器 编辑:程序博客网 时间:2024/05/16 13:02
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
区间求和:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=100000;int num[maxn];LL sum[maxn<<2],add[maxn<<2];int N,Q;void pushup(int rs){ sum[rs]=sum[rs<<1]+sum[rs<<1|1];}void pushdown(int rs,int l){ if(add[rs]) { add[rs<<1]+=add[rs]; add[rs<<1|1]+=add[rs]; sum[rs<<1]+=add[rs]*(l-(l>>1)); sum[rs<<1|1]+=add[rs]*(l>>1); add[rs]=0; }}void build(int rs,int l,int r){ if(l==r) { scanf("%I64d",&sum[rs]); return ; } int mid=(l+r)>>1; build(rs<<1,l,mid); build(rs<<1|1,mid+1,r); pushup(rs);}void update(int c,int x,int y,int l,int r,int rs){ if(l>=x&&r<=y) { add[rs]+=c; sum[rs]+=(LL)c*(r-l+1); return ; } pushdown(rs,r-l+1); int mid=(l+r)>>1; if(x<=mid) update(c,x,y,l,mid,rs<<1); if(y>mid) update(c,x,y,mid+1,r,rs<<1|1); pushup(rs);}LL query(int x,int y,int l,int r,int rs){ if(l>=x&&r<=y) return sum[rs]; pushdown(rs,r-l+1); int mid=(l+r)>>1; LL ans=0; if(x<=mid) ans+=query(x,y,l,mid,rs<<1); if(y>mid) ans+=query(x,y,mid+1,r,rs<<1|1); return ans;}int main(){ int x,y,z; std::ios::sync_with_stdio(false); while(~scanf("%d%d",&N,&Q)) { CLEAR(sum,0); CLEAR(add,0); build(1,1,N); char str[2]; while(Q--) { scanf("%s",str); if(str[0]=='C') { scanf("%d%d%d",&x,&y,&z); update(z,x,y,1,N,1); } else { scanf("%d%d",&x,&y); printf("%I64d\n",query(x,y,1,N,1)); } } } return 0;}
- 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(线段树,树状数组区间求和)
- A Simple Problem with Integers(线段树,区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- 二叉树树的研究——从创建到打印到从顶层逐步打印.....持续研究中
- 谷歌浏览器常用键盘快捷键整理
- 魅族缘何成为双十一最冰火手机厂商
- myEclipse 设置默认编码及优化启动速度
- 百度编辑器UEditor v1.4.3 asp版 图片上传失败的解决
- POJ 3468 A Simple Problem with Integers(线段树区间求和)
- Android_PullToRefresh使用
- javascript函数参数的arguments对象
- C++ 类的使用注意
- UVALive - 3602 DNA Consensus String
- win7下安装配置tomcat,java运行环境
- FZU 2150 Fire Game(BFS)
- OC的深Copy和浅Copy
- 用Python解析XML文件