#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 50010;
int tree[N * 4];
int n;
int a[N];
int casei;
void build_tree(int node, int l, int r) {
if (l >= r) {
tree[node] = a[l];
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
build_tree(left_node, l, mid), build_tree(right_node, mid + 1, r);
tree[node] = tree[left_node] + tree[right_node];
}
void update_tree(int node, int l, int r, int idx, int val) {
if (l >= r) {
a[idx] += val;
tree[node] += val;
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
if (idx >= l && idx <= mid)
update_tree(left_node, l, mid, idx, val);
else
update_tree(right_node, mid + 1, r, idx, val);
tree[node] = tree[left_node] + tree[right_node];
}
int query_tree(int node, int l, int r, int start, int end) {
if (l > end || r < start)
return 0;
else if (l >= start && r <= end)
return tree[node];
else if (l >= r)
return tree[node];
else {
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
return query_tree(left_node, l, mid, start, end) + query_tree(right_node, mid + 1, r, start, end);
}
}
int main() {
int T;
scanf("%d", &T);
while (T -- ) {
printf("Case %d:\n", ++ casei);
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
scanf("%d", &a[i]);
build_tree(1, 1, n);
char op[10];
while (~scanf("%s", op)) {
if (op[0] == 'E')
break;
int a, b;
scanf("%d%d", &a, &b);
if (op[0] == 'A')
update_tree(1, 1, n, a, b);
else if (op[0] == 'S')
update_tree(1, 1, n, a, -b);
else
printf("%d\n", query_tree(1, 1, n, a, b));
}
}
return 0;
}
线段树
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int tree[N * 4];
int n;
void build_tree(int node, int l, int r) {
if (l >= r) {
tree[node] = 0;
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
build_tree(left_node, l, mid), build_tree(right_node, mid + 1, r);
}
void update_tree(int node, int l, int r, int start, int end) {
if (l == start && r == end) {
tree[node] ++ ;
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
if (end <= mid)
update_tree(left_node, l, mid, start, end);
else if (start > mid)
update_tree(right_node, mid + 1, r, start, end);
else {
update_tree(left_node, l, mid, start, mid);
update_tree(right_node, mid + 1, r, mid + 1, end);
}
}
void query_tree(int node, int l, int r) {
if (l >= r) {
if (l != 1)
printf(" ");
printf("%d", tree[node]);
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
if (tree[node]) {
tree[left_node] += tree[node];
tree[right_node] += tree[node];
tree[node] = 0;
}
query_tree(left_node, l, mid), query_tree(right_node, mid + 1, r);
}
int main() {
while (~scanf("%d", &n)) {
if (n == 0)
break;
memset(tree, 0, sizeof tree);
build_tree(1, 1, n);
for (int i = 1; i <= n; i ++ ) {
int a, b;
scanf("%d%d", &a, &b);
update_tree(1, 1, n, a, b);
}
query_tree(1, 1, n);
puts("");
}
return 0;
}
树状数组
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int t[N];
int n;
int lowbit(int x) {
return x & -x;
}
void add(int x, int k) {
for ( ; x <= N; x += lowbit(x))
t[x] += k;
}
int ask(int x) {
int res = 0;
for ( ; x; x -= lowbit(x))
res += t[x];
return res;
}
int main() {
while (~scanf("%d", &n)) {
if (n == 0)
break;
memset(t, 0, sizeof t);
for (int i = 1; i <= n; i ++ ) {
int a, b;
scanf("%d%d", &a, &b);
add(a, 1), add(b + 1, -1);
}
for (int i = 1; i <= n; i ++ )
if (i != n)
printf("%d ", ask(i));
else
printf("%d\n", ask(i));
}
return 0;
}
差分
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int s[N];
int n;
void insert(int l, int r, int c) {
s[l] += c;
s[r + 1] -= c;
}
int main() {
while (~scanf("%d", &n)) {
if (n == 0)
break;
memset(s, 0, sizeof s);
for (int i = 1; i <= n; i ++ ) {
int a, b;
scanf("%d%d", &a, &b);
insert(a, b, 1);
}
for (int i = 1; i <= n; i ++ )
s[i] += s[i - 1];
for (int i = 1; i <= n; i ++ )
if (i != n)
printf("%d ", s[i]);
else
printf("%d\n", s[i]);
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
LL tree[N * 4];
LL add_Mark[N * 4];
LL arr[N * 4];
int n, m;
LL ans;
void build_tree(int node, int l, int r) {
add_Mark[node] = 0;
if (l == r) {
tree[node] = arr[l];
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
build_tree(left_node, l, mid), build_tree(right_node, mid + 1, r);
tree[node] = tree[left_node] + tree[right_node];
}
void update_tree_son(int node, int l, int r) {
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
if (add_Mark[node]) {
add_Mark[left_node] += add_Mark[node];
add_Mark[right_node] += add_Mark[node];
tree[left_node] += add_Mark[node] * (mid - l + 1);
tree[right_node] += add_Mark[node] * (r - (mid + 1) + 1);
add_Mark[node] = 0;
}
}
void update_tree(int node, int l, int r, int start, int end, LL val) {
if (l >= start && r <= end) {
add_Mark[node] += val;
tree[node] += (r - l + 1) * val;
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
update_tree_son(node, l, r);
if (l == r)
return;
if (end <= mid)
update_tree(left_node, l, mid, start, end, val);
else if (start > mid)
update_tree(right_node, mid + 1, r, start, end, val);
else {
update_tree(left_node, l, mid, start, mid, val);
update_tree(right_node, mid + 1, r, mid + 1, end, val);
}
tree[node] = tree[left_node] + tree[right_node];
}
void query_tree(int node, int l, int r, int start, int end) {
if (l >= start && r <= end) {
ans += tree[node];
return;
}
int mid = (l + r) / 2;
int left_node = node * 2;
int right_node = node * 2 + 1;
update_tree_son(node, l, r);
if (l == r)
return;
if (end <= mid)
query_tree(left_node, l, mid, start, end);
else if (start > mid)
query_tree(right_node, mid + 1, r, start, end);
else {
query_tree(left_node, l, mid, start, mid);
query_tree(right_node, mid + 1, r, mid + 1, end);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
scanf("%lld", &arr[i]);
build_tree(1, 1, n);
char op[2];
while (m -- ) {
scanf("%s", op);
if (op[0] == 'C') {
int a, b;
LL c;
scanf("%d%d%lld", &a, &b, &c);
update_tree(1, 1, n, a, b, c);
} else {
int a, b;
scanf("%d%d", &a, &b);
ans = 0;
query_tree(1, 1, n, a, b);
printf("%lld\n", ans);
}
}
return 0;
}
问下,最后一篇中的update_tree_son这个函数是什么意思?
就是我们写的pushup操作、延迟标记的使用