题意:
一串数字,C操作 从x,到y 所有数字+z
Q x y 所有数字的。
思路;
线段树区间更新,区间查询模板题。竟然写WA了。注意long long 传参也要long long 。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#define lson i<<1,left,mid
#define rson i<<1|1,mid+1,right
using namespace std;
struct TreeNode
{
long long lazy,val,left,right,tag;
} Tree[400005];
long long ans=0;
void Build(int i , int left , int right )
{
Tree[i].lazy=0;
Tree[i].left=left;
Tree[i].right=right;
Tree[i].tag=0;
if(left==right)
{
scanf("%lld",&Tree[i].val);
return ;
}
int mid=(left+right)>>1;
Build(lson);
Build(rson);
Tree[i].val=Tree[i<<1].val+Tree[i<<1|1].val;
}
void add(int i,int left, int right , long long w)
{
if(Tree[i].left==left && Tree[i].right ==right )
{
Tree[i].tag+=w;
Tree[i].lazy=1;
Tree[i].val+=w*(right - left +1);
return ;
}
int mid=(Tree[i].left+Tree[i].right)>>1;
if(Tree[i].lazy==1)
{
Tree[i].lazy=0;
add(i<<1,Tree[i].left,mid,Tree[i].tag);
add(i<<1|1,mid+1,Tree[i].right,Tree[i].tag);
Tree[i].tag=0;
}
if(right<=mid)
{
add(i<<1,left,right,w);
}
else if(left>mid)
{
add(i<<1|1,left,right,w);
}
else
{
add(lson,w);
add(rson,w);
}
Tree[i].val=Tree[i<<1].val+Tree[i<<1|1].val;
}
void query(int i,int left,int right)
{
if(Tree[i].left==left && Tree[i].right ==right )
{
ans+=Tree[i].val;
return ;
}
int mid=(Tree[i].left+Tree[i].right)>>1;
if(Tree[i].lazy==1)
{
Tree[i].lazy=0;
add(i<<1,Tree[i].left,mid,Tree[i].tag);
add(i<<1|1,mid+1,Tree[i].right,Tree[i].tag);
Tree[i].tag=0;
}
if(right<=mid)
{
query(i<<1,left,right);
}
else if(left>mid)
{
query(i<<1|1,left,right);
}
else
{
query(lson);
query(rson);
}
Tree[i].val=Tree[i<<1].val+Tree[i<<1|1].val;
}
int main()
{
long long x,y,z,n,m;
scanf("%lld%lld",&n,&m);
Build(1,1,n);
while(m--)
{
char s[5];
scanf("%s",s);
if(s[0]=='C')
{
scanf("%lld%lld%lld",&x,&y,&z);
if(x>y)
{
swap(x,y);
}
add(1,x,y,z);
}
else
{
ans=0;
scanf("%lld%lld",&x,&y);
if(x>y)
{
swap(x,y);
}
query(1,x,y);
printf("%lld\n",ans);
}
}
return 0;
}
/*
10 110
1 2 3 4 5 6 7 8 9 10
C 1 3 3
C 1 4 4
Q 1 1
*/