POJ 3468 A Simple Problem with Integers【线段树区间更新入门题】
2017-01-10 17:31
555 查看
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
原题链接:http://poj.org/problem?id=3468
线段树区间更新入门题。区间更新用单点更新的方法去做必然会TLE,当要更新的区间正好是某个节点的区间的时候,我们就更新到此,不再继续往下更新,从而节省了时间,实现了次区间更新的高效性。
但后面更新的区间是上次更新区间的子区间的时候就要把之前保存在父节点的更新数据“下放”到对应的子区间,从而实现了操作的正确性。
对于线段树区间更新的描述网上有好多不错的博客,我就不用再去造轮子了。写法也有好多种,适合自己的才是最好的。
以下三篇代表了几种不同的写法。
此题参考博客:http://blog.csdn.net/w00w12l/article/details/7920846
http://blog.csdn.net/acdreamers/article/details/8578161
http://blog.csdn.net/acceptedxukai/article/details/6933446
#include <cstdio>
using namespace std;
const int maxn=100000+5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long LL;
LL sum[maxn<<2],add[maxn];
int a[maxn];
struct node
{
int l,r,m;
LL sum,mark;
}tree[maxn<<2];
void BuildTree(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].m=(l+r)>>1;
tree[k].mark=0;
if(l==r)
{
tree[k].sum=a[l];
return;
}
int mid=(l+r)>>1;
BuildTree(l,mid,k<<1);
BuildTree(mid+1,r,k<<1|1);
tree[k].sum=(tree[k<<1].sum+tree[k<<1|1].sum);
}
void UpdateTree(int l,int r,int k,int x)
{
if(tree[k].l==l&&tree[k].r==r)
{
tree[k].mark+=x;
return;
}
tree[k].sum+=(LL)x*(r-l+1);
//int mid=(tree[k].l+tree[k].r)>>1;
int mid=tree[k].m;
if(r<=mid)
UpdateTree(l,r,k<<1,x);
else if(mid<l)
UpdateTree(l,r,k<<1|1,x);
else
{
UpdateTree(l,mid,k<<1,x);
UpdateTree(mid+1,r,k<<1|1,x);
}
}
LL QueryTree(int l,int r,int k)
{
//cout<<l<<","<<r<<","<<k<<endl;
if(tree[k].l==l&&tree[k].r==r)
return tree[k].sum+tree[k].mark*(LL)(r-l+1);
if(tree[k].mark!=0)
{
tree[k<<1].mark+=tree[k].mark;
tree[k<<1|1].mark+=tree[k].mark;
tree[k].sum+=(LL)(tree[k].r-tree[k].l+1)*tree[k].mark;//记得+1
tree[k].mark=0;
}
//int mid=(tree[k].l+tree[k].r)>>1;
int mid=tree[k].m;
if(mid>=r)
return QueryTree(l,r,k<<1);
else if(l>mid)
return QueryTree(l,r,k<<1|1);
else
return QueryTree(l,mid,k<<1)+QueryTree(mid+1,r,k<<1|1);
}
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++)
cin>>a[i];
BuildTree(1,n,1);
char ch[2];
int x,y,z;
while(m--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",QueryTree(x,y,1));
}
else if(ch[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
UpdateTree(x,y,1,z);
}
}
}
return 0;
}
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 102132 | Accepted: 31872 | |
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
原题链接:http://poj.org/problem?id=3468
线段树区间更新入门题。区间更新用单点更新的方法去做必然会TLE,当要更新的区间正好是某个节点的区间的时候,我们就更新到此,不再继续往下更新,从而节省了时间,实现了次区间更新的高效性。
但后面更新的区间是上次更新区间的子区间的时候就要把之前保存在父节点的更新数据“下放”到对应的子区间,从而实现了操作的正确性。
对于线段树区间更新的描述网上有好多不错的博客,我就不用再去造轮子了。写法也有好多种,适合自己的才是最好的。
以下三篇代表了几种不同的写法。
此题参考博客:http://blog.csdn.net/w00w12l/article/details/7920846
http://blog.csdn.net/acdreamers/article/details/8578161
http://blog.csdn.net/acceptedxukai/article/details/6933446
AC代码:
#include <iostream>#include <cstdio>
using namespace std;
const int maxn=100000+5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long LL;
LL sum[maxn<<2],add[maxn];
int a[maxn];
struct node
{
int l,r,m;
LL sum,mark;
}tree[maxn<<2];
void BuildTree(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].m=(l+r)>>1;
tree[k].mark=0;
if(l==r)
{
tree[k].sum=a[l];
return;
}
int mid=(l+r)>>1;
BuildTree(l,mid,k<<1);
BuildTree(mid+1,r,k<<1|1);
tree[k].sum=(tree[k<<1].sum+tree[k<<1|1].sum);
}
void UpdateTree(int l,int r,int k,int x)
{
if(tree[k].l==l&&tree[k].r==r)
{
tree[k].mark+=x;
return;
}
tree[k].sum+=(LL)x*(r-l+1);
//int mid=(tree[k].l+tree[k].r)>>1;
int mid=tree[k].m;
if(r<=mid)
UpdateTree(l,r,k<<1,x);
else if(mid<l)
UpdateTree(l,r,k<<1|1,x);
else
{
UpdateTree(l,mid,k<<1,x);
UpdateTree(mid+1,r,k<<1|1,x);
}
}
LL QueryTree(int l,int r,int k)
{
//cout<<l<<","<<r<<","<<k<<endl;
if(tree[k].l==l&&tree[k].r==r)
return tree[k].sum+tree[k].mark*(LL)(r-l+1);
if(tree[k].mark!=0)
{
tree[k<<1].mark+=tree[k].mark;
tree[k<<1|1].mark+=tree[k].mark;
tree[k].sum+=(LL)(tree[k].r-tree[k].l+1)*tree[k].mark;//记得+1
tree[k].mark=0;
}
//int mid=(tree[k].l+tree[k].r)>>1;
int mid=tree[k].m;
if(mid>=r)
return QueryTree(l,r,k<<1);
else if(l>mid)
return QueryTree(l,r,k<<1|1);
else
return QueryTree(l,mid,k<<1)+QueryTree(mid+1,r,k<<1|1);
}
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++)
cin>>a[i];
BuildTree(1,n,1);
char ch[2];
int x,y,z;
while(m--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",QueryTree(x,y,1));
}
else if(ch[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
UpdateTree(x,y,1,z);
}
}
}
return 0;
}
相关文章推荐
- poj 3468 A Simple Problem with Integers (线段树区间更新入门)
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
- 线段树 poj 3468 A Simple Problem with Integers 区间add更新
- 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(线段树,区间更新,区间求和)
- (Relax 线段树1.1)POJ 3468 A Simple Problem with Integers(线段树子区间更新的维护:集中更新和动态统计子序列中的数据)
- POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树
- 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(线段树区间更新)