- 树状数组1:单点修改,区间查询
1
- @ 2026-7-23 10:18:19
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
struct Fenwick {
vector<ll> t;
int n;
Fenwick(int n_) {
n = n_;
t.assign(n + 1, 0);
}
void change(int x, ll y) {
for (; x <= n; x += x & -x) {
t[x] += y;
}
}
ll sum(int u) {
ll res = 0;
for (; u; u -= u & -u) {
res += t[u];
}
return res;
}
ll sum(int l, int r) {
return sum(r) - sum(l - 1);
}
};
void solve() {
int n, m;
cin >> n >> m;
Fenwick t(n);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
t.change(i, x);
}
while (m--) {
int o, x, y;
cin >> o >> x >> y;
if (o == 1) {
t.change(x, y);
} else {
cout << t.sum(x, y) << '\n';
}
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
// cout << powl(1 - 1 / 1e9, 3e7) << '\n';
int t = 1;
while (t--) solve();
return 0;
}
/*
g++ -std=c++20 1.cpp -o 1 && 1 < in.txt > out.txt
*/
0 条评论
目前还没有评论...
信息
- ID
- 502
- 时间
- ms
- 内存
- MiB
- 难度
- 3
- 标签
- 递交数
- 117
- 已通过
- 28
- 上传者