POJ 3468 线段树入门题

来源:互联网 发布:apache cxf 下载 编辑:程序博客网 时间:2024/06/01 08:24




#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define N 110000#define LL long long #define lson L,mid,rt<<1#define rson mid+1,R,rt<<1|1LL sum[N<<2],add[N<<2];struct point{int lc,rc;}tree[N<<2];void Pushup(int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];//向上更新父亲节点}void Pushdown(int rt,int m){if(add[rt])//如果add还有值没有更新,则向下更新{add[rt<<1]+=add[rt];add[rt<<1|1]+=add[rt];sum[rt<<1]+=add[rt]*(m-(m>>1));//将区间分成2部分,这里字节点区间长度分别为m-(m>>1);和m>>1;sum[rt<<1|1]+=add[rt]*(m>>1);add[rt]=0;//将add[rt]的0制空}}void build(int L,int R,int rt){tree[rt].lc=L;tree[rt].rc=R;add[rt]=0;if(L==R){scanf("%lld",&sum[rt]);return ;}int mid=(tree[rt].lc+tree[rt].rc)>>1;build( L,mid,rt<<1);build( mid+1,R,rt<<1|1);//构造左右子树Pushup(rt);//更新完子节点后再向上更新父节点}void update(LL c,int L,int R,int rt){if(tree[rt].lc==L&&tree[rt].rc==R)//如果更新区间恰好是当前区间则更新{add[rt]+=c;sum[rt]+=long long ((R-L+1)*c);return ;}if(tree[rt].lc==tree[rt].rc){return;}Pushdown(rt,tree[rt].rc-tree[rt].lc+1);//向下更新(如果add[rt]有值可以更新)int mid=(tree[rt].lc+tree[rt].rc)>>1;if(R<=mid)update(c,L,R,rt<<1);//右端点比mid小,则直接更新左子树else if(L>mid)update (c,L,R,rt<<1|1);//左端点比mid大,则直接<span style="font-family: Arial, Helvetica, sans-serif;">更新右子树</span>else //左右分开在2个不同字数,所以分步更新{update(c,L,mid,rt<<1);update(c,mid+1,R,rt<<1|1);}Pushup(rt);}LL query(int L,int R,int rt){if(L==tree[rt].lc&&R==tree[rt].rc)//查找到更新区间直接返回{return sum[rt];}Pushdown(rt, tree[rt].rc-tree[rt].lc+1);//否则先向下更新LL res=0;//以下内容同上update函数的更新思想int mid=(tree[rt].lc+tree[rt].rc)>>1;if(R<=mid)res+=query(L,R,rt<<1);else if(L>mid)res+=query(L,R,rt<<1|1);else{res+=query(L,mid,rt<<1);res+=query(mid+1,R,rt<<1|1);}return res;}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifchar s[10];int n,m,a,b;LL c;while(~scanf("%d%d",&n,&m)){build(1,n,1);while(m--){scanf("%s",s);if(s[0]=='C'){scanf("%d%d%lld",&a,&b,&c);update(c,a,b,1);}else if(s[0]=='Q'){scanf("%d%d",&a,&b);printf("%lld\n",query(a,b,1));}}}return 0;}

对着模板敲得,正在理解中。经典的模板题,经典的模板,题解见注释。


A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 81413 Accepted: 25148Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi



0 0
原创粉丝点击