Input
第一行给出两个整数N,M。分别表示序列长度和操作个数
接下来一行有N个数,即给定的序列a1,a2,….an
接下来M行,每行对应一个操作,格式见题目描述
Output
对于每个询问操作,输出一行,表示所询问的SSi的值。
Sample Input
5 3
1 2 3 4 5
Query 5
Modify 3 2
Query 5
Sample Output
35
32
HINT
1<=N,M<=100000,且在任意时刻0<=Ai<=100000
设两个数组,一个数组res1维护ai, 一个数组res2维护(n-i+1)*ai;
这里是由于
S1 = a1;
S2 = a1 + a2;
S3 = a1 + a2 + a3;
.....
Sn = a1 + a2 + .... + an;
则SSn = ∑(n-i+1)*ai;
求SSt = getSum(res2, t)-(N-t)*getSum(res1, t)
这里记得减去(N-t)*getSum(res1, t)
举例:
例如 求SS2: 我们可以清楚得算出SS2 = 2a1 + a2;
而单独getSum(res2, t)算的却是SS2 = 5a1 + 4a2;
这里比原先的值大了3a1 + 3a2, 是由于我们在维护的时候维护的是(n-i+1)*ai,长度是对应整个数组的长度,而这里是部分长,所以这里我们需要减去多余部分的(a1+a2),这里是多出的部分就是(5-2)*(a1+a2)
= 3a1+3a2,所以我们写代码的时候是写成减去(n-i)*getSum(res1, i);
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define scini(n) scanf("%d", &n)
#define scinl(n) scanf("%lld", &n)
#define scinii(n, m) scanf("%d%d", &n, &m)
#define scinll(n, m) scanf("%lld%lld", &n, &m)
#define scout(n) printf("%d\n", n);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 50010;
using namespace std;
int N, M;
ll in[100100];
ll res1[100100];
ll res2[100100];
ll a, b;
void Modify(ll *S, int i, ll x)
{
while(i <= N){
S[i] += x;
i += lowbit(i);
}
}
ll getSum(ll *S, int i){
ll ret = 0;
while(i > 0){
ret += S[i];
i -= lowbit(i);
}
return ret;
}
void Query(int k)
{
printf("%lld\n", getSum(res2, k)-(N-k)*getSum(res1, k));
}
int main()
{
//fin;
scinii(N, M);
mes(res1, 0);
mes(res2, 0);
for(int i = 1; i <= N; ++i){
scinl(in[i]);
Modify(res1, i, in[i]);
Modify(res2, i, (N-i+1)*in[i]);
}
char op[7];
while(M--){
scanf("%s", op);
if(op[0] == 'M'){
scinll(a, b);
Modify(res1, a, b - in[a]); //要先减取之前加的数,加上新的数
Modify(res2, a, (N-a+1)*(b - in[a]));
in[a] = b;
}
else{
scinl(a);
Query(a);
}
}
return 0;
}