线段树 + 区间更新(区间增加v)模板 ---- poj 3468 - Snarl_jsb
2016-03-05 21:26
627 查看
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 59798 Accepted: 18237
Case Time Limit: 2000MS
Description
You have N integers, A 1 , A 2 , ... , A N . 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 A 1 , A 2 , ... , A N . -1000000000 ≤ A i ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c " means adding c to each of A a , A a +1 , ... , A b . -10000 ≤ c ≤ 10000.
"Q a b " means querying the sum of A a , A a +1 , ... , A b .
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint:The sums may exceed the range of 32-bit integer
【题目大意】
一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和。(这里区间指的是数列中连续的若干个数)对每次询问给出结果。
【题目分析】
裸的区间更新线段树。
线段树单点更新和区间更新的区别:
1.每个结点中多了一个add值,代表该结点以下的结点需要增加的值;
2.build函数中,如果在建树的过程中就赋值给num,那么在建完树之后不要忘记pushup,因为此时只是叶子结点有值,上面的值都为空;这个在区间更新中很常用,因为区间更新中如果输入一个值,然后更新一个值,这样会很麻烦,会耗费更多的时间;
3.update函数中,区间更新多了一个upshdown函数,并且更新sum和add值的判断条件是树中结点的l~r和要更新的区间的l~r相等,此时sum加的值是整个区间的长度*要更新的值,然后add值记录后面每个结点需要加上的值,即:c;
4.upshdown函数最后不要忘了将延时标记add清零;
区间修改模板
Updata(把某一区间的数增加v)
Query(计算区间元素的和,最小值,最大值)
本代码为求的元素的和,求最大值的话改为//的语句
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 59798 Accepted: 18237
Case Time Limit: 2000MS
Description
You have N integers, A 1 , A 2 , ... , A N . 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 A 1 , A 2 , ... , A N . -1000000000 ≤ A i ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c " means adding c to each of A a , A a +1 , ... , A b . -10000 ≤ c ≤ 10000.
"Q a b " means querying the sum of A a , A a +1 , ... , A b .
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint:The sums may exceed the range of 32-bit integer
【题目大意】
一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和。(这里区间指的是数列中连续的若干个数)对每次询问给出结果。
【题目分析】
裸的区间更新线段树。
线段树单点更新和区间更新的区别:
1.每个结点中多了一个add值,代表该结点以下的结点需要增加的值;
2.build函数中,如果在建树的过程中就赋值给num,那么在建完树之后不要忘记pushup,因为此时只是叶子结点有值,上面的值都为空;这个在区间更新中很常用,因为区间更新中如果输入一个值,然后更新一个值,这样会很麻烦,会耗费更多的时间;
3.update函数中,区间更新多了一个upshdown函数,并且更新sum和add值的判断条件是树中结点的l~r和要更新的区间的l~r相等,此时sum加的值是整个区间的长度*要更新的值,然后add值记录后面每个结点需要加上的值,即:c;
4.upshdown函数最后不要忘了将延时标记add清零;
#include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<cmath> #define MAX 110000 #define LL long long using namespace std; LL n,m; LL ans; struct Tree { LL l,r; LL sum,add; }; Tree tree[MAX*3]; void pushup(LL x) { LL tmp=2*x; tree[x].sum=tree[tmp].sum+tree[tmp+1].sum; } void pushdown(LL x) { LL tmp=2*x; tree[tmp].add+=tree[x].add; tree[tmp+1].add+=tree[x].add; tree[tmp].sum+=tree[x].add*(tree[tmp].r-tree[tmp].l+1); tree[tmp+1].sum+=tree[x].add*(tree[tmp+1].r-tree[tmp+1].l+1); tree[x].add=0; } void build(int l,int r,int x) { tree[x].l=l; tree[x].r=r; tree[x].add=0; if(l==r) { scanf("%lld",&tree[x].sum); return ; } int tmp=x<<1; int mid=(l+r)>>1; build(l,mid,tmp); build(mid+1,r,tmp+1); pushup(x); //如果在建树的过程中给sum赋值,记得后面要pushup } void update(LL l,LL r,LL c,LL x) { if(r<tree[x].l||l>tree[x].r) return ; if(l<=tree[x].l&&r>=tree[x].r) { tree[x].add+=c; tree[x].sum+=c*(tree[x].r-tree[x].l+1); return ; } if(tree[x].add) pushdown(x); LL tmp=x<<1; update(l,r,c,tmp); update(l,r,c,tmp+1); pushup(x); } void query(LL l,LL r,LL x) { if(r<tree[x].l||l>tree[x].r) //要更新的区间不在该区间上 return ; if(l<=tree[x].l&&r>=tree[x].r) //要更新区间包括了该区间 { ans+=tree[x].sum; return ; } if(tree[x].add) pushdown(x); LL tmp=x<<1; LL mid=(tree[x].l+tree[x].r)>>1; if(r<=mid) query(l,r,tmp); else if(l>mid) query(l,r,tmp+1); else { query(l,mid,tmp); query(mid+1,r,tmp+1); } } int main() { scanf("%lld %lld",&n,&m); build(1,n,1); char str[5]; while(m--) { scanf("%s",str); if(str[0]=='Q') { LL l,r; scanf("%lld %lld",&l,&r); ans=0; query(l,r,1); printf("%lld\n",ans); } else { LL l,r,c; scanf("%lld %lld %lld",&l,&r,&c); update(l,r,c,1); } } return 0; }
区间修改模板
Updata(把某一区间的数增加v)
Query(计算区间元素的和,最小值,最大值)
本代码为求的元素的和,求最大值的话改为//的语句
#define MAX 110000 #define LL long long LL n,m; LL ans; struct Tree { LL l,r; LL sum,add;//maxx }; Tree tree[MAX*4]; void pushup(LL x) { LL tmp=2*x; tree[x].sum=tree[tmp].sum+tree[tmp+1].sum;//tree[x].maxx=max(tree[tmp].maxx,tree[tmp+1].maxx); } void pushdown(LL x) { LL tmp=2*x; tree[tmp].add+=tree[x].add; tree[tmp+1].add+=tree[x].add; tree[tmp].sum+=tree[x].add*(tree[tmp].r-tree[tmp].l+1); //tree[tmp].maxx+=tree[x].add; tree[tmp+1].sum+=tree[x].add*(tree[tmp+1].r-tree[tmp+1].l+1); //tree[tmp+1].maxx+=tree[x].add; tree[x].add=0; } void build(int l,int r,int x) { tree[x].l=l; tree[x].r=r; tree[x].add=0; if(l==r) { scanf("%lld",&tree[x].sum);//scanf("%lld",&tree[x].maxx); return ; } int tmp=x<<1; int mid=(l+r)>>1; build(l,mid,tmp); build(mid+1,r,tmp+1); pushup(x); } void update(LL l,LL r,LL c,LL x) { if(r<tree[x].l||l>tree[x].r) return ; if(l<=tree[x].l&&r>=tree[x].r) { tree[x].add+=c; tree[x].sum+=c*(tree[x].r-tree[x].l+1);//tree[x].maxx+=c; return ; } if(tree[x].add) pushdown(x); LL tmp=x<<1; update(l,r,c,tmp); update(l,r,c,tmp+1); pushup(x); } void query(LL l,LL r,LL x) { if(r<tree[x].l||l>tree[x].r) return ; if(l<=tree[x].l&&r>=tree[x].r) { ans+=tree[x].sum;//ans=max(ans,tree[x].maxx); return ; } if(tree[x].add) pushdown(x); LL tmp=x<<1; LL mid=(tree[x].l+tree[x].r)>>1; if(r<=mid) query(l,r,tmp); else if(l>mid) query(l,r,tmp+1); else { query(l,mid,tmp); query(mid+1,r,tmp+1); } } int main() { scanf("%lld %lld",&n,&m); build(1,n,1); char ch; if(ch=='Q') { LL l,r; scanf("%lld %lld",&l,&r); ans=0; query(l,r,1); printf("%lld\n",ans); } else { LL l,r,c; scanf("%lld %lld %lld",&l,&r,&c); update(l,r,c,1); }
相关文章推荐
- js对象的相关操作方法
- 【不为人知的编码】 Javascript“整型数据趋向”操作符
- JavaScript简易计算器
- 深入理解变量声明提升和函数声明提升
- 传智博客 JS(显示当前系统时间)
- jsp九大内置对象简介
- CommonJS模块规范
- JavaScript的一些记录
- JavaScript 变量的作用域
- json排序
- js中的|| 与 &&
- 【js笔记】神奇的js将a标签转换为超强的submit按钮
- JS思维之路菜鸟也能有大能量-序幕
- js闭包(三)
- js闭包(二)
- javascript学习----window对象的学习与总结
- js
- JSP九大内置对象和四种属性范围解读
- js变量、作用域和内存问题的复习**
- js 省市级联