10pts AC #1 求条
查看原帖
10pts AC #1 求条
1208546
AnotherDream楼主2025/1/16 10:56
#include <bits/stdc++.h>
using namespace std;
#define debug cerr<<"The code runs successfully.\n";
#define endl '\n'
#define TRACE 1
#define tcout TRACE && cout
#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define mid ((l+r)>>1)
#define ls(x) (x<<1)
#define rs(x) ((x<<1)+1)
const int P = 1e9 + 7; 
const int Base = 131;
const int INF = 0x3f3f3f3f3f3f3f3f; 
const int N = 1e5 + 10, M = 2e6 + 10;
int a[N],s1[N<<2],s2[N<<2],t[N<<2];
int n,q;
inline int fpm(int a,int n) {
	int res=1;
	while(n) {
		if(n&1) {
			res=res*a%P;
		}
		n>>=1;
		a=a*a%P;
	}
	return res;
}
inline void calc(int p,int l,int r,int x) {
	t[p]+=x%P;
	t[p]%=P;
	s2[p]+=2*x*s1[p]+x*x*(r-l+1)%P;
	s2[p]%=P;
	s1[p]+=(r-l+1)*x%P;
	s1[p]%=P;
}
inline void pushup(int x) {
	s1[x]=s1[ls(x)]+s1[rs(x)]%P;
	s1[x]%=P;
	s2[x]=s2[ls(x)]+s2[rs(x)]%P;
	s2[x]%=P;
}
inline void pushdown(int p,int l,int r) {
	if(!t[p]) return;
	calc(ls(p),l,mid,t[p]);
	calc(rs(p),mid+1,r,t[p]);
	t[p]=0;
}
inline void build(int p,int l,int r) {
	if(l==r) {
		s1[p]=a[l]%P;
		s2[p]=a[l]*a[l]%P;
		return;
	}
	build(ls(p),l,mid);
	build(rs(p),mid+1,r);
	pushup(p);
	
}
inline void add(int p,int l,int r,int L,int R,int x) {
	if(r<L||R<l) return;
	if(L<=l&&r<=R) {
		calc(p,l,r,x);
		return;
	}
	pushdown(p,l,r);
	add(ls(p),l,mid,L,R,x);
	add(rs(p),mid+1,r,L,R,x);
	pushup(p);
}
inline int ask1(int p,int l,int r,int L,int R) {
	if(r<L||R<l) return 0;
	if(L<=l&&r<=R) {
		return s1[p]%P;
	}
	pushdown(p,l,r);
	return (ask1(ls(p),l,mid,L,R)%P+ask1(rs(p),mid+1,r,L,R)%P)%P;
	
}
inline int ask2(int p,int l,int r,int L,int R) {
	if(r<L||R<l) return 0;
	if(L<=l&&r<=R) {
		return s2[p]%P;
	}
	pushdown(p,l,r);
	return (ask2(ls(p),l,mid,L,R)%P+ask2(rs(p),mid+1,r,L,R)%P)%P;
	
}
signed main() {
	fst;
	cin>>n>>q;
	for(int i=1;i<=n;i++) {
		cin>>a[i];
		a[i]%=P;
	}
	build(1,1,n);
	while(q--) {
		int op,x,y;
		cin>>op>>x>>y;
		if(op==1) {
			y%=P;
			add(1,1,n,x,x,y-a[x]);
			a[x]=y;
		}
		else {
			int avg=ask1(1,1,n,x,y)%P*fpm(y-x+1,P-2)%P;
			cout<<(-avg*avg%P+ask2(1,1,n,x,y)%P*fpm(y-x+1,P-2)%P)%P<<endl;
		}
	}
	return 0;
}
2025/1/16 10:56
加载中...