3155: Preprefix sum
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155
把给出的a_i当成查分数组d_i做就可以了。
XJB搞一搞就过了。
code:
#include <iostream>
#include <cstdio>
using namespace std;
#define int long long
const int wx=200017;
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
int n,m;
int sum2[wx];
int d[wx],sum1[wx],a[wx];
char opt[wx];
void add(int pos,int k){
for(int i=pos;i<=n;i+=(i&-i)){
sum1[i]+=k;
sum2[i]+=k*pos;
}
}
int query(int pos){
int re=0;
for(int i=pos;i>=1;i-=(i&-i)){
re+=(pos+1)*sum1[i]-sum2[i];
}
return re;
}
int query_wx(int l,int r){
return query(r)-query(l-1);
}
signed main(){
n=read(); m=read();
for(int i=1;i<=n;i++){
d[i]=read();
a[i]=a[i-1]+d[i];
add(i,a[i]); add(i+1,-a[i]);
}
for(int i=1;i<=m;i++){
scanf("%s",opt+1);
if(opt[1]=='Q'){
int x; x=read();
printf("%lld
",query_wx(1,x));
}
else{
int x,y;
x=read(); y=read();
int tmp=y-d[x];
d[x]=y; add(x,tmp); //add(x+1,-tmp);
}
}
return 0;
}