您的位置:首页 > 其它

A Simple Problem with Integers----线段树

2016-06-02 18:55 218 查看
C - A Simple Problem with Integers
Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d
& %I64u
Submit Status 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


题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66989#problem/C

kuangbin大神的专题,这两天比赛也多,考试也多,好久都没写点东西了,今晚没有什么太大的事,正值集训队休息,敲篇博客,看看手生没生,然后准备准备今晚的codeforces,哎,好晚的说---------||||||||

这个题是裸的区间线段树更新的题,在讲区间线段树的时候我想应该先讲一下单点线段树的更新,比较好理解。
放一个裸的单点线段树更新的题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=3397
这个是我刚开始学代码有一次比赛学长们出的防AK的题,恩,楠神出的,卡了我好久。。。刚开始学你就出线段树,楠神你也是绝了。。。
我是用静态方法构建线段树,即用数组来做(毕竟下标比较好用),左子树下标为2×i,右子树为2×i+1,不要问我为什么,完全二叉树的性质,保险起见,数组一般开4×n,具体为什么仔细一想就明白了,因为是完全二叉树,所以第一个叶子节点的下标为大于等于n的第一个2^i,举例来说n==33,所以第一个叶子节点的下标为64(2^6),所以左孩子为128,右孩子是129,所以是4倍。其实实际用到的是2×n+1个,有一些空间用不到。
有几个函数在这里解释一下是什么意思PushUp(由下往上更新修改后的数据),Build(初始化线段树),Update(修改叶子结点的数据),Query(查询区间数据)



百度图片里找了张图片,很清晰,如果你能够在写代码的时候脑子里一直想着树的形状会很好写
跟着代码走一边过程,不用我讲你就会单点更新了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[6000000];
void PushUp(int st)
{
a[st]=a[st<<1]+a[st<<1|1];
}
void Build(int L,int R,int st)
{
a[st]=0;
if(L==R)
{
scanf("%d",&a[st]);
return ;
}
int mid=(L+R)>>1;
Build(L,mid,st<<1);
Build(mid+1,R,st<<1|1);
PushUp(st);
}
void Updata(int L,int R,int st,int x,int d)
{
if(L==R)
{
a[st]+=d;
return ;
}
int mid=(L+R)>>1;
if(x<=mid)
Updata(L,mid,st<<1,x,d);
else
Updata(mid+1,R,st<<1|1,x,d);
PushUp(st);
}

int Query(int L,int R,int st,int l,int r)
{
if(R<l||L>r)
return 0;
if(L>=l&&R<=r)
return a[st];
int mid=(L+R)>>1;
int ans=0;
if(l<=mid)
ans+=Query(L,mid,st<<1,l,r);
if(r>mid)
ans+=Query(mid+1,R,st<<1|1,l,r);
return ans;
}
int main()
{
int n,m;
char str[100];
int x,d,l,r;
while(~scanf("%d%d",&n,&m))
{
Build(1,n,1);
while(m--)
{
scanf("%s",str);
if(!strcmp(str,"UPDATE"))
{
scanf("%d%d",&x,&d);
Updata(1,n,1,x,d);
}
else
{
scanf("%d%d",&l,&r);
printf("%d\n",Query(1,n,1,l,r));
}
}
cout<<endl;
}
return 0;
}

好吧,我承认上面的代码有点啰嗦,不过好入门,入了门以后来看我简化版的
其实原理是一样的,看这个题http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66989#problem/A
经典线段树单点更新
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <stack>
#include <deque>
#define eps 0.00000001
using namespace std;
int a[600000];
int c,b;
void PushUp(int st)
{
while(st--)
a[st]=a[st<<1]+a[st<<1|1];
}
void Updata(int x,int y)
{
while(x)
{
a[x]+=y;
if(a[x]<0)
a[x]=0;
x>>=1;
}
}
int Query(int k,int x,int y)
{
int mid=x+((y-x)>>1),ans=0;
if(c<=x&&b>=y) return a[k];
if(c<=mid) ans+=Query(k<<1,x,mid);
if(mid<b) ans+=Query(k*2+1,mid+1,y);
return ans;
}
int main()
{
int t;
int x,y;
scanf("%d",&t);
int n;
int top=0;
while(t--)
{
memset(a,0,sizeof(a));
top++;
scanf("%d",&n);
int h=1;
while(1)
{
if(h>=n)
break;
h<<=1;
}
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[h+i]);
}
printf("Case %d:\n",top);
PushUp(h);
char s[1000];
while(1)
{
scanf("%s",s);
if(!strcmp(s,"End"))
break;
else if(!strcmp(s,"Query"))
{
scanf("%d%d",&c,&b);
cout<<Query(1,1,h)<<endl;
}
else if(!strcmp(s,"Add"))
{
scanf("%d%d",&x,&y);
Updata(h+x-1,y);
}
else if(!strcmp(s,"Sub"))
{
scanf("%d%d",&x,&y);
y=-y;
Updata(h+x-1,y);
}
}
}
return 0;
}这个就简洁好多了吧,懂了下标后好好利用就行。
这里我放一个题,就当练练手吧http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66989#problem/B
好了,讲了这么多,单点线段树算是讲完了,我们开看区间更新线段树。
原理是差不多的,有点不太一样,每个节点增加一个add变量来记录增量的累加,因为每一个数都更新到叶子节点是很不明智的选择,所以,我们找到那个区间,记录下来。
我记得网上有一张神图清楚明了,但是我找了好久没找到,当时我也是看到了那张神图恍然大悟。。。
上代码吧
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define eps 0.00000001
using namespace std;
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-l)>>1);
Build(i<<1,l,mid);
Build(i<<1|1,mid+1,r);
seg[i].nsum=seg[i<<1].nsum+seg[i<<1|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)>>1;
if(r<=mid) Add(i<<1,l,r,c);
else if(l>mid) Add(i<<1|1,l,r,c);
else
{
Add(i<<1,l,mid,c);
Add(i<<1|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)>>1;
Add(i<<1,seg[i].l,mid,seg[i].add);
Add(i<<1|1,mid+1,seg[i].r,seg[i].add);
seg[i].add=0;
if(b<=mid) return Query(i<<1,a,b);
else if(a>mid) return Query(i<<1|1,a,b);
else return Query(i<<1,a,mid)+Query(i<<1|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);
cout<<Query(1,a,b)<<endl;
}
}
}
return 0;
}

其实线段树看网上讲的再多,不如顺着代码来一便的快!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: