void add(int x, int k) {
while (x <= n) { // 不能越界
c[x] = c[x] + k;
x = x + lowbit(x);
}
}
2.区间查询
int getsum(int x) { // a[1]..a[x]的和
int ans = 0;
while (x > 0) {
ans = ans + c[x];
x = x - lowbit(x);
}
return ans;
}
3.初始建立树状数组
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m;
int a[N], c[N];
void add(int x, int k) {
while (x <= n) {
c[x] += k;
x = x + lowbit(x);
}
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(i, a[i]);
}
}
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int n, m;
int a[N], c[N];
int lowbit(int x) { return x & -x; }
void add(int x, int k) {
while (x <= n) {
c[x] += k;
x = x + lowbit(x);
}
}
int getsum(int x) {
int ans = 0;
while (x > 0) {
ans = ans + c[x];
x -= lowbit(x);
}
return ans;
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(i, a[i]);
}
while (m--) {
int a, b, c;
cin >> a >> b >> c;
if (a == 1) {
add(b, c);
} else {
cout << getsum(c) - getsum(b - 1) << endl;
}
}
}
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int n, m;
int a[N], c[N];
int lowbit(int x) { return x & -x; }
void add(int x, int k) {
while (x <= n) {
c[x] += k;
x = x + lowbit(x);
}
}
int getsum(int x) {
int ans = 0;
while (x > 0) {
ans = ans + c[x];
x -= lowbit(x);
}
return ans;
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(i, a[i] - a[i - 1]);
}
while (m--) {
int a, b, c, d;
cin >> a >> b;
if (a == 1) {
cin >> c >> d;
add(b, d);
add(c + 1, -1 * d);
} else {
cout << getsum(b) << endl;
}
}
}