Transformation
Yuanfang is puzzled with the question below:
There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformation a k<---a k+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between a x and a y inclusive. In other words, do transformation a k<---a k×c, k = x,x+1,…,y.
Operation 3: Change the numbers between a x and a y to c, inclusive. In other words, do transformation a k<---c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between a x and a y inclusive. In other words, get the result of a x p+a x+1 p+…+a y 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.
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int NUM=100005;
const int mod=10007;
struct point
{
int l,r,w1,w2,w3,f1,f2,f3;
}tree[NUM*4];
int LL,RR,val,ans;
void build(int k,int LL,int RR)
{
tree[k].l=LL,tree[k].r=RR;
tree[k].f1=tree[k].f3=tree[k].w1=tree[k].w2=tree[k].w3=0;
tree[k].f2=1;
if(tree[k].l==tree[k].r){
return;
}
int bet=(tree[k].l+tree[k].r)/2;
build(k*2,LL,bet);
build(k*2+1,bet+1,RR);
}
int aaa(int a,int b)
{
int bbb=1;
for(int i=0;i<b;i++){
bbb=bbb*a%mod;
}
return bbb;
}
void down(int k)
{
if(tree[k].l==tree[k].r)
return;
if(tree[k].f3){
tree[k*2].f3=tree[k*2+1].f3=tree[k].f3;
tree[k*2].w1=((tree[k*2].r-tree[k*2].l+1)*tree[k].f3)%mod;
tree[k*2+1].w1=((tree[k*2+1].r-tree[k*2+1].l+1)*tree[k].f3)%mod;
tree[k*2].w2=((tree[k*2].r-tree[k*2].l+1)*aaa(tree[k].f3,2))%mod;
tree[k*2+1].w2=((tree[k*2+1].r-tree[k*2+1].l+1)*aaa(tree[k].f3,2))%mod;
tree[k*2].w3=((tree[k*2].r-tree[k*2].l+1)*aaa(tree[k].f3,3))%mod;
tree[k*2+1].w3=((tree[k*2+1].r-tree[k*2+1].l+1)*aaa(tree[k].f3,3))%mod;
tree[k].f3=tree[k*2].f1=tree[k*2+1].f1=0;
tree[k*2].f2=tree[k*2+1].f2=1;
}
if(tree[k].f2!=1){
if(tree[k*2].f3) down(k*2);
if(tree[k*2+1].f3) down(k*2+1);
tree[k*2].f2=(tree[k*2].f2*tree[k].f2)%mod;
tree[k*2+1].f2=(tree[k*2+1].f2*tree[k].f2)%mod;
tree[k*2].w1=(tree[k*2].w1*tree[k].f2)%mod;
tree[k*2+1].w1=(tree[k*2+1].w1*tree[k].f2)%mod;
tree[k*2].w2=(tree[k*2].w2*aaa(tree[k].f2,2))%mod;
tree[k*2+1].w2=(tree[k*2+1].w2*aaa(tree[k].f2,2))%mod;
tree[k*2].w3=(tree[k*2].w3*aaa(tree[k].f2,3))%mod;
tree[k*2+1].w3=(tree[k*2+1].w3*aaa(tree[k].f2,3))%mod;
tree[k*2].f1=(tree[k*2].f1*tree[k].f2)%mod;
tree[k*2+1].f1=(tree[k*2+1].f1*tree[k].f2)%mod;
tree[k].f2=1;
}
if(tree[k].f1){
if(tree[k*2].f3||tree[k*2].f2!=1) down(k*2);
if(tree[k*2+1].f3||tree[k*2+1].f2!=1) down(k*2+1);
tree[k*2].f1=(tree[k*2].f1+tree[k].f1)%mod;
tree[k*2+1].f1=(tree[k*2+1].f1+tree[k].f1)%mod;
tree[k*2].w3=(tree[k*2].w3+3*tree[k*2].w2*tree[k].f1+3*tree[k*2].w1*aaa(tree[k].f1,2)+(tree[k*2].r-tree[k*2].l+1)*aaa(tree[k].f1,3))%mod;
tree[k*2+1].w3=(tree[k*2+1].w3+3*tree[k*2+1].w2*tree[k].f1+3*tree[k*2+1].w1*aaa(tree[k].f1,2)+(tree[k*2+1].r-tree[k*2+1].l+1)*aaa(tree[k].f1,3))%mod;
tree[k*2].w2=(tree[k*2].w2+2*tree[k*2].w1*tree[k].f1+(tree[k*2].r-tree[k*2].l+1)*aaa(tree[k].f1,2))%mod;
tree[k*2+1].w2=(tree[k*2+1].w2+2*tree[k*2+1].w1*tree[k].f1+(tree[k*2+1].r-tree[k*2+1].l+1)*aaa(tree[k].f1,2))%mod;
tree[k*2].w1=((tree[k*2].r-tree[k*2].l+1)*tree[k].f1+tree[k*2].w1)%mod;
tree[k*2+1].w1=((tree[k*2+1].r-tree[k*2+1].l+1)*tree[k].f1+tree[k*2+1].w1)%mod;
tree[k].f1=0;
}
}
void change(int k,int ch)
{
if(LL<=tree[k].l&&RR>=tree[k].r){
down(k);
if(ch==1){
tree[k].f1=(val+tree[k].f1)%mod;
tree[k].w3=(tree[k].w3+3*tree[k].w2*val+3*tree[k].w1*aaa(val,2)+(tree[k].r-tree[k].l+1)*aaa(val,3))%mod;
tree[k].w2=(tree[k].w2+2*tree[k].w1*val+(tree[k].r-tree[k].l+1)*aaa(val,2))%mod;
tree[k].w1=((tree[k].r-tree[k].l+1)*val+tree[k].w1)%mod;
return;
}
if(ch==2){
tree[k].f2=(val*tree[k].f2)%mod;
tree[k].w1=(tree[k].w1*val)%mod;
tree[k].w2=(tree[k].w2*aaa(val,2))%mod;
tree[k].w3=(tree[k].w3*aaa(val,3))%mod;
return;
}
if(ch==3){
tree[k].f3=val;
tree[k].w1=(tree[k].r-tree[k].l+1)*val%mod;
tree[k].w2=(tree[k].r-tree[k].l+1)*aaa(val,2)%mod;
tree[k].w3=(tree[k].r-tree[k].l+1)*aaa(val,3)%mod;
return;
}
}
if(tree[k].f1||tree[k].f2!=1||tree[k].f3) down(k);
int bet=(tree[k].l+tree[k].r)/2;
if(LL<=bet) change(k*2,ch);
if(RR>bet) change(k*2+1,ch);
tree[k].w1=(tree[k*2].w1+tree[k*2+1].w1)%mod;
tree[k].w2=(tree[k*2].w2+tree[k*2+1].w2)%mod;
tree[k].w3=(tree[k*2].w3+tree[k*2+1].w3)%mod;
}
void ask(int k,int ch)
{
if(LL<=tree[k].l&&RR>=tree[k].r){
if(ch==1)
ans=(tree[k].w1+ans)%mod;
if(ch==2)
ans=(tree[k].w2+ans)%mod;
if(ch==3)
ans=(tree[k].w3+ans)%mod;
return;
}
if(tree[k].f1||tree[k].f2!=1||tree[k].f3) down(k);
int bet=(tree[k].l+tree[k].r)/2;
if(LL<=bet) ask(k*2,ch);
if(RR>bet) ask(k*2+1,ch);
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=-1){
if(n==0&&m==0)
break;
build(1,1,n);
int ch;
for(int i=0;i<m;i++){
scanf("%d %d %d %d",&ch,&LL,&RR,&val);
if(ch==4){
ans=0;
ask(1,val);
printf("%d\n",ans);
}
else{
change(1,ch);
}
}
}
return 0;
}
段元芳面临一个包含四种操作的数列难题:加法、乘法、赋值和幂次求和。通过区间操作更新数列,并求解特定区间的幂次和,所有计算需取模。挑战在于高效处理大量操作,涉及数据结构和算法优化。
1044

被折叠的 条评论
为什么被折叠?



