0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 14:56:27 +00:00

P3374 【模板】树状数组 1

R73698570
This commit is contained in:
Baoshuo Ren 2022-04-10 21:35:23 +08:00
parent 9b3f4d7978
commit f311f6f98d
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -1,75 +1,46 @@
#include <bits/stdc++.h> #include <iostream>
using namespace std; using std::cin;
using std::cout;
const char endl = '\n';
struct node { const int N = 5e5 + 5;
int l, r, s;
node() { int n, m, op, x, y;
l = r = s = 0; int c[N];
}
node(int _l, int _r) {
l = _l;
r = _r;
s = 0;
}
} tr[500005 << 2];
int n, m, op, x, y, a[500005];
void pushup(int u) { inline int lowbit(int x) {
tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s; return x & -x;
} }
void build(int u, int l, int r) { void add(int x, int y) {
tr[u] = node(l, r); for (; x <= n; x += lowbit(x)) c[x] += y;
if (l == r) {
tr[u].s = a[l];
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
} }
void change(int u, int x, int d) { int sum(int x) {
if (tr[u].l == tr[u].r) { int ans = 0;
tr[u].s += d; for (; x; x -= lowbit(x)) ans += c[x];
return; return ans;
}
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) {
change(u << 1, x, d);
} else {
change(u << 1 | 1, x, d);
}
pushup(u);
}
int query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) {
return tr[u].s;
}
int mid = tr[u].l + tr[u].r >> 1;
int s = 0;
if (l <= mid) s += query(u << 1, l, r);
if (r > mid) s += query(u << 1 | 1, l, r);
return s;
} }
int main() { int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m; cin >> n >> m;
for (int i = 1; i <= n; i++) { for (int i = 1, x; i <= n; i++) {
cin >> a[i]; cin >> x;
add(i, x);
} }
build(1, 1, n);
while (m--) { while (m--) {
cin >> op >> x >> y; cin >> op >> x >> y;
if (op == 1) { if (op == 1) {
change(1, x, y); add(x, y);
} else if (op == 2) { } else { // op == 2
cout << query(1, x, y) << endl; cout << sum(y) - sum(x - 1) << endl;
} }
} }
return 0; return 0;
} }