数列操作a(单点修改,区间查询)
树状数组:
#include <bits/stdc++.h>
#define lowbit(x) ((x) & (-(x)))
using namespace std;
const int N = 5e5 + 5;
int c[N], n, m;
inline void update(int p, int val) {
for (; p <= n; p += lowbit(p)) c[p] += val;
}
inline int query(int p) {
int ans = 0;
for (; p >= 1; p -= lowbit(p)) ans += c[p];
return ans;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
update(i, x);
}
while (m--) {
int op; cin >> op;
if (op == 1) {
int x, y; cin >> x >> y;
update(x, y);
} else {
int x, y; cin >> x >> y;
cout << query(y) - query(x - 1) << '\n';
}
}
return 0;
}
线段树:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int a[N], n, m;
struct node {
#define ls p << 1
#define rs p << 1 | 1
int l, r;
int val;
} tree[N << 2];
void build(int p, int l, int r) {
tree[p].l = l;
tree[p].r = r;
if (l == r) return;
int mid = (r + l) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
}
int add(int p) {
if (tree[p].l == tree[p].r) {
tree[p].val = a[tree[p].r];
return tree[p].val;
}
tree[p].val = add(ls) + add(rs);
return tree[p].val;
}
void update(int p, int dis, int k) {
tree[p].val += k;
if (tree[p].l == tree[p].r) return;
if (dis <= tree[ls].r) update(ls, dis, k);
if (dis >= tree[rs].l) update(rs, dis, k);
return;
}
int query(int p, int l, int r) {
if (tree[p].l >= l && tree[p].r <= r) return tree[p].val;
int ans = 0;
if (tree[ls].r >= l) ans += query(ls, l, r);
if (tree[rs].l <= r) ans += query(rs, l, r);
return ans;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
build(1, 1, n);
add(1);
while (m--) {
int op; cin >> op;
if (op == 1) {
int x, y; cin >> x >> y;
update(1, x, y);
} else {
int x, y; cin >> x >> y;
cout << query(1, x, y) << '\n';
}
}
return 0;
}
数列操作b(区间修改,单点查询)
树状数组:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-(x)))
using namespace std;
const int N = 5e5 + 5;
int c[N], n, m;
inline void update(int p, int val) {
for (; p <= n; p += lowbit(p)) c[p] += val;
}
inline int query(int p) {
int ans = 0;
for (; p >= 1; p -= lowbit(p)) ans += c[p];
return ans;
}
int main() {
cin >> n >> m;
for (int i = 1, tmp = 0; i <= n; i++) {
int x; cin >> x;
update(i, x - tmp);
tmp = x;
}
while (m--) {
int op; cin >> op;
if (op == 1) {
int l, r, k; cin >> l >> r >> k;
update(l, k);
update(r + 1, -k);
} else {
int x; cin >> x;
cout << query(x) << '\n';
}
}
return 0;
}
线段树:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int a[N], n, m;
struct node {
#define ls p << 1
#define rs p << 1 | 1
int l, r;
int val;
} tree[N << 2];
void build(int p, int l, int r) {
tree[p].l = l;
tree[p].r = r;
if (l == r) return;
int mid = (r + l) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
}
void update(int p, int l, int r, int k) {
if (tree[p].l >= l && tree[p].r <= r) {
tree[p].val += k;
return;
}
if (tree[ls].r >= l) update(ls, l, r, k);
if (tree[rs].l <= r) update(rs, l, r, k);
}
int query(int p, int dis) {
if (tree[p].l == tree[p].r) return tree[p].val;
int ans = tree[p].val;
if (dis <= tree[ls].r) ans += query(ls, dis);
if (dis >= tree[rs].l) ans += query(rs, dis);
return ans;
}
int main() {
cin >> n >> m;
build(1, 1, n);
for (int i = 1; i <= n; i++)
cin >> a[i];
while (m--) {
int op; cin >> op;
if (op == 1) {
int l, r, k; cin >> l >> r >> k;
update(1, l, r, k);
} else {
int x; cin >> x;
cout << query(1, x) + a[x] << '\n';
}
}
return 0;
}
数列操作c(区间修改,区间查询)
树状数组:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-(x)))
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
ll c1[N], c2[N];
int n, m;
void update(int p, ll val) {
for (int P = p; p <= n; p += lowbit(p)) {
c1[p] += val;
c2[p] += P * val;
}
}
ll query(int p) {
ll ans = 0;
for (int P = p; p >= 1; p -= lowbit(p)) {
ans += (P + 1) * c1[p] - c2[p];
}
return ans;
}
int main() {
cin >> n >> m;
for (int i = 1, tmp = 0; i <= n; i++) {
ll x; cin >> x;
update(i, x - tmp);
tmp = x;
}
while (m--) {
int op; cin >> op;
if (op == 1) {
int l, r; ll k; cin >> l >> r >> k;
update(l, k);
update(r + 1, -k);
} else {
int l, r; cin >> l >> r;
cout << query(r) - query(l - 1) << '\n';
}
}
return 0;
}
线段树:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
using ll = long long;
ll a[N];
int n, m;
struct node {
#define ls p << 1
#define rs p << 1 | 1
int l, r;
ll val, lt;
} tree[N << 2];
void build(int p, int l, int r) {
tree[p].l = l;
tree[p].r = r;
if (l == r) {
tree[p].val = a[l];
return;
}
int mid = (l + r) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
tree[p].val = tree[ls].val + tree[rs].val;
}
void pushdown(int p) {
if (tree[p].lt == 0) return;
tree[ls].lt += tree[p].lt;
tree[rs].lt += tree[p].lt;
int mid = (tree[p].l + tree[p].r) >> 1;
tree[ls].val += tree[p].lt * (mid - tree[ls].l + 1);
tree[rs].val += tree[p].lt * (tree[rs].r - mid);
tree[p].lt = 0;
}
void update(int p, int l, int r, ll k) {
if (tree[p].r <= r && tree[p].l >= l) {
tree[p].val += k * (tree[p].r - tree[p].l + 1);
tree[p].lt += k;
return;
}
pushdown(p);
if (tree[ls].r >= l) update(ls, l, r, k);
if (tree[rs].l <= r) update(rs, l, r, k);
tree[p].val = tree[ls].val + tree[rs].val;
}
ll query(int p, int l, int r) {
if (tree[p].l >= l && tree[p].r <= r)
return tree[p].val;
pushdown(p);
ll ans = 0;
if (tree[ls].r >= l) ans += query(ls, l, r);
if (tree[rs].l <= r) ans += query(rs, l, r);
return ans;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
build(1, 1, n);
while (m--) {
int op; cin >> op;
if (op == 1) {
int l, r; ll k; cin >> l >> r >> k;
update(1, x, y, k);
} else {
int l, r; cin >> l >> r;
cout << query(1, l, r) << '\n';
}
}
return 0;
}
区间加、乘 + 查询区间和
P3373 【模板】线段树 2
线段树:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, P = 571373;
using ll = long long;
int a[N], n, m;
struct node {
#define ls p << 1
#define rs p << 1 | 1
int l, r;
ll val, add, mul;
} tree[N << 2];
inline void build(int p, int l, int r) {
tree[p].l = l;
tree[p].r = r;
tree[p].mul = 1;
if (l == r) {
tree[p].val = a[l] % P;
return;
}
int mid = (l + r) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
tree[p].val = (tree[ls].val + tree[rs].val) % P;
}
inline void pushdown(int p) {
ll k1 = tree[p].mul, k2 = tree[p].add;
tree[ls].val = (tree[ls].val * k1 + k2 * (tree[ls].r - tree[ls].l + 1)) % P;
tree[rs].val = (tree[rs].val * k1 + k2 * (tree[rs].r - tree[rs].l + 1)) % P;
tree[ls].mul = (tree[ls].mul * k1) % P;
tree[rs].mul = (tree[rs].mul * k1) % P;
tree[ls].add = (tree[ls].add * k1 + k2) % P;
tree[rs].add = (tree[rs].add * k1 + k2) % P;
tree[p].add = 0;
tree[p].mul = 1;
}
inline void mul(int p, int l, int r, int k) {
if (tree[p].r < l || tree[p].l > r)
return;
if (tree[p].l >= l && tree[p].r <= r) {
tree[p].val = (tree[p].val * k) % P;
tree[p].mul = (tree[p].mul * k) % P;
tree[p].add = (tree[p].add * k) % P;
return;
}
pushdown(p);
if (tree[ls].r >= l) mul(ls, l, r, k);
if (tree[rs].l <= r) mul(rs, l, r, k);
tree[p].val = (tree[ls].val + tree[rs].val) % P;
}
inline void add(int p, int l, int r, int k) {
if (tree[p].r < l || tree[p].l > r)
return;
if (tree[p].l >= l && tree[p].r <= r) {
tree[p].val += ((tree[p].r - tree[p].l + 1) * k) % P;
tree[p].add = (tree[p].add + k) % P;
return;
}
pushdown(p);
if (tree[ls].r >= l) add(ls, l, r, k);
if (tree[rs].l <= r) add(rs, l, r, k);
tree[p].val = (tree[ls].val + tree[rs].val) % P;
}
inline int query(int p, int l, int r) {
if (tree[p].r < l || tree[p].l > r)
return 0;
if (tree[p].l >= l && tree[p].r <= r)
return tree[p].val;
pushdown(p);
ll val = 0;
if (tree[ls].r >= l) val += query(ls, l, r) % P;
if (tree[rs].l <= r) val += query(rs, l, r) % P;
return val % P;
}
int main() {
int _; cin >> n >> m >> _;
for (int i = 1; i <= n; i++)
cin >> a[i];
build(1, 1, n);
while (m--) {
int op; cin >> op;
if (op == 1) {
int l, r, k; cin >> l >> r >> k;
k %= P;
mul(1, l, r, k);
}
if (op == 2) {
int l, r, k; cin >> l >> r >> k;
k %= P;
add(1, l, r, k);
}
if (op == 3) {
int l, r; cin >> l >> r;
cout << query(1, l, r) << '\n';
}
}
return 0;
}