题意
1<=N,M<=100000,且在任意时刻0<=Ai<=100000
分析
唉果然像我这种弱鸡就只能切切这种大家都会的sb题了。
把答案的式子展开后用树状数组维护一下对应项就好了。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=100005;
int n,m,a[N];
LL c1[N],c2[N];
int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void ins1(int x,LL y)
{
while (x<=n) c1[x]+=y,x+=x&(-x);
}
void ins2(int x,LL y)
{
while (x<=n) c2[x]+=y,x+=x&(-x);
}
LL query1(int x)
{
LL ans=0;
while (x) ans+=c1[x],x-=x&(-x);
return ans;
}
LL query2(int x)
{
LL ans=0;
while (x) ans+=c2[x],x-=x&(-x);
return ans;
}
int main()
{
n=read();m=read();
for (int i=1;i<=n;i++) a[i]=read(),ins1(i,a[i]),ins2(i,(LL)a[i]*i);
while (m--)
{
char ch[10];
scanf("%s",ch);
if (ch[0]=='M')
{
int x=read(),y=read();
ins1(x,-a[x]);ins2(x,(LL)-a[x]*x);
a[x]=y;
ins1(x,a[x]);ins2(x,(LL)a[x]*x);
}
else
{
int x=read();
LL s1=query1(x),s2=query2(x);
printf("%lld\n",(LL)s1*(x+1)-s2);
}
}
return 0;
}