Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 72424 | Accepted: 22343 | |
Case Time Limit: 2000MS |
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
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
这道题的大致题目意思是:
首先给你两个整数N,Q;N代表的是你有几个数,然后Q代表的是有几个操作:
然后只有两种操作类型,"C a b c"代表的意思是:给A[a]到A[b]的数都加上一个数c。 "Q a b”的意思是让你求下标为a到b的和。
线段树裸题,但是是第一次做,所以还是写写吧,而且网上线段树模板鱼龙混杂,我也是费了好大力气才找到一个适合我的模板的。
在此附上链接:http://www.tuicool.com/articles/BVVfi2E
如果不懂线段树的可以先去学习一下上面那个人写的内容然后这道题我想就很简单了吧。
这道线段树的题,我总共写了三遍,但是每一遍都会产生不同的错误,总共错了 两次,但是在写的过程中渐渐的找到了适合自己的写法,我想还是要总结一下:
1.总是会忘记打pushup和pushdown,或者说分不太清楚什么时候应该用什么函数,但是就我最近做过的题来看,当建立一棵线段树时,最后是要加上pushup的,因为你要把上面的结点也得更新到啊。
然后,update的时候,如果像是区间更新的问题的话,如果父节点上还带有lazy标志的话,那么就要pushdown,因为你要把那个lazy标志传下去。但是update的最后,因为你更新完了,所以你还要再继续pushup,别忘了把父节点也更新了。
2.是小问题,但是也是致命的问题,就是我经常会忘记打符号,比如说一个数在原来的基础上再加上一个数x,那么不是tree[v].x+=x吗,但是我有时候会不小心忘记打+号,这个问题虽然小,但是小的地方最难查,所以还是要注重细节!!!
以下是写了三遍之后个人认为比较满意的代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 111111
typedef long long ll;
ll sum=0;
ll a[maxn];
struct node{
int l,r;
ll x,add;
}tree[maxn*4];
void pushup(int v){
int temp=v<<1;
tree[v].x=tree[temp].x+tree[temp+1].x;
}
void pushdown(int v){
int temp=v<<1;
tree[temp].add+=tree[v].add;
tree[temp+1].add+=tree[v].add;
tree[temp].x+=tree[v].add*(tree[temp].r-tree[temp].l+1);
tree[temp+1].x+=tree[v].add*(tree[temp+1].r-tree[temp+1].l+1);
tree[v].add=0;
}
void build(int v,int l,int r){
tree[v].l=l;
tree[v].r=r;
tree[v].add=0;
tree[v].x=0;
if(l==r){
tree[v].x=a[l];
return ;
}
int mid=(l+r)>>1;
int temp=v<<1;
build(temp,l,mid);
build(temp+1,mid+1,r);
pushup(v);
}
void query(int v,int l,int r){
if(tree[v].l==l&&tree[v].r==r){
sum+=tree[v].x;
return;
}
if(tree[v].add) pushdown(v);
int mid=(tree[v].l+tree[v].r)>>1;
int temp=v<<1;
if(r<=mid) query(temp,l,r);
else if(l>mid) query(temp+1,l,r);
else{
query(temp,l,mid);
query(temp+1,mid+1,r);
}
}
void update(int v,int l,int r,int x){
if(tree[v].l==l&&tree[v].r==r){
tree[v].add+=x;
tree[v].x+=x*(tree[v].r-tree[v].l+1);
return ;
}
if(tree[v].add) pushdown(v);
int mid=(tree[v].l+tree[v].r)>>1;
int temp=v<<1;
if(r<=mid) update(temp,l,r,x);
else if(l>mid) update(temp+1,l,r,x);
else{
update(temp,l,mid,x);
update(temp+1,mid+1,r,x);
}
pushup(v); //!!!
}
int main(){
int n,Q;
char ss[5];
scanf("%d%d",&n,&Q);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,1,n);
while(Q--){
int a,b,c;
scanf("%s",ss);
if(ss[0]=='C'){
scanf("%d%d%d",&a,&b,&c);
update(1,a,b,c);
}
else if(ss[0]=='Q'){
sum=0;
scanf("%d%d",&a,&b);
query(1,a,b);
printf("%lld\n",sum);
}
}
}
没有什么新的东西是刚开始就能够得心应手的,所以慢慢的进步吧,加油,hades!