ACM-ICPC 2018 徐州賽區網路預賽 H題
阿新 • • 發佈:2018-12-24
思路
區間詢問,單點修改,用樹狀陣列,維護區間和資訊。
AC程式碼
#include<cstdio>
#include<vector>
using namespace std;
inline int lowbit(int x) { return x&-x; }
struct FenwickTree {
int n;
vector<long long> C;
void resize(int n) { this->n = n; C.resize(n); }
void clear() { fill(C.begin(), C.end(), 0 ); }
// 計算A[1]+A[2]+...+A[x] (x<=n)
long long sum(int x) {
long long ret = 0;
while(x > 0) {
ret += C[x]; x -= lowbit(x);
}
return ret;
}
// A[x] += d (1<=x<=n)
void add(int x, long long d) {
while(x <= n) {
C[x] += d; x += lowbit(x);
}
}
long long get(int l, int r) {
return sum(r)-sum(l-1);
}
};
const int maxn = 200000 + 5;
int a[maxn];
FenwickTree f, p;
int main() {
int n, q;
while(scanf("%d%d", &n, &q) == 2) {
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
f.resize(n);
f.clear();
p.resize(n);
p.clear();
for (int i = 1; i <= n; i++) {
f.add(i, a[i]);
p.add(i, 1LL*a[i]*i);
}
int cmd;
for(int i = 0; i < q; i++) {
int x, y;
scanf("%d%d%d", &cmd, &x, &y);
if(cmd == 1) {
long long ans = 1LL * (y+1) * f.get(x, y) - 1LL * p.get(x, y);
//long long ans = f.get(x, y);
printf("%lld\n", ans);
} else {
f.add(x, y - a[x]);
p.add(x, 1LL*y*x - 1LL*a[x]*x);
a[x] = y;
}
}
}
return 0;
}
如有不當之處歡迎指出!