hdu4578(线段树)

时间:2024-06-04 19:06:38

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4578

题意:n个数,初始值为0,4种操作:

1。将某个区间所有值加上另一个值;

2。将区间所有值都乘上另一个值;

3。将区间所有值置为某个值;

4。查询区间中所有值的p次方和。

详细分析:http://www.cnblogs.com/GBRgbr/archive/2013/08/13/3254442.html

#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 10007
#define inf 0x3f3f3f3f
#define N 100010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
LL sum1[N<<],sum2[N<<],sum3[N<<];
LL mul[N<<],add[N<<];
void Pushup(int rt)
{
sum1[rt]=(sum1[rt<<]+sum1[rt<<|])%mod;
sum2[rt]=(sum2[rt<<]+sum2[rt<<|])%mod;
sum3[rt]=(sum3[rt<<]+sum3[rt<<|])%mod;
}
void Pushdown(int rt,int len)
{
if(mul[rt]!=)
{
mul[rt<<]=mul[rt<<]*mul[rt]%mod;
mul[rt<<|]=mul[rt<<|]*mul[rt]%mod;
add[rt<<]=add[rt<<]*mul[rt]%mod;
add[rt<<|]=add[rt<<|]*mul[rt]%mod;
LL u=mul[rt]*mul[rt]%mod,v=mul[rt]*mul[rt]*mul[rt]%mod;
sum1[rt<<]=sum1[rt<<]*mul[rt]%mod;
sum1[rt<<|]=sum1[rt<<|]*mul[rt]%mod;
sum2[rt<<]=sum2[rt<<]*u%mod;
sum2[rt<<|]=sum2[rt<<|]*u%mod;
sum3[rt<<]=sum3[rt<<]*v%mod;
sum3[rt<<|]=sum3[rt<<|]*v%mod;
mul[rt]=;
}
if(add[rt]!=)
{
add[rt<<]=(add[rt<<]+add[rt])%mod;
add[rt<<|]=(add[rt<<|]+add[rt])%mod;
LL u=add[rt]*add[rt]%mod,v=add[rt]*add[rt]*add[rt]%mod;
LL t1=sum1[rt<<],t2=sum1[rt<<|];
LL t3=sum2[rt<<],t4=sum2[rt<<|];
sum1[rt<<]=(sum1[rt<<]+(len-(len>>))*add[rt])%mod;
sum1[rt<<|]=(sum1[rt<<|]+(len>>)*add[rt])%mod;
sum2[rt<<]=(sum2[rt<<]+(len-(len>>))*u+t1*add[rt]*)%mod;
sum2[rt<<|]=(sum2[rt<<|]+(len>>)*u+t2*add[rt]*)%mod;
sum3[rt<<]=(sum3[rt<<]+(len-(len>>))*v+t3*add[rt]*+*u*t1)%mod;
sum3[rt<<|]=(sum3[rt<<|]+(len>>)*v+t4*add[rt]*+*u*t2)%mod;
add[rt]=;
}
} void build(int l,int r,int rt)
{
sum1[rt]=sum2[rt]=sum3[rt]=;
mul[rt]=;add[rt]=;
if(l==r)return;
int m=(l+r)>>;
build(lson);
build(rson);
}
void update(int L,int R,LL c,int l,int r,int rt,int op)
{
if(L<=l&&r<=R)
{
LL u=c*c%mod,v=c*c*c%mod;
LL len=r-l+;
if(op==)
{
LL t1=sum1[rt],t2=sum2[rt];
add[rt]+=c;add[rt]%=mod;
sum1[rt]=(sum1[rt]+len*c)%mod;
sum2[rt]=(sum2[rt]+*t1*c+len*u)%mod;
sum3[rt]=(sum3[rt]+len*v+*c*t2+*u*t1)%mod;
}
else if(op==)
{
mul[rt]*=c;mul[rt]%=mod;
add[rt]*=c;add[rt]%=mod;
sum1[rt]=sum1[rt]*c%mod;
sum2[rt]=sum2[rt]*u%mod;
sum3[rt]=sum3[rt]*v%mod;
}
else
{
add[rt]=c;mul[rt]=;
sum1[rt]=len*c%mod;
sum2[rt]=len*u%mod;
sum3[rt]=len*v%mod;
}
return;
}
Pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)update(L,R,c,lson,op);
if(m<R)update(L,R,c,rson,op);
Pushup(rt);
}
LL query(int L,int R,int l,int r,int rt,int op)
{
if(L<=l&&r<=R)
{
if(op==)return sum1[rt];
else if(op==)return sum2[rt];
else return sum3[rt];
}
Pushdown(rt,r-l+);
int m=(l+r)>>;
LL res=;
if(L<=m)res+=query(L,R,lson,op);
if(m<R)res+=query(L,R,rson,op);
return res%mod;
} int main()
{
int n,m;
int op,x,y,c;
while(scanf("%d%d",&n,&m)>)
{
if(m+n==)break;
build(,n,);
while(m--)
{
scanf("%d%d%d%d",&op,&x,&y,&c);
if(op==)
{
printf("%I64d\n",query(x,y,,n,,c));
}
else
{
update(x,y,c,,n,,op);
// printf("%d\n",sum1[1]);
}
}
}
}