设 代表一维前缀和, 代表二维前缀和。
这个时候问题变成了区间维护二维前缀和。
修改的柿子不难,懒得敲了,贴个题解的图。
到 的部分的意义就是,从 到 的所有点对都会让当前点的二维前缀和加 , 到 的那一坨柿子前面的意义不变,后面的代表 到 中的每个点都会让二维前缀和加上区间增加量。
没卡常,跑的还挺快。
Code
#include<bits/stdc++.h>
#define int long long
namespace fast_IO{
#define IOSIZE 100000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
}using namespace fast_IO;
using namespace std;
const int N=5e5+10,mod=1e9+7,inv2=5e8+4;
int n,m,a[N],s[N],ss[N];
struct tree{
int l,r,sum,sumi,sumi2,lazy1,lazy2,add;
#define lson(u) (u<<1)
#define rson(u) (u<<1|1)
}t[4*N];
inline int add(int a,int b){return ((a+=b)>mod)?a-mod:a;}
inline int nor(int a,int b){return ((a-=b)<0)?a+mod:a;}
inline void push_up(int u){t[u].sum=add(t[lson(u)].sum,t[rson(u)].sum);}
inline void work1(int u,int val){
t[u].lazy1=add(t[u].lazy1,val);
t[u].sum=add(t[u].sum,t[u].sumi2*val%mod);
}
inline void work2(int u,int val){
t[u].lazy2=add(t[u].lazy2,val);
t[u].sum=add(t[u].sum,t[u].sumi*val%mod);
}
inline void work3(int u,int val){
t[u].add=add(t[u].add,val);
t[u].sum=add(t[u].sum,(t[u].r-t[u].l+1)*val%mod);
}
inline void push_down(int u){
work1(lson(u),t[u].lazy1);
work1(rson(u),t[u].lazy1);
work2(lson(u),t[u].lazy2);
work2(rson(u),t[u].lazy2);
work3(lson(u),t[u].add);
work3(rson(u),t[u].add);
t[u].lazy1=t[u].lazy2=t[u].add=0;
}
inline void build(int u,int l,int r){
t[u].l=l;
t[u].r=r;
if(l==r){
t[u].sum=ss[l];
t[u].sumi=l;
t[u].sumi2=l*l%mod;
return;
}
int mid=(t[u].l+t[u].r)>>1;
build(lson(u),l,mid);
build(rson(u),mid+1,r);
push_up(u);
t[u].sumi=add(t[lson(u)].sumi,t[rson(u)].sumi);
t[u].sumi2=add(t[lson(u)].sumi2,t[rson(u)].sumi2);
}
inline void update(int u,int l,int r,int delta1,int delta2,int delta3){
if(l<=t[u].l&&t[u].r<=r){
work1(u,delta1);
work2(u,delta2);
work3(u,delta3);
return;
}
push_down(u);
int mid=(t[u].l+t[u].r)>>1;
if(l<=mid)update(lson(u),l,r,delta1,delta2,delta3);
if(r>mid)update(rson(u),l,r,delta1,delta2,delta3);
push_up(u);
}
inline int query(int u,int l,int r){
if(l<=t[u].l&&t[u].r<=r)return t[u].sum;
push_down(u);
int mid=(t[u].l+t[u].r)>>1,val=0;
if(l<=mid)val=query(lson(u),l,r);
if(r>mid)val=add(val,query(rson(u),l,r));
return val;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;++i){
cin>>a[i];
s[i]=(s[i-1]+a[i])%mod;
ss[i]=(ss[i-1]+s[i])%mod;
}
build(1,1,n);
int opt,l,r,delta;
while(m--){
cin>>opt;
if(opt==1){
cin>>l>>r>>delta;
if(l>r)swap(l,r);
update(1,l,r,inv2*delta%mod,((3-2*l)*inv2%mod+mod)%mod*delta%mod,((l*l%mod-3*l+2)%mod+mod)%mod*inv2%mod*delta%mod);
if(r<n)update(1,r+1,n,0,(r-l+1)*delta%mod,(((r-l+1)*(r-l+2)%mod*inv2%mod+(-r*r%mod+l*r%mod-r))%mod+mod)%mod*delta%mod);
}
else{
cin>>l>>r;
int res=query(1,n,n)*(r-l+1)%mod;
if(r!=1)res=nor(res,query(1,max(1ll,l-1),r-1));
if(l!=n)res=nor(res,query(1,max(1ll,n-r),n-l));
cout<<(res%mod+mod)%mod<<"\n";
}
}
return 0;
}