线段树 poj 3468 A Simple Problem with Integers 区间add更新
来源:互联网 发布:mobi域名 编辑:程序博客网 时间:2024/06/05 18:34
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<cmath>#include<cstring>#include<algorithm>#define N 222222using namespace std;int num[N];struct Tree{ int l; int r; long long sum; long long col;} tree[N*4];void push_up(int rt){ tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;}void push_down(int rt,int m){ if (tree[rt].col!=0) { tree[rt<<1].col+=tree[rt].col; tree[rt<<1|1].col+=tree[rt].col; tree[rt<<1].sum+=(long long)(m-(m/2))*tree[rt].col; tree[rt<<1|1].sum+=(long long)(m/2)*tree[rt].col; tree[rt].col=0; }}void build(int root,int l,int r){ tree[root].l=l; tree[root].r=r; tree[root].col=0; if(tree[root].l==tree[root].r) { tree[root].sum=num[l]; return; } int mid=(l+r)/2; build(root<<1,l,mid); build(root<<1|1,mid+1,r); push_up(root);}void update(int root,int L,int R,int val){ if(L<=tree[root].l&&R>=tree[root].r) { tree[root].col+=val; tree[root].sum+=(long long)val*(tree[root].r-tree[root].l+1); return; } push_down(root,tree[root].r-tree[root].l+1); int mid=(tree[root].l+tree[root].r)/2; if (L<=mid) update(root<<1,L,R,val); if (R>mid) update(root<<1|1,L,R,val); push_up(root);}long long query(int root,int L,int R){ if(L<=tree[root].l&&R>=tree[root].r) return tree[root].sum; push_down(root,tree[root].r-tree[root].l+1); int mid=(tree[root].l+ tree[root].r)/2; long long ret=0; if(L<=mid) ret+=query(root<<1,L,R); if(R>mid) ret+=query(root<<1|1,L,R); return ret;}int main(){ int n,q; char s[2]; scanf("%d%d",&n,&q); for (int i=1; i<=n; i++) scanf("%d",&num[i]); build(1,1,n); while (q--) { int x,y,z; scanf("%s",s); if (s[0]=='Q') { scanf("%d%d",&x,&y); printf("%I64d\n",query(1,x,y)); } else if (s[0]=='C') { scanf("%d%d%d",&x,&y,&z); update(1,x,y,z); } } return 0;}
- 线段树 poj 3468 A Simple Problem with Integers 区间add更新
- A Simple Problem with Integers +poj+线段树区间更新
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- poj 3468 A Simple Problem with Integers (线段树区间更新 + 树状数组区间更新)
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- 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(线段树 区间更新 区间查询)
- 线段树 (更新区间查询区间)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#入门到精通》学习笔记 -- 异常类
- highcharts 导出图片 .net c#
- 明晰C++内存分配的五种方法的区别
- 碰巧而已
- JS汉字转拼音,超级实用
- 线段树 poj 3468 A Simple Problem with Integers 区间add更新
- arcgis engine 图形操作函数(by 懒羊羊)
- js中==与===的区别
- SVN代码版本管理
- 编译Nexus 7 源码的流程
- Android4.1系统设置时间格式默认为24小时
- 帝国CMS 6.0功能解密之字段处理函数
- HDOJ 1248 寒冰王座 (多重背包)
- QTimeLine