poj3468 A Simple Problem with Integers
来源:互联网 发布:编写软件怎么自学 编辑:程序博客网 时间:2024/06/06 12:48
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 <stdio.h>#define MAX 800000using namespace std;struct Tree{ long long sum; long long flag;};Tree tree[MAX];void PushUp(int rt){ tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;}void PushDown(int rt,int len){ if(tree[rt].flag!=0) { tree[rt<<1].flag+=tree[rt].flag; tree[rt<<1|1].flag+=tree[rt].flag; tree[rt<<1].sum+=(len-(len>>1))*tree[rt].flag; tree[rt<<1|1].sum+=(len>>1)*tree[rt].flag; tree[rt].flag=0; }}void Build(int l,int r,int rt){ tree[rt].flag=0; if(l==r) { cin>>tree[rt].sum; return; } int m=(l+r)>>1; Build(l,m,rt<<1); Build(m+1,r,rt<<1|1); PushUp(rt);}void Update(int L,int R,int d,int l,int r,int rt){ if(L<=l&&R>=r) { tree[rt].flag+=d; tree[rt].sum+=(r-l+1)*d; return; } PushDown(rt,r-l+1); int m=(l+r)>>1; if(L<=m) Update(L,R,d,l,m,rt<<1); if(R>m) Update(L,R,d,m+1,r,rt<<1|1); PushUp(rt);}long long Query(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) { return tree[rt].sum; } PushDown(rt,r-l+1); int m=(l+r)>>1; long long sum=0; if(L<=m) sum+=Query(L,R,l,m,rt<<1); if(R>m) sum+=Query(L,R,m+1,r,rt<<1|1); return sum;}int main(){ int n,q; int a,b,c; int i; char op; scanf("%d%d",&n,&q); { Build(1,n,1); while(q--) { cin>>op; if(op=='Q') { scanf("%d%d",&a,&b); cout<<Query(a,b,1,n,1)<<endl; } else if(op=='C') { scanf("%d%d%d",&a,&b,&c); Update(a,b,c,1,n,1); } } } return 0;}
- poj3468 A Simple Problem with Integers
- POJ3468:A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468-A Simple Problem with Integers
- POJ3468--A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- [POJ3468]A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- Beginners Level Course:File Permissions - chmod
- hiho 博弈——01(hiho 44周)
- Beginners Level Course:Day to Day with Linux
- 装饰者模式
- Beginners Level Course:File Permisions - chown
- poj3468 A Simple Problem with Integers
- 通过PHP将文件以流的方式发送客户端
- PHP基础知识总结系列文章归档
- find命令用法介绍
- OO设计思想
- 《PHP基础知识总结》关于PHP5 Session生命周期介绍
- Spring学习笔记(12)----------aop
- angularjs karma安装小问题
- 04_java基础_继承、抽象、多态