线段树成段更新——POJ 3468
2014-08-18 14:27
288 查看
对应POJ题目:点击打开链接
A Simple Problem with IntegersCrawling in process...Crawling failedTime
Limit:5000MS Memory Limit:131072KB
64bit IO Format:%I64d & %I64u
SubmitStatus
Practice
POJ 3468
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 abc" means adding c to each of Aa,Aa+1, ... ,Ab.
-10000 ≤ c ≤ 10000.
"Q ab" 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
Sample Output
这里有相当多的线段树讲解的实例:点击打开链接
题意:输入n,m;表示有n个数和m个询问。接着输入每个数的初始值。接着每个询问表示:Q表示求解i,j区间的和,C表示i~j区间的每个值都+k
A Simple Problem with IntegersCrawling in process...Crawling failedTime
Limit:5000MS Memory Limit:131072KB
64bit IO Format:%I64d & %I64u
SubmitStatus
Practice
POJ 3468
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 abc" means adding c to each of Aa,Aa+1, ... ,Ab.
-10000 ≤ c ≤ 10000.
"Q ab" 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 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
这里有相当多的线段树讲解的实例:点击打开链接
题意:输入n,m;表示有n个数和m个询问。接着输入每个数的初始值。接着每个询问表示:Q表示求解i,j区间的和,C表示i~j区间的每个值都+k
#include<cstdio> #include<cstdlib> #include<cmath> #include<map> #include<queue> #include<stack> #include<vector> #include<algorithm> #include<cstring> #include<string> #include<iostream> const int MAXN=100000+10; using namespace std; typedef long long LL; LL sum[MAXN<<2]; LL add[MAXN<<2]; LL ans; struct node { int left,right; }tree[MAXN<<2]; void down(int pos, int len) { if(add[pos]) { add[pos<<1]+=add[pos]; add[pos<<1|1]+=add[pos]; sum[pos<<1]+=add[pos]*(len-(len>>1));//这里注意-和>>的优先级。。。 sum[pos<<1|1]+=add[pos]*(len>>1); } add[pos]=0; } void buildtree(int left, int right, int pos) { tree[pos].left=left; tree[pos].right=right; if(left==right){ scanf("%I64d", &sum[pos]); return; } int mid=(left+right)>>1; buildtree(left,mid,pos<<1); buildtree(mid+1,right,pos<<1|1); sum[pos]=sum[pos<<1]+sum[pos<<1|1]; } void update(int pos, int l, int r, int val) { if(tree[pos].left==l && tree[pos].right==r){ add[pos]+=val; sum[pos]+=val*(r-l+1); return; } down(pos,tree[pos].right-tree[pos].left+1); int mid=(tree[pos].left+tree[pos].right)>>1; if(r<=mid) update(pos<<1,l,r,val); else if(l>mid) update(pos<<1|1,l,r,val); else{ update(pos<<1,l,mid,val); update(pos<<1|1,mid+1,r,val); } sum[pos]=sum[pos<<1]+sum[pos<<1|1]; } void query(int pos, int l, int r) { if(l<=tree[pos].left && tree[pos].right<=r){ ans+=sum[pos]; return; } down(pos,tree[pos].right-tree[pos].left+1); int mid=(tree[pos].left+tree[pos].right)>>1; if(r<=mid) query(pos<<1,l,r); else if(l>mid) query(pos<<1|1,l,r); else{ query(pos<<1,l,r); query(pos<<1|1,l,r); } } int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d%d", &n,&m)==2) { memset(add,0,sizeof(add)); int i,j,k; buildtree(1,n,1); char ch[3]; while(m--) { scanf("%s", ch); if(ch[0]=='Q'){ scanf("%d%d", &i,&j); ans=0; query(1,i,j); printf("%I64d\n", ans); } else{ scanf("%d%d%d", &i,&j,&k); update(1,i,j,k); } } } return 0; }
相关文章推荐
- hdu 1698 poj 3468 线段树 成段更新(lazy标记)
- poj 3468 -- A Simple Problem with Integers ( 线段树 , 段更新 , 段求和 )
- poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)
- POJ 3468 A Simple Problem with Integers(线段树成段更新)
- poj 3468线段树 成段更新
- poj 3468 A Simple Problem with Integers(线段树成段更新,懒惰标记的使用)经典题目
- poj 3468 线段树 区间内的线段和(成段更新)
- poj_3468线段树成段更新求区间和
- POJ 3468 A Simple Problem with Integers (线段树成段更新) 解题报告
- POJ 3468 A Simple Problem with Integers?(线段树成段更新求和)
- 关于线段树懒惰标记的理解,成段更新(poj 3468为例)
- 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,线段树成段更新
- 线段树成段更新 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