POJ 3468-A Simple Problem with Integers(区间更新线段树)
来源:互联网 发布:南昌大学网络教学凭条 编辑:程序博客网 时间:2024/06/06 12:22
A Simple Problem with Integers
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
From:http://www.cnblogs.com/littlex/archive/2012/01/30/2331670.html
这题很好的体现了lazy_tag的思想,当要增加的区间覆盖当前区间,则直接打上标记返回,当下次查询这个区间儿子区间的时候,直接标记往下传。可以想象,这个标记表示的是这个区间表示的整个区间的标记是整个子树的性质,当你不需要用到这个区间的儿子区间的时候,你可以不把操作都做到底,而是要用到的时候再往下传!
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define MAX 500000struct node{ int l,r; long long val;//增量 long long num;//总和} tree[4*MAX];long long num[MAX];void build(int l,int r,int index)//建树{ tree[index].l=l; tree[index].r=r; if(tree[index].l==tree[index].r) { tree[index].num=num[l-1]; return ; } int mid=(l+r)/2; build(l,mid,2*index); build(mid+1,r,2*index+1); tree[index].num=tree[index*2].num+tree[index*2+1].num;//每个节点和}void update(int l,int r,int c,int index)//更新{ if(tree[index].l>=l&&tree[index].r<=r) { tree[index].num+=(tree[index].r-tree[index].l+1)*c; tree[index].val+=c; return ; } else { if(tree[index].val)//最关键部分就是这个标记下传,将增加值val传给他的左右孩子 { tree[index*2].num+=tree[index].val*(tree[index*2].r-tree[index*2].l+1); tree[index*2].val+=tree[index].val; tree[index*2+1].num+=tree[index].val*(tree[index*2+1].r-tree[index*2+1].l+1); tree[index*2+1].val+=tree[index].val; tree[index].val=0; } if(r>=tree[index*2+1].l) update(l,r,c,2*index+1); if(l<=tree[index*2].r) update(l,r,c,2*index); tree[index].num=tree[index*2].num+tree[index*2+1].num;//每个节点和 }}long long query(int a,int b,int index)//查询{ if(tree[index].l>=a&&tree[index].r<=b) return tree[index].num; if(tree[index].val) //求和同样需要标记下传 { tree[index*2].num+=tree[index].val*(tree[index*2].r-tree[index*2].l+1); tree[index*2].val+=tree[index].val; tree[index*2+1].num+=tree[index].val*(tree[index*2+1].r-tree[index*2+1].l+1); tree[index*2+1].val+=tree[index].val; tree[index].val=0; } int mid=(tree[index].l+tree[index].r)/2; long long max1=0,max2=0; if(mid<a) max1= query(a,b,2*index+1); else if(b<=mid) max1=query(a,b,2*index); else { max1=query(a,mid,2*index); max2=query(mid+1,b,2*index+1); } return max1+max2;}int main(){ int n,i,m; while(scanf("%d%d",&n,&m)!=EOF) { memset(tree,0,sizeof(tree)); for(i=0; i<n; i++) scanf("%I64d",&num[i]); build(1,n,1); for(i=0; i<m; i++) { char st; cin>>st; if(st=='C') { int a,b,c; scanf("%d%d%d",&a,&b,&c); update(a,b,c,1); } else { int a,b; long long sum=0; scanf("%d%d",&a,&b); sum=query(a,b,1); printf("%I64d\n",sum); } } } return 0;}
- 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(线段树(区间更新))
- POJ 3468 A Simple Problem with Integers(线段树区间更新+求和)
- ubuntu14.04中改变文件夹的图标
- jQuery EasyUI使用教程之创建简单窗口
- [转] CocoaPods详解之----使用篇
- 测试验证datatime字段是否走索引
- sqlserver 用 STUFF 和 for xml path 把查询结果连接成字符串
- POJ 3468-A Simple Problem with Integers(区间更新线段树)
- <!DOCTYPE html>重要性
- BootStrap响应式导航条
- JAVA 中Exception和Error 面试问题
- synchronized与static synchronized 的区别
- Dr.Elephant用户指南 - 【Dr.Elephant系列文章-2】
- Java 对象流ObjectInputStream和ObjectOutputStream类
- [转] CocoaPods详解之----进阶篇
- css3的盒模型