10分WA求助
  • 板块P5142 区间方差
  • 楼主Nickle
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/10/9 11:28
  • 上次更新2023/11/4 04:17:24
查看原帖
10分WA求助
392707
Nickle楼主2021/10/9 11:28

rt

除了第一个点,剩下都是WA。

救救孩子吧!

#include<bits/stdc++.h>
#define double long double
#define int long long
using namespace std;
const int mod=1e9+7;
const int N=2e5+105;
int n,m,opt,x,y;
int inv(int x){
	if(x==1) return 1;
	if(x<1) return 0;
	return (mod-mod/x)*inv(mod%x)%mod;
}
namespace BIT{
	int d1[N],d2[N];
	inline int lowbit(int x){return x&(-x);}
	void add(int x,int v,int *d){while(x<=n)d[x]+=v,d[x]%=mod,x+=lowbit(x);}
	int query(int x,int *d){int res=0;while(x)res+=d[x],res%=mod,x-=lowbit(x);return res%mod;}
	inline void change(int x,int v,int *d){
		v=v%mod;
		int tmp=(x>1)?(query(x,d)-query(x-1,d))%mod:query(x,d);
		add(x,(v-tmp)%mod,d);
	}
}
using namespace BIT;
namespace work{
	inline void work_one(int x,int y){change(x,y,d1);change(x,y*y,d2);}
	inline int work_two(int x,int y){
		if(x==y) return 0;
		int inve=inv(y-x+1);
		int a1=query(y,d2),a2=query(x-1,d2),b1=query(y,d1),b2=query(x-1,d1);
		int a=(a1-a2)%mod,b=(b1-b2)%mod;
		int f=b*inve%mod;
		int ans=(a-(f<<1)*b)*inve%mod;
		ans=((ans+f*f)%mod+mod)%mod;
		return ans%mod;
	}
}
using namespace work;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
signed main(){
	n=read();m=read();
	for(int i=1;i<=n;i++) {
		int q=read();work_one(i,q);
	}
	while(m--){
		opt=read();
		if(opt==1){
			x=read();y=read();
			work_one(x,y);
		}
		else if(opt==2){
			x=read();y=read();
			printf("%lld\n",work_two(x,y));
		}
	}
	return 0;
}
2021/10/9 11:28
加载中...