0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 13:58:48 +00:00

P3368 【模板】树状数组 2

https://www.luogu.com.cn/record/94813602
This commit is contained in:
Baoshuo Ren 2022-11-20 16:52:14 +08:00
parent 0247fe4aec
commit ccc6262369
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,91 +1,60 @@
#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;
int d;
long long s;
node() { int n, m, c[N];
l = r = 0;
d = 0;
s = 0;
}
node(int _l, int _r) { int lowbit(int x) {
l = _l; return x & -x;
r = _r;
s = 0;
d = 0;
}
} tr[500005 << 2];
int n, m, op, x, y, k, a[500005];
void pushup(int u) {
tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s;
} }
void pushdown(int u) { void add(int x, int y) {
tr[u << 1].d += tr[u].d; for (; x <= n; x += lowbit(x)) c[x] += y;
tr[u << 1].s += (tr[u << 1].r - tr[u << 1].l + 1) * tr[u].d;
tr[u << 1 | 1].d += tr[u].d;
tr[u << 1 | 1].s += (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1) * tr[u].d;
tr[u].d = 0;
} }
void build(int u, int l, int r) { int sum(int x) {
tr[u] = node(l, r); int res = 0;
if (l == r) { for (; x; x -= lowbit(x)) res += c[x];
tr[u].s = a[l]; return res;
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
long long 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;
long long s = 0;
pushdown(u);
if (l <= mid) s += query(u << 1, l, r);
if (r > mid) s += query(u << 1 | 1, l, r);
return s;
}
void modify(int u, int l, int r, int d) {
if (tr[u].l >= l && tr[u].r <= r) {
tr[u].d += d;
tr[u].s += (tr[u].r - tr[u].l + 1) * d;
return;
}
int mid = tr[u].l + tr[u].r >> 1;
pushdown(u);
if (l <= mid) modify(u << 1, l, r, d);
if (r > mid) modify(u << 1 | 1, l, r, d);
pushup(u);
} }
int main() { int main() {
scanf("%d%d", &n, &m); std::ios::sync_with_stdio(false);
for (int i = 1; i <= n; i++) { cin.tie(nullptr);
scanf("%d", &a[i]);
cin >> n >> m;
for (int i = 1, x; i <= n; i++) {
cin >> x;
add(i, x);
add(i + 1, -x);
} }
build(1, 1, n);
while (m--) { while (m--) {
scanf("%d%d", &op, &x); int op;
cin >> op;
if (op == 1) { if (op == 1) {
scanf("%d%d", &y, &k); int x, y, k;
modify(1, x, y, k);
} else if (op == 2) { cin >> x >> y >> k;
printf("%d\n", query(1, x, x));
add(x, k);
add(y + 1, -k);
} else { // op == 2
int x;
cin >> x;
cout << sum(x) << endl;
} }
} }
return 0; return 0;
} }