A Simple Problem with Integers POJ - 3468(线段树区间更新,查找)
2017-09-25 20:11
429 查看
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 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 integers.
题意:对这样一组数进行下列操作: Q操作表示读取该区间上的和,C操作表示为i到j区间的数全部增加c。
#include<stdio.h> #include<string.h> #include<stdalign.h> int num[400000]; struct node { int l,r; long long nsum; long long add; }seg[400000]; void Build(int i,int l,int r)//建立线段树 { seg[i].l=l; seg[i].r=r; seg[i].add=0; if(l==r) { seg[i].nsum = num[l]; return ; } int mid = (l+r)/2; Build(i*2,l,mid); Build(i*2+1,mid+1,r); seg[i].nsum = seg[i*2].nsum+seg[i*2+1].nsum; } void Add(int i,int l,int r,long long c)//区间上的每个数增加 { if(seg[i].l==l&&seg[i].r==r) { seg[i].add+=c; return ; } seg[i].nsum += c*(r-l+1);//这个区间总共增加了这些 int mid = (seg[i].l+seg[i].r)/2; if(r<=mid) Add(i*2,l,r,c); else if(l>mid) Add(i*2+1,l,r,c); else { Add(i*2,l,mid,c); Add(i*2+1,mid+1,r,c); } } long long Query(int i,int a,int b)//查询a-b的总和 { if(seg[i].l==a&&seg[i].r==b) { return seg[i].nsum+(b-a+1)*seg[i].add; } seg[i].nsum+=(seg[i].r-seg[i].l+1)*seg[i].add; int mid=(seg[i].l+seg[i].r)/2; Add(i*2,seg[i].l,mid,seg[i].add); Add(i*2+1,mid+1,seg[i].r,seg[i].add); seg[i].add=0; if(b<=mid) return Query(i*2,a,b); else if(a>mid) return Query(i*2+1,a,b); else return Query(i*2,a,mid)+Query(i*2+1,mid+1,b); } int main() { int n,m,i; int a,b,c; char s[10]; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) { scanf("%d",&num[i]); } Build(1,1,n); for(i=0;i<m;i++) { scanf("%s",s); if(s[0]=='C') { scanf("%d%d%d",&a,&b,&c); Add(1,a,b,c); } else { scanf("%d%d",&a,&b); printf("%lld\n",Query(1,a,b)); } } } return 0; }
相关文章推荐
- 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(线段树——区间更新)
- 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 线段树区间更新 纯模板题
- A Simple Problem with Integers POJ - 3468--------线段树的区间更新解析及例题