Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## Re:给一个线段树容易出错的测试用例

Posted by cyy1308947278 at 2016-08-26 21:35:01 on Problem 3468
In Reply To:Re:给一个线段树容易出错的测试用例 Posted by:black_sky_zhang at 2016-07-28 11:53:19
```#include<stdio.h>
#include<string.h>
#include<iostream>
#define N 111111
#define INF 0x3f3f3f3
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,n,1
#define LL long long
using namespace std;
void Pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void Build(int l,int r,int rt)
{
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int m=(l+r)>>1;
Build(l,m,rt<<1);
Build(m+1,r,rt<<1|1);
Pushup(rt);
}
void Pushdown(int rt,int m)
{
{
}
}
void Update(LL L,LL R,LL c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
sum[rt]+=(LL)(r-l+1)*(LL)c;
return ;
}
Pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)Update(L,R,c,lson);
if(R>m)Update(L,R,c,rson);
Pushup(rt);
}
LL Query(LL L,LL R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return sum[rt];
Pushdown(rt,r-l+1);
int m=(l+r)>>1;
LL res=0;
if(L<=m)res+=Query(L,R,l,m,rt<<1);
if(R>m)res+=Query(L,R,m+1,r,rt<<1|1);
return res;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(sum,0,sizeof(sum));
int a,b,c;
Build(1,n,1);
char ch[3];
while(m--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
LL aa,bb;
scanf("%lld%lld",&aa,&bb);
printf("%lld\n",Query(aa,bb,root));
}
else
{
LL aa,bb,cc;
scanf("%lld%lld%lld",&aa,&bb,&cc);
Update(aa,bb,cc,root);
}
}
}
return 0;
}
```

Followed by: