https://www.luogu.org/problemnew/show/P4868
题目大意
单点修改,查询前缀前缀和。
分析
遇到了单点修改,前缀和,很明显是要树状数组维护解决问题。
请看以下我的数列的转换
\[s1+s2+s3+...+sn\]
\[a1+a1+a2+a1+a2+a3+...+an\]
\[a1*n+a2*(n-1)+a3*(n-2)+...an*1\]
\[(a1+a2+a3+...+an)*N - (a2+a3^2+a4^3+...+an^{n-1})\]
ac代码
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define N 100005
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
struct bittree {
#define lowbit(x) (x&(-x))
ll tr[N];
int n;
void add(int k, ll val) {
for (int i = k; i <= n; i += lowbit(i))
tr[i] += val;
}
ll query(int x) {
ll res = 0;
for (int i = x; i; i -= lowbit(i))
res += tr[i];
return res;
}
}tr1, tr2;
int n, m;
ll a[N];
int main() {
read(n); read(m);
tr1.n = tr2.n = n;
for (int i = 1; i <= n; i ++) {
read(a[i]);
tr1.add(i, a[i]);
tr2.add(i, a[i] * (i - 1));
}
while (m --) {
char opt[10];
scanf("%s", opt);
ll x, y;
if (opt[0] == 'Q') {
read(x);
printf("%lld\n", (ll)(tr1.query(x) * x) - 1ll * tr2.query(x));
}
else {
read(x); read(y);
tr1.add(x, y - a[x]);
tr2.add(x, (y - a[x]) * (x - 1));
a[x] = y;
}
}
return 0;
}