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.
题解:正常线段数模板题,节点记录的为两儿子中的最大值。套用模板请戳这 @_@
代码如下:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 55557
#define N 100005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<x<<" "<<endl
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
typedef struct Node
{
int l;
int r;
int mid()
{
return (l + r) / 2.0;
}
};
LL ans;
Node node[N << 2];//为什么乘4
LL add[N << 2];// 延迟标记数组
LL sum[N << 2];//每个节点的sum
void PushUp(int i);
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
void build(int i,int l,int r)//建树并完成初始化
{
node[i].l = l;
node[i].r = r;
sum[i] = 0;
add[i] = 0;
if(l == r)
{
cin >> sum[i];//在内部输入,为叶子节点
return ;//一定要写return!!!
}
int m = node[i].mid();
build(lson);//只想下更新sum
build(rson);
PushUp(i);//递归回去完成区间的更新.
}
void PushUp(int i) // 建树递归返回时更新。
{
sum[i] = sum[i << 1] + sum[i << 1 | 1];
}
void PushDown(int i,int L) // L为区间长度 (延迟标记)
{
if(add[i])
{
add[i << 1] += add[i];
add[i << 1 | 1] += add[i];
sum[i << 1] += add[i] * (L - (L >> 1)); // 易错
sum[i << 1 | 1] += add[i] * (L >> 1);
add[i] = 0;
}
}
void update(int v,int l,int r,int i)//更新操作,区间【l,r】加值v
{
if(node[i].l == l && node[i].r == r)//当找到目标区间时,
{
sum[i] += (LL)v * (r - l + 1);//更新当前节点的值,
add[i] += v;//延迟标记+V
return ;
}
PushDown(i,node[i].r - node[i].l + 1);// 因为没有匹配到合适的区间,向下寻找,检查这个区间是否有标记.
int m = node[i].mid();//如果有标记,说明应该先把之前的标记量给计算出,再进行寻找.
if(r <= m)
{
update(v,l,r,i << 1);
}
else
{
if(l > m)
update(v,l,r,i << 1 | 1);
else
{
update(v,l,m,i << 1);
update(v,m + 1,r,i << 1 | 1);
}
}
PushUp(i);//递归返回
}
void query(int l,int r,int i) //查询
{
if(node[i].l == l && node[i].r == r)
{
ans += sum[i];
return ;
}
PushDown(i,node[i].r - node[i].l + 1);
int m = node[i].mid();
if(r <= m)
query(l,r,i << 1 );
else
{
if(l > m)
query(l,r,i << 1 | 1);
else
{
query(l,m,i << 1);
query(m + 1,r,i << 1 | 1);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
while(cin >> n >> m)
{
build(1,1,n);
string s;
while(m--)
{
int a,b,c;
cin >> s;
if(s[0] == 'Q')
{
ans = 0;
cin >> a >> b;
query(a,b,1);
cout << ans << endl;
}
else
{
cin >> a >> b >> c;
update(c,a,b,1);
}
}
}
}