ACM POJ 3468 A Simple Problem with Integers(线段树) by kuangbin
2011-08-14 20:36
423 查看
题目链接:http://poj.org/problem?id=3468
本文作者:kuangbin
博客地址:http://www.cnblogs.com/kuangbin/
题目:
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
Sample Output
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
[b][b]简单的线段树练习题。[/b][/b]
[b][b][b]主要是树的节点存储哪些信息。[/b][/b][/b]
[b][b][b][b]每个一个数都更新到叶子节点不是明智的做法,很消耗时间。[/b][/b][/b][/b]
[b][b][b][b][b]故每个节点加一个Inc来记录增量的累加。[/b][/b][/b][/b][/b]
[b][b][b][b][b][b]具体看代码:[/b][/b][/b][/b][/b][/b]
本文作者:kuangbin
博客地址:http://www.cnblogs.com/kuangbin/
题目:
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 22796 | Accepted: 6106 | |
Case Time Limit: 2000MS |
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.
Source
POJ Monthly--2007.11.25, Yang Yi
[b][b]简单的线段树练习题。[/b][/b]
[b][b][b]主要是树的节点存储哪些信息。[/b][/b][/b]
[b][b][b][b]每个一个数都更新到叶子节点不是明智的做法,很消耗时间。[/b][/b][/b][/b]
[b][b][b][b][b]故每个节点加一个Inc来记录增量的累加。[/b][/b][/b][/b][/b]
[b][b][b][b][b][b]具体看代码:[/b][/b][/b][/b][/b][/b]
/* POJ 3468 A Simple Problem with Integers 题目意思: 给定Q个数:A1,A2,```,AQ,以及可能多次进行下列两个操作: 1)对某个区间Ai```Aj的数都加n(n可变) 2)对某个区间Ai```Aj求和 */ #include<stdio.h> #include<algorithm> #include<iostream> usingnamespace std; constint MAXN=100000; int num[MAXN]; struct Node { int l,r;//区间的左右端点 longlong nSum;//区间上的和 longlong Inc;//区间增量的累加 }segTree[MAXN*3]; void Build(int i,int l,int r) { segTree[i].l=l; segTree[i].r=r; segTree[i].Inc=0; if(l==r) { segTree[i].nSum=num[l]; return; } int mid=(l+r)>>1; Build(i<<1,l,mid); Build(i<<1|1,mid+1,r); segTree[i].nSum=segTree[i<<1].nSum+segTree[i<<1|1].nSum; } void Add(int i,int a,int b,longlong c)//在结点i的区间(a,b)上增加c { if(segTree[i].l==a&&segTree[i].r==b) { segTree[i].Inc+=c; return; } segTree[i].nSum+=c*(b-a+1); int mid=(segTree[i].l+segTree[i].r)>>1; if(b<=mid) Add(i<<1,a,b,c); elseif(a>mid) Add(i<<1|1,a,b,c); else { Add(i<<1,a,mid,c); Add(i<<1|1,mid+1,b,c); } } longlong Query(int i,int a,int b)//查询a-b的总和 { if(segTree[i].l==a&&segTree[i].r==b) { return segTree[i].nSum+(b-a+1)*segTree[i].Inc; } segTree[i].nSum+=(segTree[i].r-segTree[i].l+1)*segTree[i].Inc; int mid=(segTree[i].l+segTree[i].r)>>1; Add(i<<1,segTree[i].l,mid,segTree[i].Inc); Add(i<<1|1,mid+1,segTree[i].r,segTree[i].Inc); segTree[i].Inc=0; if(b<=mid) return Query(i<<1,a,b); elseif(a>mid) return Query(i<<1|1,a,b); elsereturn Query(i<<1,a,mid)+Query(i<<1|1,mid+1,b); } int main() { int n,q; int i; int a,b,c; char ch; while(scanf("%d%d",&n,&q)!=EOF) { for(i=1;i<=n;i++) scanf("%d",&num[i]); Build(1,1,n); for(i=1;i<=q;i++) { cin>>ch; if(ch=='C') { scanf("%d%d%d",&a,&b,&c); Add(1,a,b,c); } else { scanf("%d%d",&a,&b); printf("%I64d\n",Query(1,a,b)); } } } return0; }
相关文章推荐
- 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【线段树(结构体)】
- poj3468 A Simple Problem with Integers(线段树+区间更新+非完全替换)模板
- poj 3468 A Simple Problem with Integers 线段树
- POJ 3468 - A Simple Problem with Integers(线段树,区间更新)
- [线段树] poj3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers 线段树
- poj3468 A Simple Problem with Integers(spaly&&线段树)
- POJ3468 A Simple Problem with Integers(线段树区间修改--动态实现)
- HDOJ 4267 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 (线段树 区间更新 区间求和)