Aimee
首先可以知道对于任意一个\(a_i\),我们可以知道他的贡献在\([i,n]\)
那么对于每一次对于\(ss_k\)的查询,贡献是\((k-i+1)*a_i\)
分配一下,贡献是\(a_i*(k+1)+a_i\),分别计算这两个就可以了
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define int long long using namespace std; int tree[3][1000001]; int a[1000001]; int n,m; string s; int lowbit(int x){ return x&(-x); } int add(int i,int x,int f){ while(i<=n){ tree[f][i]+=x; i+=lowbit(i); } } int find(int x,int f){ int ans=0; while(x){ ans+=tree[f][x]; x-=lowbit(x); } return ans; } int tem; int x,y; int ans; signed main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;++i){ scanf("%d",&a[i]); add(i,a[i],1); add(i,a[i]*i,2); } for(int i=1;i<=m;++i){ cin>>s; if(s=="Query"){ scanf("%d",&tem); cout<<(tem+1)*(find(tem,1))-find(tem,2)<<endl; }else{ scanf("%d%d",&x,&y); add(x,y-a[x],1); add(x,(y-a[x])*x,2); a[x]=y; } } return 0; }