线段树模板(有lazy数组):
#include<iostream>
using namespace std;
#define int long long
typedef long long ll;
const int N = 2e5 + 10;
#define lson rt << 1 // == rt * 2 左儿子
#define rson rt << 1 | 1 // == rt * 2 + 1 右儿子
#define int_mid int mid = tree[rt].l + tree[rt].r >> 1
int a[N]; // 初始值
struct node {
int l, r;
ll val, lazy;
} tree[N * 4];
void push_up(int rt) {
tree[rt].val = min(tree[lson].val, tree[rson].val);//最小值
//tree[rt].val = max(tree[lson].val, tree[rson].val);//最大值
// tree[rt].val = tree[lson].val + tree[rson].val;
}
void push_down(int rt) {
if (tree[rt].lazy) {
tree[lson].lazy += tree[rt].lazy;
tree[rson].lazy += tree[rt].lazy;
{ // 维护最大最小值
tree[lson].val += tree[rt].lazy;
tree[rson].val += tree[rt].lazy;
}
// { // 维护和
// int l = tree[rt].l, r = tree[rt].r;
// int mid = l + r >> 1;
// tree[lson].val += 1ll * (mid - l + 1) * tree[rt].lazy;
// tree[rson].val += 1ll * (r - mid) * tree[rt].lazy;
// }
tree[rt].lazy = 0;
}
}
void build(int rt, int l, int r) { // 建树
tree[rt].l = l, tree[rt].r = r;
tree[rt].lazy = 0;
if (l == r) {
tree[rt].val = a[l]; // 给定一个初始值
return;
} else {
int mid = l + r >> 1; // (l + r) / 2
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
}
}
void update_point(int rt, int pos, ll val) { // 单点更新
if (tree[rt].l == tree[rt].r && pos == tree[rt].l) {
tree[rt].val += val;
return;
}
int_mid;
if (pos <= mid) update_point(lson, pos, val);
else update_point(rson, pos, val);
push_up(rt);
}
void update_interval(int rt, int l, int r, ll val) { // 区间更新
if (l <= tree[rt].l && r >= tree[rt].r) {
tree[rt].lazy += val;
tree[rt].val += val; // 维护最大最小值
//tree[rt].val += 1ll * (tree[rt].r - tree[rt].l + 1) * val; // 维护和
return;
}
push_down(rt);
int_mid;
if (l <= mid) update_interval(lson, l, r, val);
if (r > mid) update_interval(rson, l, r, val);
push_up(rt);
}
ll query_point(int rt, int pos) { // 单点查询
if (tree[rt].l == tree[rt].r && tree[rt].l == pos)
return tree[rt].val;
push_down(rt);
int_mid;
if (pos <= mid) query_point(lson, pos);
else query_point(rson, pos);
}
ll query_interval(int rt, int l, int r) { // 区间查询
if (l <= tree[rt].l && r >= tree[rt].r)
return tree[rt].val;
push_down(rt);
int_mid;
if (r <= mid) return query_interval(lson, l, r);
else if (l > mid) return query_interval(rson, l, r);
else {
return min(query_interval(lson, l, mid), query_interval(rson, mid + 1, r));//最小值
//return max(query_interval(lson, l, mid), query_interval(rson, mid + 1, r));//最大值
// return query_interval(lson, l, mid) + query_interval(rson, mid + 1, r);//求和
}
}
signed main()
{
freopen("a.txt", "r", stdin);
// int n;
// cin >> n;
// for(int i = 1; i <= n; i ++){
// cin >> a[i];
// }
// build(1, 1, n);
// for(int i = 1; i <= 13; i ++){
// cout << tree[i].val << ' ';
// }
// cout << "\n";
// update_interval(1, 1, 2, 2);
// for(int i = 1; i <= 13; i ++){
// cout << tree[i].val << ' ';
// }
}
树状数组:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 10;
int n;
int tr[N];
int lowbit(int x){
return x & -x;
}
void update(int i, int v){
while(i <= n){
tr[i] += v;
i += lowbit(i);
}
}
int getsum(int i){
int res = 0;
while(i > 0){
res += tr[i];
i -= lowbit(i);
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
for(int j = 1; j <= T; j ++){
memset(tr, 0, sizeof tr);
printf("Case %d:\n", j);
scanf("%d", &n);
for(int i = 1; i <= n; i ++){
int x;
scanf("%d", &x);
update(i, x);
}
while(1){
string s;
cin >> s;
if(s == "End")break;
else if(s == "Add"){
int i, v;
scanf("%d%d", &i, &v);
update(i, v);
}else if(s == "Query"){
int st, ed;
scanf("%d%d", &st, &ed);
printf("%d\n", getsum(ed) - getsum(st - 1));
}else{
int i, v;
scanf("%d%d", &i, &v);
update(i, -1 *v);
}
}
}
}