HDU 4578(线段树
题目:
要求支持四种区间操作:1.区间加上一个数,2.区间乘上一个数,3.区间全部变成一个数,4.区间求和(要求支持平方和,立方和.
思路:这题有些恶心啊.....幸而交上去1A,不然我感觉真得难以debug.....注意下传标记时三种操作的顺序.(常数有点大...估计是多余的mod造成的...
//priority_queue #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include <queue> #include <stack> #include <functional> #include <set> #include <cmath> #include <bitset> using namespace std; #define IOS std::ios::sync_with_stdio (false);std::cin.tie(0) #define pb push_back #define PB pop_back #define bk back() #define fs first #define se second #define sq(x) ((x)*(x)) #define eps (1e-8) #define INF (0x3f3f3f3f) #define clr(x) memset((x),0,sizeof (x)) #define cp(a,b) memcpy((a),(b),sizeof (b)) typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> P; #define chl (o<<1) #define chr ((o<<1)|1) #define mi (l+((r-l)>>1)) const int mod=10007; const int maxn=5e5; inline int qpow(int a,int p){ int ans=1; a%=mod; while(p>0){ if(p&1) ans=ans*a%mod; a=a*a%mod;p>>=1; } return ans; } int val[3][maxn]; int add[maxn],mul[maxn],chg[maxn]; inline void ma(int &x,int b){x=(x+b)%mod;} inline void mm(int &x,int b){x=x*b%mod;} void _init(int o,int l,int r){ val[0][o]=val[1][o]=val[2][o]=0; mul[o]=1,chg[o]=-1,add[o]=0; if(r-l<=1) return; _init(chl,l,mi); _init(chr,mi,r); } void change(int o,int l,int r,int x){ val[0][o]=(r-l)*x%mod; val[1][o]=(r-l)*qpow(x,2)%mod; val[2][o]=(r-l)*qpow(x,3)%mod; } void changeAdd(int o,int l,int r,int x){ ma(val[2][o],3*val[1][o]*x%mod+3*val[0][o]*x%mod*x%mod+sq(x)%mod*x%mod* (r-l)%mod); ma(val[1][o],val[0][o]*x*2%mod+sq(x)%mod*(r-l)%mod); ma(val[0][o],(r-l)*x); } void changeMul(int o,int l,int r,int x){ mm(val[0][o],x); mm(val[1][o],qpow(x,2)); mm(val[2][o],qpow(x,3)); } void push(int o,int l,int r){ if(r-l<=1) return; if(chg[o]!=-1){ int x=chg[o];chg[o]=-1; add[chl]=add[chr]=0; mul[chl]=mul[chr]=1; chg[chl]=chg[chr]=x; change(chl,l,mi,x); change(chr,mi,r,x); } changeMul(chl,l,mi,mul[o]);changeAdd(chl,l,mi,add[o]); changeMul(chr,mi,r,mul[o]);changeAdd(chr,mi,r,add[o]); mm(mul[chl],mul[o]);mm(mul[chr],mul[o]); mm(add[chl],mul[o]);mm(add[chr],mul[o]); ma(add[chl],add[o]);ma(add[chr],add[o]); mul[o]=1,add[o]=0; } void update(int o,int l,int r){ if(r-l<=1) return; val[0][o]=(val[0][chl]+val[0][chr])%mod; val[1][o]=(val[1][chl]+val[1][chr])%mod; val[2][o]=(val[2][chl]+val[2][chr])%mod; } void rangeMul(int o,int a,int b,int l,int r,int x){ if(l>=b||r<=a) return; push(o,l,r); if(l>=a&&r<=b){ changeMul(o,l,r,x); mm(mul[o],x); return; } rangeMul(chl,a,b,l,mi,x); rangeMul(chr,a,b,mi,r,x); update(o,l,r); } void rangeAdd(int o,int a,int b,int l,int r,int x){ if(l>=b||r<=a) return; push(o,l,r); if(l>=a&&r<=b){ changeAdd(o,l,r,x); ma(add[o],x); return; } rangeAdd(chl,a,b,l,mi,x); rangeAdd(chr,a,b,mi,r,x); update(o,l,r); } void rangeChange(int o,int a,int b,int l,int r,int x){ if(l>=b||r<=a) return; push(o,l,r); if(l>=a&&r<=b){ change(o,l,r,x); chg[o]=x; return; } rangeChange(chl,a,b,l,mi,x); rangeChange(chr,a,b,mi,r,x); update(o,l,r); } int query(int o,int a,int b,int l,int r,int x){ if(l>=b||r<=a) return 0; push(o,l,r); if(l>=a&&r<=b) return val[x][o]; return (query(chl,a,b,l,mi,x)+query(chr,a,b,mi,r,x))%mod; } int n,m; int main(){ freopen("/home/slyfc/CppFiles/in","r",stdin); //freopen("/home/slyfc/CppFiles/out","w",stdout); while(scanf("%d%d",&n,&m)!=EOF){ if(n==0&&m==0) break; _init(1,1,n+1); while(m--){ int t,x,y,c; scanf("%d%d%d%d",&t,&x,&y,&c); if(t==1){ rangeAdd(1,x,y+1,1,n+1,c); }else if(t==2){ rangeMul(1,x,y+1,1,n+1,c); }else if(t==3){ rangeChange(1,x,y+1,1,n+1,c); }else{ printf("%d\n",query(1,x,y+1,1,n+1,c-1)); } } } return 0; }