[hdoj4578][多延迟标记的线段树]
Transformation
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 9392 Accepted Submission(s): 2408
Problem Description
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.
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.
Input
There are no more than 10 test cases.
For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000.
Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3)
The input ends with 0 0.
For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000.
Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3)
The input ends with 0 0.
Output
For each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.
Sample Input
5 5
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0
Sample Output
307
7489
题意:有4种操作,区间加,区间乘,区间修改以及区间幂之和。
题解:区间加==(mul==1,add==x) 区间乘==(mul==x,add==0)区间修改==(mul==0,add==x)区间幂通过展开式转化为区间乘和加,lazy标记用来标记当前点已经被更新过,pushdown(rt)用于更新子节点
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define debug(x) cout<<"["<<#x<<"]"<<" is "<<x<<endl 4 #define forp(x) for(int i=1;i<=x;i++) 5 #define scai(x) scanf("%d",&x) 6 #define scal(x) scanf("%lld",&x) 7 #define pri(x) printf("%d\n",x) 8 #define prl(x) printf("%lld\n",x) 9 typedef long long ll; 10 const int maxn=1e5+5; 11 const ll mod=1e4+7; 12 struct node{ 13 int l; 14 int r; 15 ll ad; 16 ll mu; 17 ll a[4]; 18 }N[maxn<<2]; 19 void pushup(int rt){ 20 N[rt].a[1]=N[rt<<1].a[1]+N[(rt<<1)|1].a[1]; 21 N[rt].a[2]=N[rt<<1].a[2]+N[(rt<<1)|1].a[2]; 22 N[rt].a[3]=N[rt<<1].a[3]+N[(rt<<1)|1].a[3]; 23 N[rt].mu%=mod; 24 N[rt].ad%=mod; 25 N[rt].a[1]%=mod; 26 N[rt].a[2]%=mod; 27 N[rt].a[3]%=mod; 28 } 29 void pushdown(int rt){ 30 if(N[rt].mu!=1){ 31 ll m=N[rt].mu; 32 N[rt].mu=1; 33 N[rt<<1].ad=N[rt<<1].ad*m%mod; 34 N[(rt<<1)|1].ad=N[(rt<<1)|1].ad*m%mod; 35 N[rt<<1].mu=N[rt<<1].mu*m%mod; 36 N[(rt<<1)|1].mu=N[(rt<<1)|1].mu*m%mod; 37 N[rt<<1].a[1]=N[rt<<1].a[1]*m%mod; 38 N[rt<<1].a[2]=N[rt<<1].a[2]*m%mod*m%mod; 39 N[rt<<1].a[3]=N[rt<<1].a[3]*m%mod*m%mod*m%mod; 40 N[(rt<<1)|1].a[1]=N[(rt<<1)|1].a[1]*m%mod; 41 N[(rt<<1)|1].a[2]=N[(rt<<1)|1].a[2]*m%mod*m%mod; 42 N[(rt<<1)|1].a[3]=N[(rt<<1)|1].a[3]*m%mod*m%mod*m%mod; 43 } 44 if(N[rt].ad!=0){ 45 ll m=N[rt].ad; 46 N[rt].ad=0; 47 N[rt<<1].ad=N[rt<<1].ad+m%mod; 48 N[(rt<<1)|1].ad=N[(rt<<1)|1].ad+m%mod; 49 N[rt<<1].a[3]=N[rt<<1].a[3]+m%mod*m%mod*m%mod*(N[rt<<1].r-N[rt<<1].l+1)%mod+3*m*N[rt<<1].a[2]%mod+3*m%mod*m%mod*N[rt<<1].a[1]%mod; 50 N[rt<<1].a[3]%=mod; 51 N[rt<<1].a[2]=N[rt<<1].a[2]+m%mod*m%mod*(N[rt<<1].r-N[rt<<1].l+1)%mod+2*m*N[rt<<1].a[1]%mod; 52 N[rt<<1].a[2]%=mod; 53 N[rt<<1].a[1]=N[rt<<1].a[1]+m*(N[rt<<1].r-N[rt<<1].l+1)%mod; 54 N[rt<<1].a[1]%=mod; 55 N[(rt<<1)|1].a[3]=N[(rt<<1)|1].a[3]%mod+m%mod*m%mod*m%mod*(N[(rt<<1)|1].r-N[(rt<<1)|1].l+1)%mod+3*m*N[(rt<<1)|1].a[2]%mod+3*m%mod*m%mod*N[(rt<<1)|1].a[1]%mod; 56 N[(rt<<1)|1].a[3]%=mod; 57 N[(rt<<1)|1].a[2]=N[(rt<<1)|1].a[2]%mod+m%mod*m%mod*(N[(rt<<1)|1].r-N[(rt<<1)|1].l+1)%mod+2*m*N[(rt<<1)|1].a[1]%mod; 58 N[(rt<<1)|1].a[2]%=mod; 59 N[(rt<<1)|1].a[1]=N[(rt<<1)|1].a[1]+m*(N[(rt<<1)|1].r-N[(rt<<1)|1].l+1)%mod; 60 N[(rt<<1)|1].a[1]%=mod; 61 } 62 } 63 void build(int L,int R,int rt){ 64 N[rt].l=L; 65 N[rt].r=R; 66 N[rt].mu=1; 67 N[rt].ad=0; 68 if(L==R){ 69 N[rt].a[1]=0; 70 N[rt].a[2]=0; 71 N[rt].a[3]=0; 72 return; 73 } 74 int mid=(L+R)/2; 75 build(L,mid,rt<<1); 76 build(mid+1,R,(rt<<1)|1); 77 pushup(rt); 78 } 79 void update(int L,int R,int rt,int l1,int r1,ll add,ll mul){ 80 N[rt].mu%=mod; 81 N[rt].ad%=mod; 82 N[rt].a[1]%=mod; 83 N[rt].a[2]%=mod; 84 N[rt].a[3]%=mod; 85 if(l1<=L&&r1>=R){ 86 if(mul!=1){ 87 ll m=mul; 88 N[rt].ad=N[rt].ad*m%mod; 89 N[rt].mu=N[rt].mu*m%mod; 90 N[rt].a[1]=N[rt].a[1]*m%mod; 91 N[rt].a[2]=N[rt].a[2]*m%mod*m%mod; 92 N[rt].a[3]=N[rt].a[3]*m%mod*m%mod*m%mod; 93 } 94 if(add!=0){ 95 ll m=add; 96 N[rt].ad=N[rt].ad+m%mod; 97 N[rt].a[3]=N[rt].a[3]+m%mod*m%mod*m%mod*(N[rt].r-N[rt].l+1)%mod+3*m*N[rt].a[2]%mod+3*m%mod*m%mod*N[rt].a[1]%mod; 98 N[rt].a[3]%=mod; 99 N[rt].a[2]=N[rt].a[2]+m%mod*m%mod*(N[rt].r-N[rt].l+1)%mod+2*m%mod*N[rt].a[1]%mod; 100 N[rt].a[2]%=mod; 101 N[rt].a[1]=N[rt].a[1]+m*(N[rt].r-N[rt].l+1)%mod; 102 N[rt].a[1]%=mod; 103 } 104 return; 105 } 106 pushdown(rt); 107 int mid=(L+R)/2; 108 if(mid>=l1){ 109 update(L,mid,rt<<1,l1,r1,add,mul); 110 } 111 if(mid<r1){ 112 update(mid+1,R,(rt<<1)|1,l1,r1,add,mul); 113 } 114 pushup(rt); 115 } 116 ll query(int L,int R,int rt,int l1,int r1,int p){ 117 N[rt].mu%=mod; 118 N[rt].ad%=mod; 119 N[rt].a[1]%=mod; 120 N[rt].a[2]%=mod; 121 N[rt].a[3]%=mod; 122 if(l1<=L&&r1>=R){ 123 return N[rt].a[p]%mod; 124 } 125 int mid=(L+R)/2; 126 ll ak=0; 127 pushdown(rt); 128 if(mid>=l1){ 129 ak+=query(L,mid,rt<<1,l1,r1,p); 130 } 131 if(mid<r1){ 132 ak+=query(mid+1,R,(rt<<1)|1,l1,r1,p); 133 } 134 135 136 return ak%mod; 137 } 138 int main(){ 139 int n,m; 140 scai(n); 141 scai(m); 142 while(n||m){ 143 build(1,n,1); 144 forp(m){ 145 ll a,b,c,d; 146 scal(a); 147 scal(b); 148 scal(c); 149 scal(d); 150 if(a==1){ 151 update(1,n,1,b,c,d,1); 152 } 153 else if(a==2){ 154 update(1,n,1,b,c,0,d); 155 } 156 else if(a==3){ 157 update(1,n,1,b,c,d,0); 158 } 159 else{ 160 prl(query(1,n,1,b,c,d)); 161 } 162 } 163 scai(n); 164 scai(m); 165 } 166 return 0; 167 }