Yuanfang is puzzled with the question below:
There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between ax and ay inclusive. In other words, do transformation ak<---ak+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between ax and ay inclusive. In other words, do transformation ak<---ak×c, k = x,x+1,…,y.
Operation 3: Change the numbers between ax and ay to c, inclusive. In other words, do transformation ak<---c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between ax and ay inclusive. In other words, get the result of axp+ax+1p+…+ay p.
Yuanfang has no idea of how to do it. So he wants to ask you to help him.
区间加、乘、改、求和,线段树,推出式子即可。

1 #include<stdio.h> 2 #include<string.h> 3 const int mod=10007; 4 const int maxm=100005; 5 6 int st[4][maxm<<2],add[maxm<<2],mul[maxm<<2],cha[maxm<<2]; 7 8 void build(int o,int l,int r){ 9 st[1][o]=st[2][o]=st[3][o]=0; 10 add[o]=cha[o]=0; 11 mul[o]=1; 12 if(l==r)return; 13 int m=l+((r-l)>>1); 14 build(o<<1,l,m); 15 build(o<<1|1,m+1,r); 16 } 17 18 void pushdown(int o,int l,int r){ 19 if(cha[o]){ 20 cha[o<<1]=cha[o<<1|1]=cha[o]; 21 add[o<<1]=add[o<<1|1]=0; 22 mul[o<<1]=mul[o<<1|1]=1; 23 int m=l+((r-l)>>1); 24 st[1][o<<1]=cha[o]*(m-l+1)%mod; 25 st[1][o<<1|1]=cha[o]*(r-m)%mod; 26 st[2][o<<1]=(cha[o]*cha[o]%mod)*(m-l+1)%mod; 27 st[2][o<<1|1]=(cha[o]*cha[o]%mod)*(r-m)%mod; 28 st[3][o<<1]=((cha[o]*cha[o]%mod)*cha[o]%mod)*(m-l+1)%mod; 29 st[3][o<<1|1]=((cha[o]*cha[o]%mod)*cha[o]%mod)*(r-m)%mod; 30 cha[o]=0; 31 } 32 if(add[o]||(mul[o]!=1)){ 33 int m=l+((r-l)>>1); 34 add[o<<1]=(add[o<<1]*mul[o]%mod+add[o])%mod; 35 mul[o<<1]=mul[o<<1]*mul[o]%mod; 36 st[3][o<<1]=( 37 ((st[3][o<<1]*mul[o]%mod)*mul[o]%mod)*mul[o]%mod 38 +(((st[2][o<<1]*mul[o]%mod)*mul[o]%mod)*add[o]%mod)*3%mod 39 +(((st[1][o<<1]*mul[o]%mod)*add[o]%mod)*add[o]%mod)*3%mod 40 +((add[o]*add[o]%mod)*add[o]%mod)*(m-l+1)%mod 41 )%mod; 42 st[2][o<<1]=( 43 (st[2][o<<1]*mul[o]%mod)*mul[o]%mod 44 +((st[1][o<<1]*mul[o]%mod)*add[o]%mod)*2%mod 45 +(add[o]*add[o]%mod)*(m-l+1)%mod 46 )%mod; 47 st[1][o<<1]=( 48 st[1][o<<1]*mul[o]%mod+add[o]*(m-l+1)%mod 49 )%mod; 50 add[o<<1|1]=(add[o<<1|1]*mul[o]%mod+add[o])%mod; 51 mul[o<<1|1]=mul[o<<1|1]*mul[o]%mod; 52 st[3][o<<1|1]=( 53 ((st[3][o<<1|1]*mul[o]%mod)*mul[o]%mod)*mul[o]%mod 54 +(((st[2][o<<1|1]*mul[o]%mod)*mul[o]%mod)*add[o]%mod)*3%mod 55 +(((st[1][o<<1|1]*mul[o]%mod)*add[o]%mod)*add[o]%mod)*3%mod 56 +((add[o]*add[o]%mod)*add[o]%mod)*(r-m)%mod 57 )%mod; 58 st[2][o<<1|1]=( 59 (st[2][o<<1|1]*mul[o]%mod)*mul[o]%mod 60 +((st[1][o<<1|1]*mul[o]%mod)*add[o]%mod)*2%mod 61 +(add[o]*add[o]%mod)*(r-m)%mod 62 )%mod; 63 st[1][o<<1|1]=( 64 st[1][o<<1|1]*mul[o]%mod+add[o]*(r-m)%mod 65 )%mod; 66 add[o]=0; 67 mul[o]=1; 68 } 69 } 70 71 void update3(int o,int l,int r,int ql,int qr,int c){ 72 if(ql<=l&&qr>=r){ 73 st[1][o]=c*(r-l+1)%mod; 74 st[2][o]=(c*c%mod)*(r-l+1)%mod; 75 st[3][o]=((c*c%mod)*c%mod)*(r-l+1)%mod; 76 cha[o]=c; 77 add[o]=0; 78 mul[o]=1; 79 return; 80 } 81 pushdown(o,l,r); 82 int m=l+((r-l)>>1); 83 if(ql<=m)update3(o<<1,l,m,ql,qr,c); 84 if(qr>=m+1)update3(o<<1|1,m+1,r,ql,qr,c); 85 st[1][o]=(st[1][o<<1]+st[1][o<<1|1])%mod; 86 st[2][o]=(st[2][o<<1]+st[2][o<<1|1])%mod; 87 st[3][o]=(st[3][o<<1]+st[3][o<<1|1])%mod; 88 } 89 90 void update1(int o,int l,int r,int ql,int qr,int c){ 91 if(ql<=l&&qr>=r){ 92 add[o]=(add[o]+c)%mod; 93 st[3][o]=(st[3][o]+(c*st[2][o]%mod)*3%mod+((st[1][o]*c%mod)*c%mod)*3%mod+((c*c%mod)*c%mod)*(r-l+1)%mod)%mod; 94 st[2][o]=(st[2][o]+(c*st[1][o]%mod)*2%mod+(c*c%mod)*(r-l+1)%mod)%mod; 95 st[1][o]=(st[1][o]+c*(r-l+1)%mod)%mod; 96 return; 97 } 98 pushdown(o,l,r); 99 int m=l+((r-l)>>1); 100 if(ql<=m)update1(o<<1,l,m,ql,qr,c); 101 if(qr>=m+1)update1(o<<1|1,m+1,r,ql,qr,c); 102 st[1][o]=(st[1][o<<1]+st[1][o<<1|1])%mod; 103 st[2][o]=(st[2][o<<1]+st[2][o<<1|1])%mod; 104 st[3][o]=(st[3][o<<1]+st[3][o<<1|1])%mod; 105 } 106 107 void update2(int o,int l,int r,int ql,int qr,int c){ 108 if(ql<=l&&qr>=r){ 109 add[o]=add[o]*c%mod; 110 mul[o]=mul[o]*c%mod; 111 st[3][o]=((st[3][o]*c%mod)*c%mod)*c%mod; 112 st[2][o]=(st[2][o]*c%mod)*c%mod; 113 st[1][o]=st[1][o]*c%mod; 114 return; 115 } 116 pushdown(o,l,r); 117 int m=l+((r-l)>>1); 118 if(ql<=m)update2(o<<1,l,m,ql,qr,c); 119 if(qr>=m+1)update2(o<<1|1,m+1,r,ql,qr,c); 120 st[1][o]=(st[1][o<<1]+st[1][o<<1|1])%mod; 121 st[2][o]=(st[2][o<<1]+st[2][o<<1|1])%mod; 122 st[3][o]=(st[3][o<<1]+st[3][o<<1|1])%mod; 123 } 124 125 int query(int o,int l,int r,int ql,int qr,int p){ 126 if(ql<=l&&qr>=r){ 127 return st[p][o]; 128 } 129 pushdown(o,l,r); 130 int m=l+((r-l)>>1); 131 int ans=0; 132 if(ql<=m)ans=(ans+query(o<<1,l,m,ql,qr,p))%mod; 133 if(qr>=m+1)ans=(ans+query(o<<1|1,m+1,r,ql,qr,p))%mod; 134 return ans; 135 } 136 137 int main(){ 138 int n,m; 139 while(scanf("%d%d",&n,&m)!=EOF&&n+m){ 140 build(1,1,n); 141 for(int i=1;i<=m;++i){ 142 int t,l,r,p; 143 scanf("%d%d%d%d",&t,&l,&r,&p); 144 if(t==1)update1(1,1,n,l,r,p); 145 else if(t==2)update2(1,1,n,l,r,p); 146 else if(t==3)update3(1,1,n,l,r,p); 147 else if(t==4)printf("%d\n",query(1,1,n,l,r,p)); 148 } 149 } 150 return 0; 151 }
来源:https://www.cnblogs.com/cenariusxz/p/6598413.html