- 树状数组1:单点修改,区间查询
code
- @ 2026-2-8 10:17:54
struct Fenwick {
vector<ll> t;
int n;
Fenwick(int n_) {
n = n_;
t.assign(n + 1, 0);
}
void change(int x, int y) {
for (; x <= n; x += x & -x) {
t[x] += y;
}
}
ll query(int x) {
ll res = 0;
for (; x > 0; x -= x & -x) {
res += t[x];
}
return res;
}
ll query(int l, int r) {
return query(r) - query(l - 1);
}
};
void solve() {
int n, q;
cin >> n >> q;
Fenwick t(n);
for (int i = 1; i <= n; i++) {
int x; cin >> x;
t.change(i, x);
}
while (q--) {
int o, x, y;
cin >> o >> x >> y;
if (o == 1) {
t.change(x, y);
} else {
cout << t.query(x, y) << '\n';
}
}
}
0 条评论
目前还没有评论...
信息
- ID
- 429
- 时间
- ms
- 内存
- MiB
- 难度
- 3
- 标签
- 递交数
- 53
- 已通过
- 15
- 上传者