题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=470
操作 attract (l,r,x) 区间的和乘X;
query(l,r), 查询区间的和
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100030
using namespace std;
struct node
{
int l,r;
int lazy,sum;
bool flag;
}root[N<<2];
int shu[N];
void build(int t,int l,int r)
{
root[t].l=l;
root[t].r=r;root[t].lazy=1;
root[t].flag=0;
if(root[t].l==root[t].r)
{
root[t].sum=shu[l];
return;
}
build(t<<1,l,(l+r)/2);build(t<<1|1,(l+r)/2+1,r);
root[t].sum=root[t<<1].sum+root[t<<1|1].sum;
}
void up(int t)
{
if(root[t<<1].flag&&root[t<<1].l!=root[t<<1].r)up(t<<1);
if(root[t<<1|1].flag&&root[t<<1|1].l!=root[t<<1|1].r)up(t<<1|1);
root[t<<1].lazy*=root[t].lazy;root[t<<1|1].lazy*=root[t].lazy;
root[t<<1].flag=1;root[t<<1|1].flag=1;
root[t<<1].sum*=root[t].lazy;
root[t<<1|1].sum*=root[t].lazy;
root[t].lazy=1;
root[t].flag=0;
}
void update(int t,int l,int r,int date)
{
if(root[t].l==l&&root[t].r==r)
{ if(root[t].flag&&root[t].l!=root[t].r)//就是这调了N久才知道要补上
up(t);
root[t].sum*=date;root[t].lazy=date;root[t].flag=1; return;}
else
{
if(root[t].flag&&root[t].l!=root[t].r)
{
up(t);
}
if(root[t<<1].r>=r)update(t<<1,l,r,date);
else if(root[t<<1|1].l<=l)update(t<<1|1,l,r,date);
else {update(t<<1,l,root[t<<1].r,date);update(t<<1|1,root[t<<1|1].l,r,date);}
root[t].sum=root[t<<1].sum+root[t<<1|1].sum;
}
}
int query(int t,int l,int r)
{
int s;
if(root[t].l==l&&root[t].r==r)
return root[t].sum;
else
{
if(root[t].flag&&root[t].l!=root[t].r)
up(t);
if(root[t<<1].r>=r)s=query(t<<1,l,r);
else if(root[t<<1|1].l<=l)
s=query(t<<1|1,l,r);
else
s=query(t<<1,l,root[t<<1].r)+query(t<<1|1,root[t<<1|1].l,r);
root[t].sum=root[t<<1].sum+root[t<<1|1].sum;
}
return s;
}
int main()
{
int n,q;
char s[33];
int a,b,c;
int cas=0;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
cin>>shu[i];
build(1,1,n);
scanf("%d",&q);
printf("Case %d:\n",++cas);
for(int j=1;j<=q;j++)
{
scanf("%s%d%d",s,&a,&b);
if(strcmp(s,"attack")==0)
{
scanf("%d",&c);
update(1,a,b,c);
}
else
{
printf("%d\n",query(1,a,b));
}
}
}
return 0;
}